📄 pane1.java
字号:
package pane_query;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import pane_dsdialog.Dsdialog;
import pane_dxdialog.Dxdialog;
import stu.Stu;
public class Pane1 extends JPanel implements ItemListener,ActionListener
{
/**
*
*/
private static final long serialVersionUID = -2901178447117935414L;
String com="Name";
/**
*
*/
String name,sex,eth,clas,bir,home;
int num;
public static int N=0;
JPanel pt=new JPanel();
JPanel p1=new JPanel();
JLabel l1=new JLabel("请选择查询方式:");
ButtonGroup gp=new ButtonGroup();
JRadioButton r1=new JRadioButton();
JRadioButton r2=new JRadioButton();
JRadioButton r3=new JRadioButton();
JRadioButton r4=new JRadioButton();
JPanel p2=new JPanel();
JLabel l2=new JLabel("请输入姓名:");
JTextField tf1=new JTextField(20);
JLabel l3=new JLabel("");
JButton bc=new JButton("查询");
JPanel p3=new JPanel();
JLabel l4=new JLabel("还没有进行查询。");
JScrollPane pc;
Object columnName[]={"序号","姓名","班级","学号","性别","名族","出生日期","籍贯"};
public static Object a[][]=new Object [(Stu.N1) * (Stu.N2)][8];
JTable table=new JTable(a,columnName);
JPanel pb=new JPanel();
JButton bt=new JButton("添加");
JButton bx=new JButton("修改");
JButton bs=new JButton("删除");
JButton be=new JButton("退出");
public Pane1()
{
setLayout(new BorderLayout());
r1.setText("按姓名");
r1.setSelected(true);
r2.setText("按班级");
r3.setText("按学号");
r4.setText("所有学生");
r4.setSelected(true);
r1.addItemListener(this);
r2.addItemListener(this);
r3.addItemListener(this);
r4.addItemListener(this);
gp.add(r1);
gp.add(r2);
gp.add(r3);
gp.add(r4);
pt.setLayout(new GridLayout(3,1,0,0));
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(l1);
p1.add(r1);
p1.add(r2);
p1.add(r3);
p1.add(r4);
pt.add(p1);//第一次失误时没有加上该处
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.add(l2);
p2.add(tf1);
p2.add(l3);
bc.addActionListener(this);
p2.add(bc);
p3.add(l4);
pt.add(p2);
pt.add(p3);
add(pt,"North");
table.setEnabled(false);
pc=new JScrollPane(table);
add(pc,"Center");
bt.addActionListener(this);//注册监听器
bx.addActionListener(this);
bs.addActionListener(this);
be.addActionListener(this);
bt.setEnabled(true);
bx.setEnabled(false);
bs.setEnabled(false);
be.setEnabled(true);
pb.add(bt);
pb.add(bx);
pb.add(bs);
pb.add(be);
add(pb,"South");
setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==r1)
{
tf1.setVisible(true);
l2.setText("请输入姓名:");
l3.setText("");
com ="Name=";
}
else if(e.getSource()==r2)
{
tf1.setVisible(true);
l2.setText("请输入班级:");
l3.setText("(格式为:1班)");
com="Class=";
}
else if(e.getSource()==r3)
{
tf1.setVisible(true);
l2.setText("请输入学号:");
l3.setText("(必须为数字)");
com="Num=";
}
else if(e.getSource()==r4)
{
tf1.setVisible(false);
l2.setText("");
l3.setText("");
com="所有";
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bc)//进行查询
{
for(int i=0;i<a.length;i++)
{
for(int j=0;j<8;j++)
{
a[i][j]="";
}
}
int n=0;
table.updateUI();
if((!tf1.getText().trim().equals(""))||com.equals("所有"))
{
Object t[][]=new Object [1][1];
String temp;
if(!com.equals("所有"))
{
//构建SQL语句
temp="SELECT * FROM 学生管理系统.dbo.学生基本信息 where " +
com+"'"+tf1.getText().trim()+"'";
}
else
{
temp ="SELECT * FROM 学生管理系统.dbo.学生基本信息";
}
try
{
Stu.pstmt = Stu.conn.prepareStatement(temp);
ResultSet rs =Stu.pstmt.executeQuery();
while (rs.next())
{
num=(int)rs.getLong("Num");
name=rs.getString("Name");
eth=rs.getString("Ethnic");
clas=rs.getString("Class");
bir=rs.getString("Birthday");
home=rs.getString("Home");
sex=rs.getString("Sex");
a[n][1]=name;
a[n][2]=clas;
a[n][3]=num;
a[n][4]=sex;
a[n][5]=eth;
a[n][6]=bir.substring(0,10);
a[n][7]=home;
n++;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(((Integer)a[j][3])>((Integer)a[j+1][3]))
{
t[0]=a[j];
a[j]=a[j+1];
a[j+1]=t[0];
}
}
}
for(int i=0;i<n;i++)
{
a[i][0]=i+1;
}
N=n;
//Stu.conn.close();
}catch(SQLException ee)
{
//System.out.println(ee.toString());
}
l4.setText("查询结果为:"
+com+tf1.getText().trim()+"的学生,按学号排列,共有"+n+"条记录!");
table.updateUI();
}
else{
l4.setText("请输入要查询的内容!");
}
if(n>0)
{
bx.setEnabled(true);
bs.setEnabled(true);
}
else
{
bx.setEnabled(false);
bs.setEnabled(false);
}
}
if(e.getSource()==bt)
{
//添加处理事件
Stu.car.show(Stu.p, "2");
}
if(e.getSource()==bx)
{
//修改按钮的事件处理
Dxdialog dx =new Dxdialog (Stu.st,"修改信息");
}
if(e.getSource()==bs)
{
//删除信息
Dsdialog da=new Dsdialog(Stu.st,"删除信息");
}
if(e.getSource()==be)
{
//退出按钮事件处理
Stu.car.show(Stu.p,"0");
}
}
public static void main(String args[])
{
new Pane1();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -