hello, i am trying to link to class files. one is called menu1.java and one is called Trialgrid.java. menu1.java is a menu screen that has a utton called btn1PlayerButton and it is used to open Trialgrid.java file!!
my problem is i can't get the contents of my page to actually display, i can only seem to add one item (eg. a button) and not that whole program?? can anyone help??
here is my code for the menu1.java file:-
import javax.swing.* ;
import javax.swing.event.* ;
import java.awt.* ;
import java.awt.event.* ;
publicclass Menu1 implements ActionListener {
public Menu1 ()
{
}
privatestaticvoid createAndShowGUI (Menu1 paul)
{
JFrame frame = new JFrame ("Welcome to BattleShips");
Container content = frame.getContentPane();
content.setLayout (new BorderLayout());
JButton btn1PlayerButton = new JButton ("One Player");
JButton btnRules = new JButton ("Rules");
JButton btnExit = new JButton ("Exit");
Dimension menuButtonSize = new Dimension(110, 25);
JLabel lblTitle = new JLabel ("Battleships 3000");
JLabel lbl1PlayerGame = new JLabel ("Click to start a one player game");
JLabel lblRules = new JLabel ("To see the rules click here");
JLabel lblExit = new JLabel ("Exit the program");
JPanel north = new JPanel();
north.setPreferredSize(new Dimension(110,115));
north.add(lblTitle);
lblTitle.setForeground(Color.blue);
content.add(north, BorderLayout.NORTH);
JPanel center = new JPanel();
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(160,25)); // Used to create a blank space in menu
center.add(jp);
content.add(center, BorderLayout.CENTER);
JPanel west = new JPanel();
west.setLayout(new BorderLayout());
JPanel wNorth = new JPanel();
JPanel wCenter = new JPanel();
JPanel wSouth = new JPanel();
wSouth.setPreferredSize(new Dimension(110,115));
wNorth.add(lbl1PlayerGame);
wCenter.add(btn1PlayerButton);
btn1PlayerButton.setPreferredSize(menuButtonSize);
btn1PlayerButton.addActionListener (paul);
west.add(wNorth, BorderLayout.NORTH);
west.add(wCenter, BorderLayout.CENTER);
west.add(wSouth, BorderLayout.SOUTH);
content.add(west, BorderLayout.WEST);
JPanel east = new JPanel();
east.setLayout(new BorderLayout());
JPanel eNorth = new JPanel();
JPanel eCenter = new JPanel();
JPanel eSouth = new JPanel();
eSouth.setPreferredSize(new Dimension(110,115));
eNorth.add(lblRules);
eCenter.add(btnRules);
btnRules.setPreferredSize(menuButtonSize);
east.add(eNorth, BorderLayout.NORTH);
east.add(eCenter, BorderLayout.CENTER);
east.add(eSouth, BorderLayout.SOUTH);
content.add(east, BorderLayout.EAST);
JPanel south = new JPanel();
south.setLayout(new BorderLayout());
JPanel sNorth = new JPanel();
JPanel sCenter = new JPanel();
sNorth.add(lblExit);
sCenter.add(btnExit);
btnExit.setPreferredSize(menuButtonSize);
btnExit.addActionListener(paul);
south.add(sNorth, BorderLayout.NORTH);
south.add(sCenter, BorderLayout.SOUTH);
content.add(south, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(550, 400);
frame.setResizable(false);
}
publicvoid actionPerformed (ActionEvent ae)
{
/*Object source = ae.getSource();
if (source == btn1PlayerButton)
{*/
TrialGrid grid = new TrialGrid () ;
grid.showGrid () ;
//}
/*if (souce == btnExit)
{
System.exit(0);
}*/
}
publicstaticvoid main (String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
publicvoid run()
{
Menu1 paul = new Menu1() ;
createAndShowGUI(paul);
}
});
}
}
I have commented out one of the action listeners cos i can't get it to work!?!?!?!
and here is my code for Trialgrid.java:-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class TrialGrid extends JFrame implements ActionListener
{
JButton btnStart = new JButton("Start Game"); //These buttons are menu buttons
JButton btnSub = new JButton("Submarine");
JButton btnCruiser = new JButton("Cruiser");
JButton btnDestroy = new JButton("Destroyer");
JButton btnBattle = new JButton("Battleship");
JButton btnReset = new JButton("Reset Game");
JButton btnMenu = new JButton("Menu");
JButton btnExit = new JButton("Exit");
JButton btnSetDirection = new JButton("Direction"); // *** DECLARING DIRECTION BUTTON ***
JButton[][] enemyButtons = new JButton[10][10];
JButton[][] playerButtons = new JButton[10][10];
JLabel lblTitle = new JLabel(" Welcome to Battleships", SwingConstants.CENTER);
JLabel lblPlayer1 = new JLabel("Your Battleship grid", SwingConstants.LEFT);
JLabel lblPlayer2 = new JLabel(" The enemies Battleship grid", SwingConstants.RIGHT);
JLabel lblList = new JLabel("List of ships");
Dimension gridButtonSize = new Dimension(20, 20);
Dimension menuButtonSize = new Dimension(110, 25);
public TrialGrid ()
{
int buttonNumber = 0;
JFrame frame = new JFrame("One Player"); // This produces a Border layout that contains 5 panels
Container content = frame.getContentPane();
JPanel title = new JPanel();
title.add(lblPlayer1);
title.add(lblTitle);
title.add(lblPlayer2);
content.add(title, BorderLayout.NORTH); // North panel holds just titles
JPanel center = new JPanel();
center.setLayout(new BorderLayout());
JPanel controls = new JPanel();
JPanel menu = new JPanel();
center.add(btnStart, BorderLayout.NORTH);
btnStart.setPreferredSize(menuButtonSize);
center.add(btnSetDirection, BorderLayout.SOUTH);
btnSetDirection.setPreferredSize(menuButtonSize); // *** HERE IS MY BUTTON TO SET DIRECTION ***
//btnSetDirection.setDirection(1); // *** CANNOT GET THIS TO TALK TO LOWER FUNCTION ***
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(110,25)); // Used to create a blank space in menu
menu.add(jp);
menu.add(lblList);
menu.add(btnSub);
btnSub.setPreferredSize(menuButtonSize);
menu.add(btnCruiser);
btnCruiser.setPreferredSize(menuButtonSize);
menu.add(btnDestroy);
btnDestroy.setPreferredSize(menuButtonSize);
menu.add(btnBattle);
btnBattle.setPreferredSize(menuButtonSize);
center.add(controls);
center.add(menu);
content.add(center, BorderLayout.CENTER); //Center panel just contains a menu style list of buttons
JPanel grid1 = new JPanel();
grid1.setLayout(new GridLayout(10, 10)); // sets the grid layout for player 1
for (int i = 0; i < playerButtons.length; i++)
{
for (int j = 0; j < playerButtons[i].length; j++)
{
playerButtons[i][j] = new JButton();
playerButtons[i][j].setPreferredSize(gridButtonSize);
playerButtons[i][j].setBackground(Color.blue);
grid1.add(playerButtons[i][j]);
}
}
content.add(grid1, BorderLayout.WEST); // Sets grid1 to west in border layout manager
JPanel grid2 = new JPanel();
grid2.setLayout(new GridLayout(10, 10)); // sets the grid layout for the computer
for (int i = 0; i < enemyButtons.length; i++)
{
for (int j = 0; j < enemyButtons[i].length; j++)
{
enemyButtons[i][j] = new JButton();
enemyButtons[i][j].setPreferredSize(gridButtonSize);
enemyButtons[i][j].addActionListener(this);
enemyButtons[i][j].setBackground(Color.yellow);
grid2.add(enemyButtons[i][j]);
}
}
content.add(grid2, BorderLayout.EAST); // Sets grid2 to east in border layout manager
JPanel form = new JPanel();
form.add(btnMenu);
btnMenu.setPreferredSize(menuButtonSize);
form.add(btnReset);
btnReset.setPreferredSize(menuButtonSize);
btnReset.addActionListener(this);
form.add(btnExit);
btnExit.setPreferredSize(menuButtonSize);
btnExit.addActionListener(this);
content.add(form, BorderLayout.SOUTH); // This just contains two buttons that functional buttons
frame.pack();
frame.setSize(550, 400);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoid main (String args[])
{
Trialgrid gl = new Trialgrid();
}
publicvoid actionPerformed(ActionEvent evt) // This makes the exit button work
{
Object source = evt.getSource();
if (source == btnExit)
{
System.exit(0);
}
for (int i = 0; i < enemyButtons.length; i++) // Allows you to change the colour of buttons on enemy grid
{
for (int j = 0; j < enemyButtons[i].length; j++)
{
if (source == enemyButtons[i][j])
{
enemyButtons[i][j].setBackground(Color.RED);
}
}
}
for (int i = 0; i < enemyButtons.length; i++) // Allows you to reset all the enemy buttons back to yellow
{
for (int j = 0; j < enemyButtons[i].length; j++)
{
if (source == btnReset)
{
enemyButtons[i][j].setBackground(Color.YELLOW);
}
}
}
}
publicvoid showGrid ()
{
this.getContentPane().add(frame);
this.setSize (500,500) ;
this.setVisible (true) ;
}
}