简体   繁体   English

未选择单选按钮时调用ActionEvent

[英]Having ActionEvent called when Radio Button is not selected

I've noticed that ActionEvent would still be triggered within my group of JRadioButtonMenuItem even when specifying the conditional statement: 我注意到即使指定条件语句,ActionEvent仍会在我的JRadioButtonMenuItem组中触发:

if(!button.isSelected())
    //Do stuff

defaultTheme = new JRadioButtonMenuItem("Default theme");
    defaultTheme.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(!defaultTheme.isSelected())  
                System.out.println("temp");
        }
    });

I have multiple theme options within my settings menu, however if a say (say default) is already selected, I don't want to execute any redundant code if the default menu is already selected and the user clicks on the already selected Radio Button. 我的设置菜单中有多个主题选项,但是,如果已经选择了说(说是默认) ,那么如果已经选择了默认菜单并且用户单击了已选择的单选按钮,则我不想执行任何冗余代码。

ActionListener will tell you whenever the button is "actioned" (clicked, pressed, what ever), which doesn't always change it's state. 每当按钮被“操作”(单击,按下,按任何按钮)时, ActionListener都会告诉您,这并不总是会更改其状态。 Instead, you could attach a ItemListener to the buttons model, which will tell, more accurately, when the actual state of the button changes, for example... 相反,您可以将ItemListener附加到按钮模型,该模型将更准确地告知按钮的实际状态何时更改,例如...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ButtonTest {

    public static void main(String[] args) {
        new ButtonTest();
    }

    public ButtonTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            ButtonGroup bg = new ButtonGroup();
            final JRadioButton bananas = new JRadioButton("Bananas");
            final JRadioButton apples = new JRadioButton("Apples");
            bg.add(bananas);
            bg.add(apples);

            bananas.getModel().addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    System.out.println("Bananas " + bananas.isSelected());
                }
            });
            apples.getModel().addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    System.out.println("Apples " + apples.isSelected());
                }
            });

            add(bananas, gbc);
            add(apples, gbc);

        }

    }

}

Not sure since I haven't seen the rest of your program, but you have to put all the radiobuttons in a ButtonGroup. 不确定,因为我还没有看到程序的其余部分,但是您必须将所有单选按钮都放在ButtonGroup中。 Because if you don't it would be impossible to deselect the radiobutton. 因为如果不这样做,则取消选择单选按钮是不可能的。

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

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