JavaStudentsInfoQuery(简单的学生信息查询系统)
Posted 一身千寻瀑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaStudentsInfoQuery(简单的学生信息查询系统)相关的知识,希望对你有一定的参考价值。
周末作业,写的草草,BUG很多,直接贴代码。
基于文本文档的,不是数据库。
功能极其有限,也没有用WindowBuilder。
教师查询窗口
package wh.one;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import wh.io.ReadStuFromFile;
public class FirstWindow implements ActionListener
JTextField jxt = null;
JTextArea jta = null;
JFrame f = null;
JComboBox<String> se = new JComboBox<String>();
public void showMyWindow()
JButton blog = new JButton("select");
JButton bclear = new JButton("clear");
JLabel jl = new JLabel("Select conditon:");
JLabel jl2 = new JLabel("Input:");
se.addItem("choose");
se.addItem("student number");
se.addItem("student name");
jxt = new JTextField("No support for fuzzy query",20);
jta = new JTextArea(10,20);
f = new JFrame("Select");
JPanel p = new JPanel();
p.add(jl);
p.add(se);
p.add(jl2);
p.add(jxt);
p.add(blog);
p.add(bclear);
p.add(jta);
f.add(p,BorderLayout.CENTER);
blog.addActionListener(this);
bclear.addActionListener(this);
jxt.setEditable(false);
se.addActionListener(new ActionListener()
public void actionPerformed(final ActionEvent e)
String temp = se.getSelectedItem().toString();
if(temp.equals("student number")) //Only numbers can input in the jxt
jxt.setEditable(true);
jxt.addKeyListener(new KeyAdapter()
public void keyTyped(KeyEvent e)
char keyChar = e.getKeyChar();
if(!( (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) ||
keyChar == (22&0x1f) /*Ctrl+V(ASCLL CODE) used to paste*/ ||
keyChar == KeyEvent.VK_BACK_SPACE))
JOptionPane.showMessageDialog(null,"Only NUMBERS can be input in","Warning",JOptionPane.ERROR_MESSAGE);
e.consume();
jxt.setText("");
);
else if(temp.equals("student name")) //Only Chinese can input in the jxt
jxt.setEditable(true);
jxt.addKeyListener(new KeyAdapter()
public void keyTyped(KeyEvent e)
char keyChar = e.getKeyChar();
if( !( ( (keyChar >= '\\u4e00' && keyChar <= '\\u9fa5') ||
(keyChar >= 'a' && keyChar <= 'z') ||
(keyChar >= 'A' && keyChar <= 'Z') ||
keyChar == KeyEvent.VK_BACK_SPACE ) ) )
JOptionPane.showMessageDialog(null,"Only CHINESE or ENGLISH can be input in","Warning",JOptionPane.ERROR_MESSAGE);
e.consume();
jxt.setText("");
);
else
jxt.setEditable(false);
);
f.setVisible(true);
f.setSize(700,400);
f.setLocation(200, 200);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
@Override
public void actionPerformed(ActionEvent e)
if(e.getActionCommand().equals("select")) //select button
String getstr = jxt.getText().trim();
try
jta.setText("");
ReadStuFromFile rsf = new ReadStuFromFile();
ArrayList<String> stu = rsf.getStuFromFile(getstr);
if(stu.isEmpty())
JOptionPane.showMessageDialog(null,"Not Valid","Input Error",JOptionPane.WARNING_MESSAGE);
else
f.setVisible(false);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
FifthWindow fw = new FifthWindow();
fw.showMyWindow(stu);
catch(NumberFormatException ex)
JOptionPane.showMessageDialog(null,"Not Valid","Error",JOptionPane.WARNING_MESSAGE);
else if(e.getActionCommand().equals("clear")) //clear button
jxt.setText("");
jta.setText("");
学生查看自身信息的窗口
package wh.one;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
public class FifthWindow
public void showMyWindow(String stu)
JButton quit = new JButton("quit");
JTextArea jta = new JTextArea();
JPanel p = new JPanel();
p.add(jta);
p.add(quit);
JFrame f = new JFrame("Student InfoWindow");
f.add(p,BorderLayout.CENTER);
f.setVisible(true);
f.setSize(400,300);
f.setLocation(250,250);
jta.setText(stu);
quit.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
// TODO Auto-generated method stub
if(e.getActionCommand().equals("quit"))
f.setVisible(false);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ThirdWindow tw = new ThirdWindow();
tw.showMyWindow();
);
public void showMyWindow(ArrayList<String> stu)
// TODO Auto-generated method stub
JFrame f = new JFrame("Student InfoWindow");
JButton quit = new JButton("quit");
JTextArea jta = new JTextArea();
JPanel p = new JPanel();
p.add(jta);
p.add(quit);
f.add(p,BorderLayout.CENTER);
f.setVisible(true);
f.setSize(400,300);
f.setLocation(250,250);
int i = 0;
while(i < stu.size())
String read = stu.get(i++);
jta.append(read +"\\n\\r");
quit.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
// TODO Auto-generated method stub
if(e.getActionCommand().equals("quit"))
f.setVisible(false);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
FirstWindow fw = new FirstWindow();
fw.showMyWindow();
);
教师登陆窗口
package wh.one;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class SecondWindow //Teachers login window
JTextField jxt1 = null;
JTextArea jta1 = null;
JPasswordField jxt2 = null;
JTextArea jta2 = null;
public void showMyWindow()
JButton blog = new JButton("login");
JButton bclear = new JButton("return");
JLabel jl1 = new JLabel("Job account: ");
JLabel jl2 = new JLabel(" Password: ");
jxt1 = new JTextField(15);
jxt2 = new JPasswordField(15);
JFrame f = new JFrame("Teachers");
JPanel p = new JPanel();
p.add(jl1);
p.add(jxt1);
p.add(jl2);
p.add(jxt2);
p.add(blog);
p.add(bclear);
f.add(p,BorderLayout.CENTER);
f.setVisible(true);
f.setSize(300,300);
f.setLocation(250,250);
blog.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
// TODO Auto-generated method stub
String username = jxt1.getText().trim();
String pwd = String.valueOf(jxt2.getPassword());
if(username.equals("abc")&&pwd.equals("123"))
f.setVisible(false);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
FirstWindow fw = new FirstWindow();
fw.showMyWindow();
else
JOptionPane.showMessageDialog(null,"No User or Error Password","Error",JOptionPane.ERROR_MESSAGE);
);
bclear.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
// TODO Auto-generated method stub
f.setVisible(false);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
FourthWindow fw = new FourthWindow();
fw.showMyWindow();
);
学生登陆窗口
package wh.one;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import wh.io.ReadStuFromFile;
public class ThirdWindow //Student login window
JTextField jxt1 = null;
JPasswordField jxt2 = null;
public void showMyWindow()
JButton blog = new JButton("login");
JButton bclear = new JButton("return");
JLabel jl1 = new JLabel("Student number:");
JLabel jl2 = new JLabel(" Password: ");
JTextArea jta = new JTextArea("Default password is 111111");
jxt1 = new JTextField(15);
jxt2 = new JPasswordField(15);
JFrame f = new JFrame("Students");
JPanel p = new JPanel();
p.add(jl1);
p.add(jxt1);
p.add(jl2);
p.add(jxt2);
p.add(jta);
p.add(blog);
p.add(bclear);
f.add(p,BorderLayout.CENTER);
f.setVisible(true);
f.setSize(225,250);
f.setLocation(250,250);
blog.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
// TODO Auto-generated method stub
String username = jxt1.getText().trim();
String pwd = String.valueOf(jxt2.getPassword());
ReadStuFromFile rsf = new ReadStuFromFile();
ArrayList<String> stu = rsf.getStuFromFile(username);
if( !stu.isEmpty() && pwd.equals("111111") && username.length() == 15)
f.setVisible(false);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
FifthWindow fw = new FifthWindow();
fw.showMyWindow(stu.get(0));
else
JOptionPane.showMessageDialog(null,"No User Found or Error Password","Error",JOptionPane.ERROR_MESSAGE);
);
bclear.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
// TODO Auto-generated method stub
f.setVisible(false);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
FourthWindow fw = new FourthWindow();
fw.showMyWindow();
);
角色(教师或学生)选择窗口,不同角色的权限不一样(教师可查看所有学生信息,学生仅可查看自身信息)
package wh.one;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class FourthWindow
public void showMyWindow()
JButton tr = new JButton("Teacher Role");
JButton sr = new JButton("Student Role");
JFrame f = new JFrame("Login");
JPanel p = new JPanel();
p.add(new JLabel("---Welcom to Student Info Query System---\\n\\r"));
p.add(new JLabel("--Choose the Role to Login---"));
p.add(tr);
p.add(sr);
f.add(p,BorderLayout.CENTER);
f.setVisible(true);
f.setSize(260,260);
f.setLocation(250,250);
tr.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
// TODO Auto-generated method stub
if(e.getActionCommand().equals("Teacher Role"))
f.setVisible(false);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
SecondWindow sw = new SecondWindow();
sw.showMyWindow();
);
sr.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
// TODO Auto-generated method stub
if(e.getActionCommand().equals("Student Role"))
f.setVisible(false);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ThirdWindow tw = new ThirdWindow();
tw.showMyWindow();
);
读文档(这是基于文本文档TXT的简单系统)
package wh.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class ReadStuFromFile
ArrayList<String> stulist = new ArrayList<String>();
public ArrayList<String> getStuFromFile(String getstr)
try
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new FileReader(new File("D:/myjava/eclipseJavaPro/Students.txt")));
String line = br.readLine();
while(line != null)
if(line.contains(getstr))
stulist.add(line);
line = br.readLine();
catch (FileNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
return stulist;
以下是txt文档的内容:
//Begin
1 200328000200056 张文
2 200328000500074 王者超
3 200328000900254 高绍凯
4 200328003200873 吕维
5 200328003200874 马秀敏
6 200328003200877 宋迪
7 200328004101435 钟杨杰
8 200328004201442 田超
9 200328004201450 逯非
10 200328005701636 张婷婷
11 200328005701639 戴志军
12 200328005701646 肖华东
13 200328005701648 朱三妹
14 200328005801673 张立生
15 200328005801678 陈志华
16 200328006001717 张欣
17 200328006201753 卜坤
18 200328006201754 荆玉平
19 200328006201759 徐枫
20 200328006201763 李宏伟
21 200328006301773 陈泽富
22 200328006301791 汤庆新
23 200328006301792 唐富贵
24 200328006301793 张海珍
25 200328006801874 李征
26 200328007001967 杜聪
27 200328007001969 高雪迪
28 200328007001973 黄初冬
29 200328007001989 吴斐
30 200328007001991 熊隽
31 200328007001994 张利辉
32 200328007301997 白伟华
33 200328007502079 殷建华
34 200328010202415 黄粤
35 200328010202417 刘英
36 200328010202418 唐飞
37 200328010202419 赵金
38 200328011202714 李骁健
39 200328012002872 季芬
40 200328012002892 吴旭东
41 200328015504495 李麟
42 200328015504498 林颖
43 200328015504502 邵燕
44 200328015504503 唐炜
45 200328015504504 徐刘靖
46 200328015504505 张剑
47 200328016404703 梁强
48 200328016404704 林志炳
49 200328016404706 乔岩
50 200328016404709 杨瑞广
51 200328016504746 陈利燕
52 200328016504749 方元
53 200328016504761 聂云峰
54 200328540105083 侯春林
55 200328540505135 尹继尧
56 200328540105088 陈宏峰
57 200328530105068 陆兆轼
58 200328600105155 殷红梅
59 200328540405119 佟江
60 200328540505129 胡彬
61 200328600105154 王盛校
62 200328540205102 杨泽
63 200328510105032 陈勇
64 200328530105073 左河疆
65 200328540100011 梁建宏
66 200328540505136 仲霄
67 200328600105148 丁剑
68 200328540505134 武敏捷
69 200328540105094 赵书礼
70 200328600105149 王华
71 200328540105092 许建华
72 2003J8540100013 达勇
73 200328540405120 王丹
74 200328600105152 柏华洁
75 200328540705141 刘冠中
76 200328510105038 金华
77 200328540505128 侯贺晟
78 200328540405116 董运洪
79 200328530105069 孙振宇
//end
----------------------------------分-----割-----线----------------------------------
所有代码在MainTest中测试通过
package test;
import wh.one.FourthWindow;
public class MainTest //Initial Window is Role-Selection(FourthWindow).
public static void main(String[] s)
new FourthWindow().showMyWindow();
----------------------------------分-----割-----线----------------------------------
午睡后草草码了一会,晚上去吃羊排,回来写博客。
2015/12/05
以上是关于JavaStudentsInfoQuery(简单的学生信息查询系统)的主要内容,如果未能解决你的问题,请参考以下文章