简体   繁体   English

Button.isSelected() 不返回 true

[英]Button.isSelected() does not return true

I'm creating a JFrame with 3 buttons, 3 checkboxes, and 3 radio buttons.我正在创建一个带有 3 个按钮、3 个复选框和 3 个单选按钮的 JFrame。 I'm having a problem with a function that is excecuted when 1 of the 3 buttons is pressed.我在按下 3 个按钮中的 1 个时执行的功能出现问题。 This is the function:这是函数:

private void setCenterColors() {

    if(redB.isSelected()) {
        center.setBackground(Color.RED);
    } else if (greenB.isSelected()) {
        center.setBackground(Color.GREEN);
    } else if (blueB.isSelected()){
        center.setBackground(Color.BLUE);   
    }
    System.out.println(center.getBackground());
}

redB, greenB, and blueB are all JButtons. redB、greenB 和 blueB 都是 JButton。

The event handler for the buttons is:按钮的事件处理程序是:

class ChoiceListenerButton implements ActionListener {
        public void actionPerformed(ActionEvent event)
        {
            setCenterColors();
            repaint();
        }
    }

    listenerButton = new ChoiceListenerButton();

When i excecute the program and press the buttons, all of them returns false, when one of them should return true.当我执行程序并按下按钮时,它们都返回false,而其中一个应该返回true。 What should i do to see which button is pressed?我应该怎么做才能看到按下了哪个按钮? Any help is appreciated, if you need full code, please ask and i will replay as soon as is see the notification.感谢任何帮助,如果您需要完整的代码,请询问,我会在看到通知后立即重播。 Thank you.谢谢你。

Do it as follows:请按以下步骤操作:

class ChoiceListenerButton implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        setCenterColors(event);
        repaint();
    }

    private void setCenterColors(ActionEvent event) {
        if(event.getSource() == redB) {
            center.setBackground(Color.RED);
        } else if (event.getSource() == greenB) {
            center.setBackground(Color.GREEN);
        } else if (event.getSource() == blueB){
            center.setBackground(Color.BLUE);   
        }
        System.out.println(center.getBackground());
    }
}

Also, make sure you have added listenerButton to buttons eg redB.addActionListener(listenerButton) .另外,请确保您已将listenerButton添加到按钮,例如redB.addActionListener(listenerButton)

There is nothing wrong doing it the way you are.按照你的方式去做并没有错。 But you might find this useful in the future.但你可能会发现这在未来很有用。

Since the actionListener is a Functional Interface you can use a lambda to specify the actionListener.由于actionListener是一个Functional Interface您可以使用 lambda 来指定 actionListener。 In your case an example would be:在你的情况下,一个例子是:

        JButton red = new JButton("Set Red");
        red.addActionListener((ae) -> {
            panel.setBackground(Color.red);
            panel.repaint();
        });

For more complicated listeners, you can create a private inner class and pass the necessary information via the constructor.对于更复杂的侦听器,您可以创建一个private inner class并通过构造函数传递必要的信息。 Since it is an inner class, it has access to the instance field panel defined in the outer, containing class.由于它是一个内部类,它可以访问在外部包含类中定义的实例字段panel

      private class ButtonListener implements ActionListener {
        private Color color;

        public ButtonListener(Color color) {
            this.color = color;
        }

        public void actionPerformed(ActionEvent ae) {
            panel.setBackground(color);
            panel.repaint();
        }
      }

You would use it as follows:您可以按如下方式使用它:

       button.addActionListener(new ButtonListener(Color.red));

The idea is that you can use multiple action listeners to handle different siutations or requirements.这个想法是您可以使用多个动作侦听器来处理不同的情况或要求。 In this case to set the appropriate color.在这种情况下要设置适当的颜色。

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

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