下面是源代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ComboBoxTest extends JFrame{
private JLabel sqlLabel = new JLabel("SQL:");
private JComboBox sqlComboBox = new JComboBox();
public ComboBoxTest() {
sqlComboBox.setEditable(true);
sqlComboBox.addItem("");
sqlComboBox.setBackground(Color.WHITE);
sqlComboBox.setPreferredSize(new Dimension(500,25));
sqlComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED){
if(sqlComboBox.getSelectedIndex()==-1){
String currentSQL=(String)event.getItem();
System.out.println("currentSQL : "+currentSQL);
sqlComboBox.addItem(currentSQL);
}
}
}
});
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(sqlLabel);
cp.add(sqlComboBox);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
public static void main(String[] args){
new ComboBoxTest();
}
}我得意图是让用户在JComboBox里写sql语句,当用户按回车时发送sql语句,并且把发送过的sql语句
用addItem()添加到JComboBox里,但是现在有一个问题:
当我连续敲击一次以上的回车时,为什么只能触发一次itemStateChanged,(我希望用户每次按回车都能触发一次itemStateChanged)
还有当选择了JComboBox里的被记录的sql后,再按回车却无法触发itemStateChanged;
谁能帮我解决这个问题
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ComboBoxTest extends JFrame{
private JLabel sqlLabel = new JLabel("SQL:");
private JComboBox sqlComboBox = new JComboBox();
public ComboBoxTest() {
sqlComboBox.setEditable(true);
sqlComboBox.addItem("");
sqlComboBox.setBackground(Color.WHITE);
sqlComboBox.setPreferredSize(new Dimension(500,25));
sqlComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED){
if(sqlComboBox.getSelectedIndex()==-1){
String currentSQL=(String)event.getItem();
System.out.println("currentSQL : "+currentSQL);
sqlComboBox.addItem(currentSQL);
}
}
}
});
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(sqlLabel);
cp.add(sqlComboBox);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
public static void main(String[] args){
new ComboBoxTest();
}
}我得意图是让用户在JComboBox里写sql语句,当用户按回车时发送sql语句,并且把发送过的sql语句
用addItem()添加到JComboBox里,但是现在有一个问题:
当我连续敲击一次以上的回车时,为什么只能触发一次itemStateChanged,(我希望用户每次按回车都能触发一次itemStateChanged)
还有当选择了JComboBox里的被记录的sql后,再按回车却无法触发itemStateChanged;
谁能帮我解决这个问题
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货