关于java中JTable,后一次点击总是受到前一次鼠标事件结果的影响,请大神们帮忙看看,谢谢了,很急的 5
利用表格显示数据库中读出的数据publicclassNumTeaextendsJFrame{privateTeaBasedb=newTeaBase();privatePe...
利用表格显示数据库中读出的数据
public class NumTea extends JFrame{
private TeaBase db=new TeaBase();
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() {
this.add(new JScrollPane(jlCourse),BorderLayout.WEST);
this.add(new JScrollPane(jtb));
jtb.setRowHeight(30);
jlCourse.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jlCourse.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
if(e.getValueIsAdjusting())
;
else{
for(int i=0;i<jtbm.getRowCount();i++)
jtbm.removeRow(i);
jtb.revalidate();
Object selectedCourse=jlCourse.getSelectedValue();
String selectedCourseString=(String)selectedCourse;
vector=db.getNum(selectedCourseString);
for(int i=0;i<vector.size();i++)
jtbm.addRow((Vector) vector.elementAt(i));
}
}
});
}
}
/*其中rs为ResultSet类的实例,且数据化的初始化都在Database中进行*/
public class TeaBase extends Database{
private Vector vector=new Vector();
public TeaBase(){
super();
}
public TeaBase(Person teacher){
super();
}
public Vector getNum(String selectedCourseString) {
String SQL="select distinct student.stuNum,stuNam,stu_class.claNum,cla_grade.graNum,assGra "+
"from grade,stu_class,stu_alcourse,cla_grade,course,student "+
"where course.couNam=\'"+selectedCourseString+"\' and "+
"course.couNum=stu_alcourse.couNum and "+
"stu_alcourse.stuNum=student.stuNum and "+
"student.stuNum=stu_class.stuNum and "+
"stu_class.claNum=cla_grade.claNum and "+
"cla_grade.graNum=grade.graNum;";
rs=this.select(SQL);
Num_Vector(rs);
return vector;
}
public void Num_Vector(ResultSet rs) {
try {
while(rs.next()){
Vector<String> vector1=new Vector<String>();
vector1.add(rs.getString(1));
vector1.add(rs.getString(2));
vector1.add(rs.getString(3));
vector1.add(rs.getString(4));
vector1.add(rs.getString(5));
vector.add(vector1);
vector1=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} 展开
public class NumTea extends JFrame{
private TeaBase db=new TeaBase();
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() {
this.add(new JScrollPane(jlCourse),BorderLayout.WEST);
this.add(new JScrollPane(jtb));
jtb.setRowHeight(30);
jlCourse.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jlCourse.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
if(e.getValueIsAdjusting())
;
else{
for(int i=0;i<jtbm.getRowCount();i++)
jtbm.removeRow(i);
jtb.revalidate();
Object selectedCourse=jlCourse.getSelectedValue();
String selectedCourseString=(String)selectedCourse;
vector=db.getNum(selectedCourseString);
for(int i=0;i<vector.size();i++)
jtbm.addRow((Vector) vector.elementAt(i));
}
}
});
}
}
/*其中rs为ResultSet类的实例,且数据化的初始化都在Database中进行*/
public class TeaBase extends Database{
private Vector vector=new Vector();
public TeaBase(){
super();
}
public TeaBase(Person teacher){
super();
}
public Vector getNum(String selectedCourseString) {
String SQL="select distinct student.stuNum,stuNam,stu_class.claNum,cla_grade.graNum,assGra "+
"from grade,stu_class,stu_alcourse,cla_grade,course,student "+
"where course.couNam=\'"+selectedCourseString+"\' and "+
"course.couNum=stu_alcourse.couNum and "+
"stu_alcourse.stuNum=student.stuNum and "+
"student.stuNum=stu_class.stuNum and "+
"stu_class.claNum=cla_grade.claNum and "+
"cla_grade.graNum=grade.graNum;";
rs=this.select(SQL);
Num_Vector(rs);
return vector;
}
public void Num_Vector(ResultSet rs) {
try {
while(rs.next()){
Vector<String> vector1=new Vector<String>();
vector1.add(rs.getString(1));
vector1.add(rs.getString(2));
vector1.add(rs.getString(3));
vector1.add(rs.getString(4));
vector1.add(rs.getString(5));
vector.add(vector1);
vector1=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} 展开
2个回答
展开全部
先定义一个全局的静态变量 static bool ca nClick=ture; 1.鼠标右键点击的事件前加个if判断: if(canClick){ 鼠标点击事件事件 }
2.然后添加键盘事件implements KeyListen er 在重写的方法的keyPressed中加入: if(e.getKeyCode()==KeyEvent.VK_CONTR OL){ canClick=false; } 在重新的方法的keyReleased中加入: if(e.getKeyCode()==KeyEvent.VK_CONTR OL){ canClick=true;
2.然后添加键盘事件implements KeyListen er 在重写的方法的keyPressed中加入: if(e.getKeyCode()==KeyEvent.VK_CONTR OL){ canClick=false; } 在重新的方法的keyReleased中加入: if(e.getKeyCode()==KeyEvent.VK_CONTR OL){ canClick=true;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询