刚开始学eclipse的使用,下载了一个VE写了个简单的GUI。
   但是触发 下拉菜单和列表 的事件的时候,不知道为什么执行两次。。按钮的事件正常,谁能给我解释下。
   我把代码贴出来import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JButton;public class MainPanel extends JPanel { private JLabel TypeLabel = null;
private JComboBox TypeComboBox = null;
private JScrollPane ListScrollPane = null;
private JList jList = null;
private JLabel EditLabel = null;
private JTextArea EditTextArea = null;
private JButton SaveButton = null;
private JButton CancelButton = null; /**
 * This is the default constructor
 */
public MainPanel() {
super();
initialize();
} /**
 * This method initializes this
 * 
 * @return void
 */
private void initialize() {
EditLabel = new JLabel();
EditLabel.setBounds(new java.awt.Rectangle(168,23,82,29));
EditLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
EditLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
EditLabel.setText("文件编辑区");
TypeLabel = new JLabel();
TypeLabel.setBounds(new java.awt.Rectangle(1,22,65,30));
TypeLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
TypeLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
TypeLabel.setText("选择类型");
this.setLayout(null);
this.setSize(605, 345);
this.add(TypeLabel, null);
this.add(getTypeComboBox(), null);
this.add(getListScrollPane(), null);
this.add(EditLabel, null);
this.add(getEditTextArea(), null);
this.add(getSaveButton(), null);
this.add(getCancelButton(), null);
this.setVisible(true);
} /**
 * This method initializes TypeComboBox
 * 
 * @return javax.swing.JComboBox
 */
private JComboBox getTypeComboBox() {
if (TypeComboBox == null) {
TypeComboBox = new JComboBox();
TypeComboBox.setBounds(new java.awt.Rectangle(67,22,100,30));
TypeComboBox.addItem("选项一");
TypeComboBox.addItem("选项二");
TypeComboBox.addItem("选项三");
TypeComboBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
System.out.println("下拉列表事件!"); // TODO Auto-generated Event stub itemStateChanged()
}
});
}
return TypeComboBox;
} /**
 * This method initializes ListScrollPane
 * 
 * @return javax.swing.JScrollPane
 */
private JScrollPane getListScrollPane() {
if (ListScrollPane == null) {
ListScrollPane = new JScrollPane(jList);
ListScrollPane.setBounds(new java.awt.Rectangle(2,52,165,289));
ListScrollPane.setViewportView(getJList());
}
return ListScrollPane;
} /**
 * This method initializes jList
 * 
 * @return javax.swing.JList
 */
private JList getJList() {
if (jList == null) {
jList = new JList();
Object d[]=new String[30];
for(int i=0;i<30;i++){
d[i]="列项 "+i;
}
    jList.setListData(d);
jList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent e) {
System.out.println("列表事件!"); // TODO Auto-generated Event stub valueChanged()
}
});
}
return jList;
} /**
 * This method initializes EditTextArea
 * 
 * @return javax.swing.JTextArea
 */
private JTextArea getEditTextArea() {
if (EditTextArea == null) {
EditTextArea = new JTextArea();
EditTextArea.setBounds(new java.awt.Rectangle(170,54,430,232));
}
return EditTextArea;
} /**
 * This method initializes SaveButton
 * 
 * @return javax.swing.JButton
 */
private JButton getSaveButton() {
if (SaveButton == null) {
SaveButton = new JButton();
SaveButton.setBounds(new java.awt.Rectangle(297,300,78,30));
SaveButton.setText("保存");
SaveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
EditTextArea.append("添加一条信息!\n");
System.out.println("按钮事件!"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return SaveButton;
} /**
 * This method initializes CancelButton
 * 
 * @return javax.swing.JButton
 */
private JButton getCancelButton() {
if (CancelButton == null) {
CancelButton = new JButton();
CancelButton.setBounds(new java.awt.Rectangle(397,300,76,30));
CancelButton.setText("取消");
}
return CancelButton;
}}  //  @jve:decl-index=0:visual-constraint="10,10"测试代码import javax.swing.JFrame;
import java.awt.FlowLayout;
public class Test { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
        MainPanel panel =new MainPanel();
        JFrame jFrame=null;
        jFrame = new JFrame();
        jFrame.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
jFrame.setSize(new java.awt.Dimension(615, 375));
jFrame.setTitle("XML文件维护器");
jFrame.setContentPane(panel);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}

解决方案 »

  1.   

    两次的状态不太一样,你把e.getValueIsAdjusting()打出来看看就知道了
    原因不详,以前没用过
      

  2.   

    我记得是 
    1,DESELECTED 当前已经选中的ITEM
    2,SELECTED 新的ITEM
      

  3.   

    用IDE画出来的GUI代码都是很难管理的。学学自己写吧
      

  4.   

    我记得是 
    1,DESELECTED 当前已经选中的ITEM
    2,SELECTED 新的ITEM
    ///////////////////修改一下你的程序
    TypeComboBox.addItemListener(new java.awt.event.ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent e) {
    System.out.println("下拉列表事件!"); // TODO Auto-generated Event stub itemStateChanged()
    System.out.println(e);
    }
    });
    ///////////////////修改一下你的程序
    得到下面的结果
    public void itemStateChanged(java.awt.event.ItemEvent e) {
    System.out.println("下拉列表事件!"); // TODO Auto-generated Event stub itemStateChanged()
    }
    });下拉列表事件!
    java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=选项二,stateChange=DESELECTED] on javax.swing.JComboBox[,67,22,100x30,layout=javax.swing.plaf.metal.MetalComboBoxUI$MetalComboBoxLayoutManager,alignmentX=null,alignmentY=null,border=,flags=328,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=选项二]
    下拉列表事件!
    java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=选项三,stateChange=SELECTED] on javax.swing.JComboBox[,67,22,100x30,layout=javax.swing.plaf.metal.MetalComboBoxUI$MetalComboBoxLayoutManager,alignmentX=null,alignmentY=null,border=,flags=328,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=选项三]