- Java code
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.Enumeration;
public class css extends JFrame{
int J=0;
JPanel p1 = new JPanel();
private JLabel l2 = new JLabel("Com:");
private JComboBox bo2 = new JComboBox();
public css(){
this.getContentPane().setLayout(null);
p1.setBounds(0, 0, 170, 186);
p1.setLayout(null);
p1.setBorder(new TitledBorder("Settings"));
l2.setBounds(5, 20, 60, 20);
bo2.setBounds(50,20, 110, 20);
p1.add(l2);
p1.add(bo2);
this.getContentPane().add(p1);
this.getPort();
bo2.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
boC_change(e);
}
});
bo2.setSelectedIndex(0);
this.setSize(340,310);
this.setVisible(true);
}
public void boC_change(ItemEvent e){
System.out.println(bo2.getSelectedItem().toString()); //用鼠标改变bo2的值的时候,会发现输出两行..这是什么,,我只点了一次
}
public void getPort(){
bo2.addItem("select");
bo2.addItem("selec1t");
} public static void main(String args[]){
css m = new css();
ImageIcon icon=new ImageIcon("sure.gif");
m.validate();
Image image=icon.getImage();
m.setIconImage(image);
m.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
//看上面的标注:我的JComboBox.事件,,,,,,,,怎么触发多次,我搞不明白
------解决方案--------------------
没搞过swing,兄弟实在搞不定,可以debug一下嘛~