请看下面的代码
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();
}
}
我期待的是,
- 选择一个单选按钮。它将替换组合框中的value。
- 从组合框中选择一个值。现在JLabel文本将被设置为"Selected"
但是,事实并非如此。相反,只要您选择一个单选按钮,JLabel文本就会被更改!!为什么会这样?请帮助!
这是因为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();
}
}
这是因为您的单选按钮的动作侦听器中的这段代码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");
}
}
}