studenttable.java
来自「这个是我二年级写的一个教务管理系统 大家可以下来」· Java 代码 · 共 159 行
JAVA
159 行
package caoyu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.table.*;
import java.util.*;
class StudentTable extends JFrame implements ActionListener{
public JRadioButton name = null;
public JRadioButton sno = null;
public JRadioButton classno = new JRadioButton("班级号码");
public JTextField jt = null;
public JButton btn = null;
public JTable table = null;
DefaultTableModel dtm;
public StudentTable()
{
this.initFrame();
this.setTitle("学生信息系统");
setSize(500,300);
setVisible(true);
}
public void initFrame()
{
Container container = this.getContentPane();
String[][] p = new String[100][7];
String[] n = {"sname","sno","ssex","classno","birthday","address"};
table = new JTable(p,n);
JLayeredPane jlp=new JLayeredPane();
jlp.setLayout(new GridLayout(1,5));//生成布局
JScrollPane s = new JScrollPane(table);
container.add(s, BorderLayout.CENTER);
name = new JRadioButton( "姓名" );
sno = new JRadioButton( "学号");
btn = new JButton( "查询" );
jlp.add( name );
jlp.add( sno);
jlp.add( classno);
ButtonGroup buttongroup=new ButtonGroup();
buttongroup.add(name);
buttongroup.add(sno);
buttongroup.add(classno);
jlp.add( btn );
jt = new JTextField( 10 );
jlp.add( jt );
container.add(jlp, BorderLayout.NORTH);
btn.addActionListener(this);}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn&&sno.isSelected())
{try {
DBOperatorStudent op = new DBOperatorStudent();
ResultSet rs= op.queryStudent(jt.getText().trim());
for(int i=0;rs.next();i++)
{
table.setValueAt(rs.getString(1),i,0);
table.setValueAt(rs.getString(2),i,1);
table.setValueAt(rs.getString(3),i,2);
table.setValueAt(rs.getString(4),i,3);
table.setValueAt(rs.getString(5),i,4);
table.setValueAt(rs.getString(6),i,5);
//JOptionPane.showMessageDialog(this,"cccc");
}
//if(table.getValueAt(0,0).toString().equals(null))
// JOptionPane.showMessageDialog(this,"cccc");
// System.out.print(table.getValueAt(0,0));
//System.out.print(table.getValueAt(0,0));
rs.close();
op.conn.close();
}
catch(Exception ex) {
ex.printStackTrace();
}
}//if
else if(e.getSource()==btn&&name.isSelected())
//JOptionPane.showMessageDialog(this,"name not null");
{try {
DBOperatorStudent op = new DBOperatorStudent();
ResultSet rs= op.queryStudent1(jt.getText().trim());
for(int i=0;rs.next();i++)
{
table.setValueAt(rs.getString(1),i,0);
table.setValueAt(rs.getString(2),i,1);
table.setValueAt(rs.getString(3),i,2);
table.setValueAt(rs.getString(4),i,3);
table.setValueAt(rs.getString(5),i,4);
table.setValueAt(rs.getString(6),i,5);
}
rs.close();
op.conn.close();
}
catch(Exception ex) {
ex.printStackTrace();
}
}//if
else if(e.getSource()==btn&&classno.isSelected())
{try {
DBOperatorStudent op = new DBOperatorStudent();
ResultSet rs= op.queryStudent2(jt.getText().trim());
for(int i=0;rs.next();i++)
{
table.setValueAt(rs.getString(1),i,0);
table.setValueAt(rs.getString(2),i,1);
table.setValueAt(rs.getString(3),i,2);
table.setValueAt(rs.getString(4),i,3);
table.setValueAt(rs.getString(5),i,4);
table.setValueAt(rs.getString(6),i,5);
}
rs.close();
op.conn.close();
}
catch(Exception ex) {
ex.printStackTrace();
}
}//if
else JOptionPane.showMessageDialog(this,"请选择一种查询方式");
}
public static void main(String args[])
{
StudentTable a=new StudentTable();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?