问一个JComboBox兼听事件的问题!请帮忙!

来源:百度知道 编辑:UC知道 时间:2024/05/30 07:04:14
在程序中我对JComboBox 对象listBox进行了兼听。在程序运行的时候,只要JComboBox上事件一触发。以下程序中的四个if分支都同时执行了,我不知道是什么原因。
if(listBox.getSelectedIndex()==1)
{
}

if(listBox.getSelectedIndex()==2)
{
}

if(listBox.getSelectedIndex()==3)
{
}

if(listBox.getSelectedIndex()==4)
{
}

JComboBox listBox;
lisBox.addActionListener(this);
public void actionPerformed(ActionEvent e) {

if(listBox.getSelectedIndex()==1)
{
String sql=null;
sql="select * from Book";
Vector vv=new Vector();
InquiryDAO dao=new InquiryDAO();
vv=dao.inquiry(sql);
Vector vc=new Vector();
vc.add("BookId");
vc.add("BookName");
vc.add("BookPrice");
JTable table=new JTable(vv,vc);
addTable(table);
}
if(listBox.getSelectedIndex()==2)
{
String sql=null;
sql="select BorrowMessage.* fr

看不明白...把代码贴全点

lisBox.addActionListener(this); =>listBox.addActionListener(this); //少了t;

listBox的getSelectedIndex(索引)是从0 开始的,即:
if (listBox.getSelectedIndex() == 0) {
System.out.print("1");
}
if (listBox.getSelectedIndex() == 1) {
System.out.print("2");
}
if (listBox.getSelectedIndex() == 2)
{
System.out.print("3");
}
if (listBox.getSelectedIndex() == 3)
{
System.out.print("4");
}

你用getSelectedItem测试一下,看是否有选中及验证监听是否正常