简体   繁体   English

如何使用JCheckBoxes选择?

[英]How to use JCheckBoxes selection for use?

I have a checkbox on a JFrame. 我在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");}
            }
     }}

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): 我已经测试了这个简单的例子并且它完美地工作(当你选择复选框时它会写"one has been selected" "nothing"当你取消选中时它写着"nothing" ):

import javax.swing.*;
import java.awt.event.*;

public class Example extends JFrame{
    public JCheckBox one;

    public Example() {
        one = new JCheckBox("CT scan performed");
        one.addItemListener(new CheckBoxListener());
        setSize(300,300);
        getContentPane().add(one);
        setVisible(true);
    }

    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");}
            }
        }
    }

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

In your example, it seems that one is declared in the constructor CheckList() . 在您的示例中,似乎在构造函数CheckList()声明了one Are you sure that it can be accessed in the inner class CheckBoxListener ? 你确定它可以在内部类CheckBoxListener访问吗?

You may not realize that you actually have two checkboxes going on in your program! 您可能没有意识到您的程序中实际上有两个复选框!

I assume your class looks something like this: 我假设你的课看起来像这样:

public class Checklist extends JFrame {
    private JCheckBox one;

    public Checklist() {
        JCheckBox one = new JCheckBox("CT scan performed");
        one.addItemListener(new CheckBoxListener());
        this.add(one);
    }
}

You have two copies of "one", the "private JCheckBox one" that belongs to Checklist, and the "JCheckBox one = ..." in your constructor. 你有两个“one”副本,一个属于Checklist的“private JCheckBox one”,以及你的构造函数中的“JCheckBox one = ...”。 You'll then notice that when you call 当你打电话时,你会注意到

one.addItemListener(new CheckBoxListener());
this.add(one);

you are actually using the temporary "one" in the constructor, which is NOT the same as the "one" at the top of your class! 你实际上是在构造函数中使用临时的“one”,这与你班级顶层的“one”不同!

Now, when you call 现在,当你打电话

if(e.getSource() == one)

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. 这就是删除“JCheckBox”使你的程序工作的原因 - 现在构造函数中的“one”与你类顶部的“one”相同。

To make this more, clear, the following code is what you are REALLY doing in your broken example: 为了更清楚地说明这一点,下面的代码就是你在破碎的例子中真正做的事情:

public class Checklist extends JFrame {
    private JCheckBox one;

    public Checklist() {
        JCheckBox anotherOne = new JCheckBox("CT scan performed");
        anotherOne.addItemListener(new CheckBoxListener());
        this.add(anotherOne);
    }
}

...

if(e.getSource() == one)  //not equal to anotherOne!

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 . 请参见addActionListener

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. 然后为每个复选框都有一个ActionListener,用于调用这一个worker方法。

Have you checked if this line 你检查过这条线吗?

if(e.getSource()==one){

is ok? 好吗? Try removing this and see if it helps. 尝试删除它,看看它是否有帮助。 Events may be a pain with real sources. 事件可能是真实来源的痛苦。

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

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