简体   繁体   English

JButton上的ActionListener不执行任何操作

[英]ActionListener on a JButton does not perform any action

I have a slight issue with my Actionlistener, when i click button nothing happens ?? 我的Actionlistener有一个小问题,当我点击按钮没有任何反应? 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. 您的xxxButton()方法每次都会创建新的JButton,因此您将ActionListener添加到新创建的JButton中,然后丢弃该按钮,然后添加一个完全不同的JButton,一个没有ActionListener的JButton到GUI。

Suggestion: create your JButtons, set a variable to them, add your ActionListener, and add the same button to the GUI. 建议:创建JButtons,为它们设置变量,添加ActionListener,并将相同的按钮添加到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. 注意,如果这是我的代码,我不确定我是否会使用JButtons。 Instead perhaps I'd use either JRadioButtons or a JComboBox. 相反,也许我会使用JRadioButtons或JComboBox。

You're creating each JButton with a function. 您正在使用函数创建每个JButton And later you try to add it like center.add(easyButton()); 然后你尝试添加它像center.add(easyButton()); but the one you added a ActionListener isn't the same button as this one. 但是你添加ActionListener的那个按钮与这个按钮不同。 You're creating each one with new , so the reference isn't the same. 你用new创建每一个,所以引用不一样。

You should do it like this: 你应该这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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