Java:JCheckBox 不会与 ItemListener 保持一致 [英] Java: JCheckBox won't stay checked with ItemListener

查看:33
本文介绍了Java:JCheckBox 不会与 ItemListener 保持一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我是新来的听众(仍在学习语言),这是我第一次全面尝试实施它们(即不仅仅是教科书中的练习题).

到目前为止,除了一个大错误:复选框没有保持选中状态外,一切正常.我分配给它们的 ItemListener 运行得很好(我设置了一个 JOptionPane 来触发让我知道它是否在工作),但该框本身并未保持选中状态.

我更进一步,添加了条件逻辑,判断它的状态是被选中还是未选中,并发现当我点击框时,两个状态都会被触发.所以我得到了两个 JOptionPane 弹出窗口,一个显示是否选中该框的消息,另一个显示该框是否未选中.

我在这里包含了我的代码.我做错了什么?

附注.您会注意到代码具有添加单选按钮或复选框的条件逻辑.当程序最终运行时,该组件以两种格式在多个位置生成.单选按钮工作正常,这是我遇到上述问题的复选框.

创建复选框并分配侦听器的代码:

public OtherField(int voteFor){this.voteFor = 投票;otherPanel = new JPanel();otherPanel.setLayout(new GridLayout(1, 3));otherField = new JTextField(10);otherField.setHorizo​​ntalAlignment(SwingConstants.CENTER);JLabel 其他标签;otherLabel = new JLabel("Other", SwingConstants.CENTER);otherRadio = new JRadioButton("", false);otherRadio.setHorizo​​ntalAlignment(SwingConstants.CENTER);otherRadio.addActionListener(new OtherFieldRadioListener());otherCheckBox = new JCheckBox("");otherCheckBox.setHorizo​​ntalAlignment(SwingConstants.CENTER);otherCheckBox.addItemListener(new OtherFieldCheckBoxListener());otherPanel.add(otherLabel);otherPanel.add(otherField);if(voteFor == 1){otherPanel.add(otherRadio);}别的{otherPanel.add(otherCheckBox);}}

LISTENER CODE(与上面的代码属于同一个类的私有类):

私有类OtherFieldCheckBoxListener实现ItemListener{public void itemStateChanged(ItemEvent e){字符串名称 = otherField.getText();if(e.getStateChange() == ItemEvent.SELECTED){JOptionPane.showMessageDialog(null, name);}别的{JOptionPane.showMessageDialog(null, "未选中");}}}

解决方案

我会尝试的第一件事是在初始化时将复选框设置为 true 或 false,即

otherCheckBox.setSelected(false)

如果这不起作用,我会在每次选中复选框时检查是否从其他地方调用了其他字段,从而重绘组件/重置选择(使用调试器并在其他字段的开头设置断点)

Ok, I'm new to listeners (still learning the language), and this is my first full-scale attempt to implement them (ie more than just a practice problem in a textbook).

So far, everything is working fine except one big bug: the checkboxes don't stay checked. The ItemListener I assign them runs perfectly (I have a JOptionPane set up to trigger to let me know if it's working or not), but the box itself doesn't stay checked.

I went even further and added conditional logic for if it's state is checked versus unchecked, and found that when I click the box BOTH states get triggered. So I get both JOptionPane popups, the one with the message for if the box is checked and the one for if the box isn't checked.

I'm including my code here. What am I doing wrong?

PS. You'll notice that the code has conditional logic to either add a radio button or a checkbox. When the program finally runs, this component is generated in multiple locations in both formats. The radio button works fine, it's the checkbox ones that I'm having the above issue with.

CODE THAT CREATES THE CHECKBOXES AND ASSIGNS THE LISTENERS:

public OtherField(int voteFor){


            this.voteFor = voteFor;


            otherPanel = new JPanel();
            otherPanel.setLayout(new GridLayout(1, 3));


            otherField = new JTextField(10);
            otherField.setHorizontalAlignment(SwingConstants.CENTER);

            JLabel otherLabel;
            otherLabel = new JLabel("Other", SwingConstants.CENTER);

            otherRadio = new JRadioButton("", false);
            otherRadio.setHorizontalAlignment(SwingConstants.CENTER);
            otherRadio.addActionListener(new OtherFieldRadioListener());

            otherCheckBox = new JCheckBox("");
            otherCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
            otherCheckBox.addItemListener(new OtherFieldCheckBoxListener());

            otherPanel.add(otherLabel);
            otherPanel.add(otherField);

            if(voteFor == 1){
                otherPanel.add(otherRadio);
            }else{
                otherPanel.add(otherCheckBox);
            }



        }

LISTENER CODE (it's a private class in the same class as the code above):

private class OtherFieldCheckBoxListener implements ItemListener{
            public void itemStateChanged(ItemEvent e){
                String name = otherField.getText();
                if(e.getStateChange() == ItemEvent.SELECTED){
                    JOptionPane.showMessageDialog(null, name);
                }else{
                    JOptionPane.showMessageDialog(null, "Not Selected");
                }


            }   
        }

解决方案

First thing I would try is to set the checkbox either to true or false when you initialize it, i.e

otherCheckBox.setSelected(false) 

If this does not work I would check whether OtherField gets called from somewhere else everytime the checkbox is selected and thus the components are redrawn/ the selection is reset (use the debugger and set a breakpoint at the beginning of OtherFields)

这篇关于Java:JCheckBox 不会与 ItemListener 保持一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆