1

请看下面的代码

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



}

这里我期待的是,

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

但是,这不是正在发生的事情。相反,只要您选择单选按钮,JLabel 文本就会更改!!!为什么是这样?请帮忙!

4

3 回答 3

1

这是因为ComboAction implements ItemListener. 你没有改变组合框的值吗?当您选择单选按钮的值时?

更新:

好吧,你的代码有点问题。它改变了标签的值,因为你有一个ItemListener。所以我采用了PopupMenuListener当列表变得不可见时会拍摄的。你想要的就很好。

代码:

     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.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
        public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
        }
        public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            jComboBox1PopupMenuWillBecomeInvisible(evt);
        }
        public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
        }
    });

        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 void jComboBox1PopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
   label.setText("selected");

}

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



       }
于 2013-01-14T18:53:27.193 回答
1

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

当您单击单选按钮时,然后在将该特定单选按钮文本添加到组合框中之前,您将删除所有项目,之后仅剩下的项目JComboBox是单击单选按钮后添加的项目,默认情况下选中该单选按钮,然后调用您JComboBoxitemStateChanged然后更改文本JLabel

于 2013-01-14T18:58:52.527 回答
1

在这里,您的需求只需稍作改动即可完成

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");
                }
            }
        }
于 2013-01-14T19:02:57.473 回答