简体   繁体   中英

ActionListener on a JButton does not perform any action

I have a slight issue with my Actionlistener, when i click button nothing happens ?? I do not see where the problem is, so another pair of eyes could help me out :)

 public class GameOptions extends JPanel implements ActionListener{ 
    public GameOptions(){
        System.out.println("GameOptions Class test blabla");

        easyButton().addActionListener(this);
        mediumButton().addActionListener(this);
        hardButton().addActionListener(this);

        JPanel center = new JPanel(new GridLayout(4,1,10,10));
        center.add(new JLabel("Chose Difficulty Level"));
        center.add(easyButton());
        center.add(mediumButton());
        center.add(hardButton());

        this.add(center, BorderLayout.CENTER);
        this.setPreferredSize(this.getPreferredSize()); 
        this.setFocusable(true);
        this.requestFocusInWindow();
    }
    private JButton easyButton(){
        JButton levelEasy = new JButton("Easy");
        return levelEasy;
    }
    private JButton mediumButton(){
        JButton levelMedium = new JButton("Medium");
        return levelMedium;
    }
    private JButton hardButton(){
        JButton levelHard = new JButton("Hard");
        return levelHard;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();
        if(src == easyButton()){
            System.out.println("Easy");
        }
        else if(src == mediumButton()){
            System.out.println("Medium");
        }
        else if(src == hardButton()){
            System.out.println("Hard");
        }
        else{

        }
      }
    }

Your xxxButton() methods create new JButtons each time, and so you add the ActionListener to a newly created JButton and then discard the button, and then add a completely different JButton, one without the ActionListener to the GUI.

Suggestion: create your JButtons, set a variable to them, add your ActionListener, and add the same button to the GUI.

So instead of this:

easyButton().addActionListener(this);  // creates one JButton

center.add(easyButton());   // creates a completey different JButton

do this:

JButton easyButton = easyButton();
easyButton.addActionListener(this);

center.add(easyButton);

Note, if this were my code, I'm not sure that I'd use JButtons at all. Instead perhaps I'd use either JRadioButtons or a JComboBox.

You're creating each JButton with a function. And later you try to add it like center.add(easyButton()); but the one you added a ActionListener isn't the same button as this one. You're creating each one with new , so the reference isn't the same.

You should do it like this:

JButton buttonEasy = easyButton();
buttonEasy.addActionListener(this);
center.add(buttonEasy);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM