import java.awt.event.*;
import javax.swing.*;
public class TeachSearchInfo extends JPanel implements ActionListener {
// 创建提示标签数组
private JLabel[] jlArray = { new JLabel("学    号:"), new JLabel("密  码:") };
// 创建提示标签
private JLabel jl = new JLabel("请输入学号:");
// 创建用于输入学号的文本框控件
private JTextField jtf = new JTextField();
// 创建查询按钮
private JButton jb = new JButton("查询");
// 创建用于显示信息的标签数组
private JLabel[] jlArray2 = new JLabel[2];
// 声明GetStuInfo引用
private GetStuInfo getsi; // 构造器
public TeachSearchInfo() { // 初始化界面
this.initialFrame();
} // 初始化界面的方法
public void initialFrame() {// 将各控件添加到容器中
this.setLayout(null); jl.setBounds(30, 20, 250, 30);
this.add(jl);
jtf.setBounds(115, 20, 150, 30);
this.add(jtf);
jtf.addActionListener(this);
jb.setBounds(280, 20, 80, 30);
this.add(jb);
jb.addActionListener(this); } // 实现ActionListener接口中的actionPerformed()方法
public void actionPerformed(ActionEvent e) { if (e.getSource() == jb || e.getSource() == jtf) {// 按下查询按钮的处理代码
// 获得输入的学号
String stu_id = jtf.getText();
if (stu_id.equals("")) {// 学号为空
JOptionPane.showMessageDialog(this, "请输入学生学号", "错误",
JOptionPane.ERROR_MESSAGE);
return;
} else {// 调用GetStuInfo的方法获得指定学生的基本信息
String[] baseinfo = getsi.getBaseInfo(stu_id);
if (baseinfo[0] == null) {// 如果baseinfo[0]==null,则说明没有该学生
JOptionPane.showMessageDialog(this, "没有该学生", "错误",
JOptionPane.ERROR_MESSAGE);
return;
} else {// 更新界面显示的所有控件
this.removeAll();
for (int i = 0; i < 2; i++) {
jlArray2[i] = new JLabel(baseinfo[i]);
}
jl.setBounds(30, 20, 150, 30);
this.add(jl);
jtf.setBounds(115, 20, 150, 30);
this.add(jtf);
jb.setBounds(280, 20, 80, 30);
this.add(jb); jlArray[0].setBounds(30, 100, 100, 30);
this.add(jlArray[0]);
jlArray[1].setBounds(30, 150, 100, 30);
this.add(jlArray[1]); jlArray2[0].setBounds(130, 100, 150, 30);
this.add(jlArray2[0]);
jlArray2[1].setBounds(130, 150, 150, 30);
this.add(jlArray2[1]); this.repaint();
} }
} } public void setFocus() {
this.jtf.requestFocus(true);
}
}