如何使用JCheckBoxes选择使用?

发布于 2024-10-18 08:39:50 字数 586 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

残花月 2024-10-25 08:39:50

我已经测试了这个简单的示例,它工作得很好(当您选择复选框时,它会写 "one has been selected" ,当您取消选择它时,它会写 "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();
    }
}

:在您的示例中,似乎在构造函数 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):

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(). Are you sure that it can be accessed in the inner class CheckBoxListener?

梦里°也失望 2024-10-25 08:39:50

您可能没有意识到您的程序中实际上有两个复选框!

我假设你的类看起来像这样:

public class Checklist extends JFrame {
    private JCheckBox one;

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

你有两个“one”的副本,属于 Checklist 的“private JCheckBox one”,以及构造函数中的“JCheckBox one = ...”。然后您会注意到,当您调用时

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

,您实际上在构造函数中使用临时“一”,这与类顶部的“一”不同!

现在,当您致电时,

if(e.getSource() == one)

您现在使用的是班级顶部的“一”,这不是您在窗口中看到的复选框!

这就是为什么删除“JCheckBox”会使您的程序正常工作 - 现在构造函数中的“一”与类顶部的“一”相同。

为了使这一点更加清晰,以下代码是您在损坏的示例中真正执行的操作:

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 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. 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!

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.

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!
岁月无声 2024-10-25 08:39:50

您需要在每个复选框上添加一个动作侦听器(第七个复选框除外,这不会产生任何影响),并在每次调用侦听器时重新计算按钮的启用状态。请参阅 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.

荒芜了季节 2024-10-25 08:39:50

为了实现这一目标,您将需要一种方法来检查您关心的所有情况,并在达到某种状态时采取适当的操作。然后为每个调用此工作方法的复选框设置一个 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.

辞旧 2024-10-25 08:39:50

你检查一下这条线是否

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

正常?尝试删除它,看看是否有帮助。
事件可能会因真实来源而令人痛苦。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文