如何使用JCheckBoxes选择使用?
我在 JFrame 上有一个复选框。当我检查它时,我想在命令窗口上显示它已被选择。下面是我正在使用的代码。它编译并执行时没有错误,但是当我选择复选框时,我没有在窗口上看到“已选择一个”。
public Checklist() {
...
JCheckBox one = new JCheckBox("CT scan performed");
one.addItemListener(new CheckBoxListener());
}
private class CheckBoxListener implements ItemListener{
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==one){ if(one.isSelected()){
System.out.println("one has been selected");
}
else{System.out.println("nothing");}
}
}}
I have a checkbox on a JFrame. When I check it, I want to display on the command window that it has been selected. Below is the code i am working with. It compiles and executes without errors, but I don't get the "one has been selected" on the window when I select the checkbox.
public Checklist() {
...
JCheckBox one = new JCheckBox("CT scan performed");
one.addItemListener(new CheckBoxListener());
}
private class CheckBoxListener implements ItemListener{
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==one){ if(one.isSelected()){
System.out.println("one has been selected");
}
else{System.out.println("nothing");}
}
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我已经测试了这个简单的示例,它工作得很好(当您选择复选框时,它会写
"one has been selected"
,当您取消选择它时,它会写"nothing"
):在您的示例中,似乎在构造函数
CheckList()
中声明了one
。确定可以在内部类CheckBoxListener
中访问到吗?I have tested this simple example and it works perfectly (it writes
"one has been selected"
when you select the checkbox, and"nothing"
when you deselect it):In your example, it seems that
one
is declared in the constructorCheckList()
. Are you sure that it can be accessed in the inner classCheckBoxListener
?您可能没有意识到您的程序中实际上有两个复选框!
我假设你的类看起来像这样:
你有两个“one”的副本,属于 Checklist 的“private JCheckBox one”,以及构造函数中的“JCheckBox one = ...”。然后您会注意到,当您调用时
,您实际上在构造函数中使用临时“一”,这与类顶部的“一”不同!
现在,当您致电时,
您现在使用的是班级顶部的“一”,这不是您在窗口中看到的复选框!
这就是为什么删除“JCheckBox”会使您的程序正常工作 - 现在构造函数中的“一”与类顶部的“一”相同。
为了使这一点更加清晰,以下代码是您在损坏的示例中真正执行的操作:
You may not realize that you actually have two checkboxes going on in your program!
I assume your class looks something like this:
You have two copies of "one", the "private JCheckBox one" that belongs to Checklist, and the "JCheckBox one = ..." in your constructor. You'll then notice that when you call
you are actually using the temporary "one" in the constructor, which is NOT the same as the "one" at the top of your class!
Now, when you call
you are now using the "one" at the top of your class, which is NOT the checkbox you see in your window!
This is why removing that "JCheckBox" makes your program work--now the "one" in your constructor is the same as the "one" at the top of your class.
To make this more, clear, the following code is what you are REALLY doing in your broken example:
您需要在每个复选框上添加一个动作侦听器(第七个复选框除外,这不会产生任何影响),并在每次调用侦听器时重新计算按钮的启用状态。请参阅
addActionListener
。You need to add an action listener on every checkbox except the seventh one, which doesn't have any impact), and re-compute the enabled state of the button each time the listener is called. See
addActionListener
.为了实现这一目标,您将需要一种方法来检查您关心的所有情况,并在达到某种状态时采取适当的操作。然后为每个调用此工作方法的复选框设置一个 ActionListener。
In order to achieve this, you will want one method that will check for all the cases you care about and take the appropriate action when a state is achieved. Then have an ActionListener for each checkbox that calls this one worker method.
你检查一下这条线是否
正常?尝试删除它,看看是否有帮助。
事件可能会因真实来源而令人痛苦。
Have you checked if this line
is ok? Try removing this and see if it helps.
Events may be a pain with real sources.