Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 附加到JComboBox的ItemListener存在问题_Java_Swing_User Interface_Jcombobox_Jradiobutton - Fatal编程技术网

Java 附加到JComboBox的ItemListener存在问题

Java 附加到JComboBox的ItemListener存在问题,java,swing,user-interface,jcombobox,jradiobutton,Java,Swing,User Interface,Jcombobox,Jradiobutton,请看一下下面的代码 import java.awt.event.*; import java.awt.*; import javax.swing.*; public class ComboIssue extends JFrame { private JRadioButton rOne, rTwo; private ButtonGroup group; private JComboBox combo;

请看一下下面的代码

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

    public class ComboIssue extends JFrame
    {
        private JRadioButton rOne, rTwo;
        private ButtonGroup group;

        private JComboBox combo;

        private JLabel label;

        public ComboIssue()
        {
            rOne = new JRadioButton("One");
            rOne.addActionListener(new ROneAction());
            rTwo = new JRadioButton("Two");
            rTwo.addActionListener(new RTwoAction());
            group = new ButtonGroup();
            group.add(rOne);
            group.add(rTwo);

            combo = new JComboBox();        
            combo.addItem("No Values");
            combo.addItemListener(new ComboAction());

            label = new JLabel("labellLabel");

            this.setLayout(new FlowLayout());
            this.add(rOne);
            this.add(rTwo);
            this.add(combo);
            this.add(label);


        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        }

        private class ROneAction implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                combo.removeAllItems();
                combo.addItem("One");
            }
        }

        private class RTwoAction implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                combo.removeAllItems();
                combo.addItem("Two");
            }
        }

        private class ComboAction implements ItemListener
        {
            public void itemStateChanged(ItemEvent ie)
            {
                if(ie.getStateChange() == ItemEvent.SELECTED)
                {
                    label.setText("Selected");
                }
            }
        }

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



}
我期待的是

  • 选择一个单选按钮。它将替换组合框中的值
  • 从组合框中选择一个值。现在,JLabel文本将设置为“选定”

  • 但事实并非如此。相反,只要您选择一个单选按钮,JLabel文本就会更改!!!为什么会这样?请帮忙

    这是因为
    ComboAction实现了ItemListener
    。 您没有更改combobox的值吗? 当您选择单选按钮的值时

    更新:

    嗯,您的代码有点问题。它更改了label的值,因为您有一个
    ItemListener
    。因此我采用了
    PopupMenuListener
    ,当列表变为不可见时,它将进行拍摄。可以根据您的需要正常工作

    代码:


    这是因为单选按钮的动作侦听器中有此代码
    combo.removeAllItems()

    单击单选按钮时,在将特定单选按钮文本添加到组合框之前,您将删除所有项目
    在这之后,
    JComboBox
    中只剩下单击单选按钮后添加的项目,默认情况下选中该单选按钮,然后调用
    JComboBox
    itemStateChanged
    ,然后更改
    JLabel
    上的文本。在这里,只需做一些小的更改即可满足您的需要

    private class ROneAction implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                label.setText("Nothing Selected");   
                combo.removeAllItems();
                combo.addItem("One");
            }
        }
    
          private class RTwoAction implements ActionListener
          {
            public void actionPerformed(ActionEvent ae)
            {
                label.setText("Nothing Selected");
                combo.removeAllItems();
                combo.addItem("Two");
            }
           }
            private class ROneAction implements ActionListener
            {
                public void actionPerformed(ActionEvent ae)
                {
                    combo.removeAllItems();
                    combo.addItem("Select");
                    combo.addItem("One");
                }
            }
    
            private class RTwoAction implements ActionListener
            {
                public void actionPerformed(ActionEvent ae)
                {
                    combo.removeAllItems();
                    combo.addItem("Select");
                    combo.addItem("Two");
                }
            }
    
            private class ComboAction implements ItemListener
            {
                public void itemStateChanged(ItemEvent ie)
                {
                    if(ie.getItem().equals("Two"))
                    {
                        label.setText("Two Selected");
                    } else if(ie.getItem().equals("One") ) {
                        label.setText("One Selected");
                    }
                }
            }
    

    这是因为当您将第一项添加到JComboBox时,它会自动被选中。这会依次触发ItemEvent。我不确定您在这里想要实现什么,因为您的组合框只包含一个值。当选择单选按钮时,这仍然会更改标签的文本。
    itemstener
    将在您向组合框添加项目时被激发。OP不希望对标签做任何操作,选择单选按钮时。仅选择组合框时,它应更改文本。对不起,如果我没有收到你的代码,可能的话,你能发布一个小的EG作为我澄清的更新吗?@joeyrohan代码在问题中,只有我在需要的地方做了更改。我的意思是
    Combo的ItemListener
    private class ROneAction implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                label.setText("Nothing Selected");   
                combo.removeAllItems();
                combo.addItem("One");
            }
        }
    
          private class RTwoAction implements ActionListener
          {
            public void actionPerformed(ActionEvent ae)
            {
                label.setText("Nothing Selected");
                combo.removeAllItems();
                combo.addItem("Two");
            }
           }
            private class ROneAction implements ActionListener
            {
                public void actionPerformed(ActionEvent ae)
                {
                    combo.removeAllItems();
                    combo.addItem("Select");
                    combo.addItem("One");
                }
            }
    
            private class RTwoAction implements ActionListener
            {
                public void actionPerformed(ActionEvent ae)
                {
                    combo.removeAllItems();
                    combo.addItem("Select");
                    combo.addItem("Two");
                }
            }
    
            private class ComboAction implements ItemListener
            {
                public void itemStateChanged(ItemEvent ie)
                {
                    if(ie.getItem().equals("Two"))
                    {
                        label.setText("Two Selected");
                    } else if(ie.getItem().equals("One") ) {
                        label.setText("One Selected");
                    }
                }
            }