📄 choicestudent.java
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class ChoiceStudent extends JDialog implements ActionListener {
JTextField id;
JLabel lable, lable1;
JButton yes, no,queryID;
JPanel p1, p2, p3,p4;
String message;
JTable table;
Object[][] a = new Object[2][5];
Object name[] = { "学号", "姓名", "性别", "得分", "参加考试时间" };
ChoiceStudent(JFrame f, String s, boolean b) {
super(f, s, b);
setLayout(new FlowLayout());
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
id = new JTextField(10);
lable1 = new JLabel("学号:");
yes = new JButton("查询全部");
queryID = new JButton("ID查询");
no = new JButton("取消");
yes.addActionListener(this);
no.addActionListener(this);
queryID.addActionListener(this);
table = new JTable(a, name);
table.setDragEnabled(true);
table.setEnabled(false);
p2.add(lable1);
p2.add(id);
p2.add(queryID);
p3.add(yes);
p3.add(no);
p4.add(new JScrollPane(table), BorderLayout.CENTER);
add(p1);
add(p2);
add(p3);
add(p4);
setSize(496, 510);
setResizable(true);
setLocation(250, 180);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Window w = e.getWindow();
w.setVisible(false);
w.dispose();
}
});
this.setVisible(true);
validate();
}
public void cleanColm(){
this.getContentPane().remove(3);
table = new JTable(a, name);
this.repaint();
table.setBackground(Color.BLACK);
table.setForeground(Color.GREEN);
add(new JScrollPane(table), BorderLayout.CENTER);
this.repaint();
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == yes) {
boolean flag = true;
if(flag){
List<Student> students = new ArrayList<Student>();
students = new StudentManager().queryStudentByAll();
int tmp = students.size();
a = new Object[tmp][5];
for (int i = 0; i < students.size(); i++) {
Student st = students.get(i);
a[i][0] = st.getId();
a[i][1] = st.getName();
a[i][2] = st.getSex();
a[i][3] = st.getScore();
if(st.getJointime()==null){
a[i][4] = "未参加考试";
}else{
a[i][4] = st.getJointime().toString();
}
}
this.getContentPane().remove(3);
table = new JTable(a, name);
this.repaint();
table.setBackground(Color.BLACK);
table.setForeground(Color.GREEN);
add(new JScrollPane(table), BorderLayout.CENTER);
this.repaint();
validate();
}else{
JOptionPane.showMessageDialog(this, "ID是无效字符!!", "消息",JOptionPane.WARNING_MESSAGE);
id.setText(null);
}
}
if(e.getSource()==queryID){
boolean flag = false;
String messageTmp = id.getText();
char[] atmp = messageTmp.toCharArray();
for (int i = 0; i < atmp.length; i++) {
if ('1' <= atmp[i] && atmp[i] <= '9') {
flag = true;
}
}
if(!flag){
JOptionPane.showMessageDialog(this, "ID是无效字符!!", "消息",JOptionPane.WARNING_MESSAGE);
this.id.setText(null);
return;
}
int id = Integer.parseInt(messageTmp);
this.repaint();
StudentManager stm = new StudentManager();
Student student = new Student();
student = stm.queryStudent(id);
cleanColm();
if(student!=null){
a[0][0] = student.getId();
a[0][1] = student.getName();
a[0][2] = student.getSex();
a[0][3] = student.getScore();
this.getContentPane().remove(3);
table = new JTable(a, name);
this.repaint();
table.setBackground(Color.BLACK);
table.setForeground(Color.GREEN);
add(new JScrollPane(table), BorderLayout.CENTER);
this.repaint();
validate();
}else{
JOptionPane.showMessageDialog(this, "没有相关学生信息!!", "消息",JOptionPane.WARNING_MESSAGE);
this.id.setText(null);
}
}
if (e.getSource() == no) {
this.setVisible(false);
this.dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -