查询界面,因为还不会数据库,嘤嘤嘤~就只能先这样写了
package StudentMajor;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;
import javax.swing.JButton;
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 SearchStudents {
private JFrame jf;
private JPanel jp,jp2,jp3;
private JTextField jtf;
private JButton jb,jb2,jb3;
private JLabel jl;
private JTable jtable;
private JScrollPane jsp1,jsp2;
private int countAStudentShow,countAllStudentShow;
//用来计数查找名字显示或者不显示此处长度应该以学生的数量为准
private Object[][] tableData = new Object[getFilelength()][];
// // 定义一维数据作为列标题
private final Object[] columnTitle = {"姓名", "学号" , "数学","语文","英语","总分"};
SearchStudents(){
}
//返回
public static int getFilelength() {
return StudentTools.getStudentsFromFile().toArray().length;
}
public void setTable() {
// 定义二维数组作为表格数据
for(int i=0;i< getFilelength();i++) {
Object[] obj = new Object[]{"","","","","",""};
tableData[i]=obj;
}
}
public JScrollPane jspOfAllStudents() {
//初始化表格的行列数
setTable();
//给表格中填入数据
Set<StudentUser> set = StudentTools.getStudentsFromFile();
if(set==null)
return null;
Object[] obj = set.toArray();
// System.out.println(obj.length);
for(int i=0;i<obj.length;i++) {
StudentUser s = (StudentUser)obj[i];
// System.out.println(s.getName());
tableData[i][0] = s.getName();
tableData[i][1] = s.getNum();
tableData[i][2] = s.getScore1();
tableData[i][3] = s.getScore2();
tableData[i][4] = s.getScore3();
tableData[i][5] = s.getSum();
}
jtable = new JTable(tableData,columnTitle);
jtable.enable(false);//让这个表格不可编辑
return new JScrollPane(jtable);
}
public JScrollPane jspOfAStudent(StudentUser s) {
if(s==null)
return null;
// 定义二维数组作为表格数据
Object[][] tableData =
{
new Object[]{"","","","","",""},
};
// 定义一维数据作为列标题
Object[] columnTitle = {"姓名", "学号" , "数学","语文","英语","总分"};
//给表格中填入数据
tableData[0][0] = s.getName();
tableData[0][1] = s.getNum();
tableData[0][2] = s.getScore1();
tableData[0][3] = s.getScore2();
tableData[0][4] = s.getScore3();
tableData[0][5] = s.getSum();
jtable = new JTable(tableData,columnTitle);
jtable.enable(false);//让这个表格不可编辑
return new JScrollPane(jtable);
}
public void init() {
jf = new JFrame("学生管理系统");
jf.setSize(300, 250);//窗体大小
jf.setLocationRelativeTo(null);//居中显示
jf.setLayout(new FlowLayout());//设置布局
jf.setResizable(false); //设置窗口不能改变大小
jp = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jl = new JLabel("请输入学号:");
jb = new JButton("查询");
jb2 = new JButton("查询全部");
jb3 = new JButton("返回");
jtf = new JTextField(10);
jp.add(jl);
jp.add(jtf);
jp.add(jb);
jp2.add(jb2);
jp3.add(jb3);
jf.add(jp);
jf.add(jp2);
jf.add(jp3);
//jf.add(jsp());
//jf.pack();
myEvent();
jf.setVisible(true);
}
public void myEvent() {
//(1)窗体关闭
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//(2)监听---确定
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(countAStudentShow==0) {
StudentUser stu = StudentTools.showAStudentToPanel(jtf.getText());
if(stu==null)
JOptionPane.showMessageDialog(null, "没有该学号的学生!", "提示信息", JOptionPane.WARNING_MESSAGE);
else{
jsp1 = jspOfAStudent(stu);
jf.add(jsp1);
jf.pack();
countAStudentShow++;
}
}else {
jf.remove(jsp1);
StudentUser stu = StudentTools.showAStudentToPanel(jtf.getText());
if(stu==null)
JOptionPane.showMessageDialog(null, "没有该学号的学生!", "提示信息", JOptionPane.WARNING_MESSAGE);
else {
jsp1 = jspOfAStudent(stu);
jf.add(jsp1);
jf.pack();
}
}
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(countAllStudentShow==0) {
if(jspOfAllStudents()==null)
JOptionPane.showMessageDialog(null, "还没有学生!", "提示信息", JOptionPane.WARNING_MESSAGE);
else{
jsp2 = jspOfAllStudents();
jf.add(jsp2);
jf.pack();
countAllStudentShow++;
}
}else {
jf.remove(jsp2);
jsp2 = jspOfAllStudents();
jf.add(jsp2);
jf.pack();
}
}
});
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new TeacherUser().init();
jf.dispose();
}
});
}
}