Posted By:
ben_hewitt
Posted On:
Monday, December 12, 2005 03:29 PM
hello! I have a class here that creates a small menu, the "btn1PlayerButton" opens another class file and the actionListener works fine, but i have two other buttons. One of them is "btnExit" and i want that to close the program, i know the code and have it there....... my problem is it assigns both buttons to have the first action, can anyone help me?? here's my code! import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class Menu1 implements ActionListener { private static void createAndShowGUI (Menu1 paul) { JButton btn1PlayerButton = new JButton ("One Player"
More>>
hello!
I have a class here that creates a small menu, the "btn1PlayerButton" opens another class file and the actionListener works fine, but i have two other buttons. One of them is "btnExit" and i want that to close the program, i know the code and have it there.......
my problem is it assigns both buttons to have the first action, can anyone help me??
here's my code!
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Menu1 implements ActionListener
{
private static void createAndShowGUI (Menu1 paul)
{
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");
JFrame frame = new JFrame ("Welcome to BattleShips");
Container content = frame.getContentPane();
content.setLayout (new BorderLayout());
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_CLOS E);
frame.setVisible(true);
frame.setSize(550, 400);
frame.setResizable(false);
}
public void actionPerformed (ActionEvent ae)
{
/*Source source = evt.getSource();
if (source == btn1PlayerButton)
{*/
TrialGrid grid = new TrialGrid();
grid.showGrid();
/*}
if (source == btnExit)
{
System.exit(0);
}*/
}
public static void main (String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Menu1 paul = new Menu1();
createAndShowGUI(paul);
}
});
}
}
Thank you
<<Less