dcsimg
multiple action listeners
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

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

Re: multiple action listeners

Posted By:   Almagest_FUTT  
Posted On:   Monday, December 12, 2005 04:07 PM

Does you code compile at all ?

You'z got a "staticity" problem.


Look there:


  • You're creating your GUI in a static context (createAndShowGUI()).
  • In this static context, you've got a variable "btn1PlayerButton", "btnExit", and so forth.
  • You're assigning Paul (the instance) as an ActionListener to these.

  • Within Paul, seen as an ActionListener, you're checking the event's source for being btn1PlayerButton, etc.
  • But, within Paul the instance, btn1PlayerButton refers to Paul's instance member field named so.

    Now, i suppose you have such a field, if your code does compile at all.

    However, when, in your static createAndShowGUI(), you declare:

    JButton btn1PlayerButton = new JButton ("One Player");,
    you have, as of then, a new Lvalue named "btn1PlayerButton" that has nothing to do with Paul's member field, and which, even more, couldn't have anything to do with it since, with the createAndShowGUI() being static, it knows naught of Paul the instance and its member fields.


    You'd have the very same problem, by the way, were your createAndShowGUI() not static, for by declaring:

    JButton btn1PlayerButton = new JButton ("One Player");,
    you'd overrule, within the actual scope (the method's), the Lvalue "btn1PlayerButton" (if you didn't want to overrule it, you'd have to omit the type [JButton]).


  • Not sure whether i'm making myself clear.

  • Easiest workaround: work with actionCommands. Assign your buttons action commands (setActionCommand(String)) and query these in the actionPerformed.

  • BTW, I believe your topic should be AWT or Swing

About | Sitemap | Contact