Java中JTabel的问题! 5
为什么单击列表框后会执行两次下列代码?System.out.println(vector.size());会输出两次packageGUI;importjava.awt.B...
为什么单击列表框后会执行两次下列代码? System.out.println(vector.size());会输出两次
package GUI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import Usebean.Person;
import DBS.TeaBase;
public class NumTea extends JFrame{
private TeaBase db;
private Person person;
private Vector vector;
private JList jlCourse=new JList(new String[]{"英语","编译原理","数学分析1","数学分析2","物理1"
,"离散数学","计算机组成原理","操作系统","java","unix"});
private String[] columnNames={"学号","姓名","班级","入学年份","辅导员"};
private DefaultTableModel jtbm=new DefaultTableModel(columnNames,0);
private JTable jtb=new JTable(jtbm);
public NumTea(Person person){
this.person=person;
this.setVisible(true);
this.setSize(600,500);
initGUI();
}
public void initGUI() {
// TODO Auto-generated method stub
this.add(new JScrollPane(jlCourse),BorderLayout.WEST);
this.add(new JScrollPane(jtb));
jtb.setRowHeight(30);
jlCourse.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jlCourse.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent arg0) {
// TODO Auto-generated method stub
for(int i=0;i<jtbm.getRowCount();i++){
jtbm.removeRow(i);
}
jtb.revalidate();
db=new TeaBase();
Object selectedCourse=jlCourse.getSelectedValue();
String selectedCourseString=(String)selectedCourse;
vector=db.getNum(selectedCourseString);
System.out.println(vector.size());
for(int i=0;i<vector.size();i++)
jtbm.addRow((Vector) vector.elementAt(i));
}
});
}
} 展开
package GUI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import Usebean.Person;
import DBS.TeaBase;
public class NumTea extends JFrame{
private TeaBase db;
private Person person;
private Vector vector;
private JList jlCourse=new JList(new String[]{"英语","编译原理","数学分析1","数学分析2","物理1"
,"离散数学","计算机组成原理","操作系统","java","unix"});
private String[] columnNames={"学号","姓名","班级","入学年份","辅导员"};
private DefaultTableModel jtbm=new DefaultTableModel(columnNames,0);
private JTable jtb=new JTable(jtbm);
public NumTea(Person person){
this.person=person;
this.setVisible(true);
this.setSize(600,500);
initGUI();
}
public void initGUI() {
// TODO Auto-generated method stub
this.add(new JScrollPane(jlCourse),BorderLayout.WEST);
this.add(new JScrollPane(jtb));
jtb.setRowHeight(30);
jlCourse.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jlCourse.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent arg0) {
// TODO Auto-generated method stub
for(int i=0;i<jtbm.getRowCount();i++){
jtbm.removeRow(i);
}
jtb.revalidate();
db=new TeaBase();
Object selectedCourse=jlCourse.getSelectedValue();
String selectedCourseString=(String)selectedCourse;
vector=db.getNum(selectedCourseString);
System.out.println(vector.size());
for(int i=0;i<vector.size();i++)
jtbm.addRow((Vector) vector.elementAt(i));
}
});
}
} 展开
展开全部
那是因为没有加上判断。需要这样。
if(arg0.getValueIsAdjusting()) ;//鼠标弹起事件
else{ //鼠标击中事件
//内容
for(int i=0;i<jtbm.getRowCount();i++){
jtbm.removeRow(i);
}
jtb.revalidate();
db=new TeaBase();
Object selectedCourse=jlCourse.getSelectedValue();
String selectedCourseString=(String)selectedCourse;
vector=db.getNum(selectedCourseString);
System.out.println(vector.size());
for(int i=0;i<vector.size();i++)
jtbm.addRow((Vector) vector.elementAt(i));
}
}
if(arg0.getValueIsAdjusting()) ;//鼠标弹起事件
else{ //鼠标击中事件
//内容
for(int i=0;i<jtbm.getRowCount();i++){
jtbm.removeRow(i);
}
jtb.revalidate();
db=new TeaBase();
Object selectedCourse=jlCourse.getSelectedValue();
String selectedCourseString=(String)selectedCourse;
vector=db.getNum(selectedCourseString);
System.out.println(vector.size());
for(int i=0;i<vector.size();i++)
jtbm.addRow((Vector) vector.elementAt(i));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询