2016/04/24 学生管理系统 (有待完善)
package com.wode.test;
/**
* 学生管理系统
*/
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class StudentSystem extends JFrame {
public StudentSystem(String title) {
super(title);
this.setSize(400, 300);// 设置窗体大小
this.setLocationRelativeTo(null);// 居中显示
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 可关闭性
this.setResizable(false);// 禁止用户放大窗口
JPanel jPanel = new JPanel();
jPanel.setLayout(null); // 设置自由布局
jPanel.setBounds(1, 1, 500, 400);// 容器大小和窗口一样大
// 标题
JLabel jLabelheadline = new JLabel("***学生管理系统登录***");
jLabelheadline.setFont(new Font("正楷", Font.BOLD, 20));
jLabelheadline.setBounds(100, 20, 400, 20);
jPanel.add(jLabelheadline);
// 用户输入
JLabel jLabelName = new JLabel("用户名:");
jLabelName.setBounds(100, 65, 60, 20);
jPanel.add(jLabelName);
// 用户输入提示
JLabel txtnameprompt = new JLabel("请输入6-18字母");
txtnameprompt.setBounds(280, 65, 100, 20);
jPanel.add(txtnameprompt);
// 用户输入框
JTextField txtName = new JTextField();
txtName.setBounds(180, 65, 85, 20);
jPanel.add(txtName);
txtName.addFocusListener(new txtNameListener(txtName, txtnameprompt));
// 密码
JLabel jLabelpassword = new JLabel(" 密码:");
jLabelpassword.setBounds(100, 120, 60, 20);
jPanel.add(jLabelpassword);
// 密码输入提示
JLabel txtpasswordprompt = new JLabel("请输入6-18数字");
txtpasswordprompt.setBounds(280, 120, 100, 20);
jPanel.add(txtpasswordprompt);
// 密码输入框
JPasswordField txtpassword = new JPasswordField();
txtpassword.setBounds(180, 120, 85, 20);
txtpassword.addFocusListener(new txtpasswordListener(txtpassword, txtpasswordprompt));
jPanel.add(txtpassword);
// 确定按钮
JButton okbtn = new JButton("确定");
okbtn.setBounds(110, 200, 60, 40);
jPanel.add(okbtn);
// 绑定事件
okbtn.addActionListener(new okListener(txtName, txtpassword));
// 取消按钮
JButton cancelbtn = new JButton("取消");
cancelbtn.setBounds(220, 200, 60, 40);
jPanel.add(cancelbtn);
this.add(jPanel);
this.setVisible(true);
}
public static void main(String[] args) {
new StudentSystem("学生管理系统");
}
}
/**
*
* @author Administrator 鼠标点击确定
*/
class okListener implements ActionListener {
private JTextField txtName;
private JPasswordField txtpassword;
public okListener(JTextField txtName, JPasswordField txtpassword) {
this.txtName = txtName;
this.txtpassword = txtpassword;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
// 触发事件
// System.out.println("ok已经点击");
if (txtName.getText().equals("zhangsan") && txtpassword.getText().equals("admin")) {
// System.out.println("学生界面");
txtName.setText("张三");
new StudentJFrame(txtName.getText());
} else if (txtName.getText().equals("laozhang") && txtpassword.getText().equals("admin")) {
// System.out.println("老师界面");
txtName.setText("张");
new TeacherJFrame(txtName.getText());
}
}
}
/**
*
* @author Administrator 用户输入框焦点
*/
class txtNameListener implements FocusListener {
private JTextField txtName;
private JLabel txtnameprompt;
JLabel jlLabel = new JLabel();
public txtNameListener(JTextField txtName, JLabel txtnameprompt) {
this.txtName = txtName;
this.txtnameprompt = txtnameprompt;
}
@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
// System.out.println("获得焦点");
txtnameprompt.setText("请输入6-18字母");
}
@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
// System.out.println("失去焦点");
if (txtName.getText().length() == 0) {
txtnameprompt.setText("用户名不能为空");
} else if (txtName.getText().length() != 0) {
Pattern p = Pattern.compile("^[a-z]{8,16}$");
Matcher m = p.matcher(txtName.getText());
System.out.println(txtName.getText());
}
}
}
/***
*
* @author Administrator 密码输入焦点
*/
class txtpasswordListener implements FocusListener {
private JPasswordField txtpassword;
private JLabel txtpasswordprompt;
public txtpasswordListener(JPasswordField txtpassword, JLabel txtpasswordprompt) {
this.txtpassword = txtpassword;
this.txtpasswordprompt = txtpasswordprompt;
}
@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
// 获得焦点时
// System.out.println("密码框获得焦点");
txtpasswordprompt.setText("请输入6-18数字");
}
@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
// 失去焦点时
// System.out.println("密码框失去焦点");
if (txtpassword.getText().length() == 0) {
txtpasswordprompt.setText("密码不能为空");
} else if (txtpassword.getText().length() != 0) {
Pattern p = Pattern.compile("^[a-z]{0,16}$");
Matcher m = p.matcher(txtpassword.getText());
if (m.matches()) {
System.out.println(txtpassword.getText());
}
}
}
package com.wode.test;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
public class StudentJFrame extends JFrame {
public StudentJFrame(String StudnetName) {
this.setSize(400, 300);// 设置窗体大小
this.setLocationRelativeTo(null);// 居中显示
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 可关闭性
// this.setResizable(false);// 禁止用户放大窗口
JPanel jPanel = new JPanel();
jPanel.setLayout(null); // 设置自由布局
jPanel.setBounds(1, 1, 500, 400);// 容器大小和窗口一样大
// 标题
JLabel jLabelheadline = new JLabel("*****学生信息*****");
jLabelheadline.setFont(new Font("正楷", Font.BOLD, 20));
jLabelheadline.setBounds(100, 0, 400, 20);
jPanel.add(jLabelheadline);
// 欢迎学生
JLabel jLabelTime = new JLabel(StudnetName + "同学欢迎你");
jLabelTime.setBounds(290, 20, 400, 20);
jPanel.add(jLabelTime);
// 同学信息
DefaultTableModel model = new DefaultTableModel(0, 3);
this.add(jPanel);
this.setVisible(true);
}
public static void main(String[] args) {
new StudentJFrame("");
}
}
package com.wode.test;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class TeacherJFrame extends JFrame {
public TeacherJFrame(String TeacherName) {
this.setSize(400, 300);// 设置窗体大小
this.setLocationRelativeTo(null);// 居中显示
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 可关闭性
this.setResizable(false);// 禁止用户放大窗口
JPanel jPanel = new JPanel();
jPanel.setLayout(null); // 设置自由布局
jPanel.setBounds(1, 1, 500, 400);// 容器大小和窗口一样大
//学生的信息
StudentList stu = new StudentList();
List<Student> list = stu.get();
List<Object> student;
JTable stujtable = new JTable();
DefaultTableModel model = new DefaultTableModel(0,3);
for (int i = 0; i < list.size(); i++) {
student = new ArrayList<>();
student.add(list.get(i).getName());
student.add(list.get(i).getAge());
student.add(list.get(i).getSite());
model.addRow(student.toArray());
}
stujtable.setModel(model);
JPanel stujpanel = new JPanel();
stujpanel.setBounds(0, 200, 30, 30);
stujpanel.setLayout(new BorderLayout());
stujpanel.add(stujtable,BorderLayout.NORTH);
// 标题
JLabel jLabelheadline = new JLabel("****老师管理界面****");
jLabelheadline.setFont(new Font("正楷", Font.BOLD, 20));
jLabelheadline.setBounds(100, 1, 400, 20);
jPanel.add(jLabelheadline);
// 开始
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu begin = new JMenu("开始");
menuBar.add(begin);
JMenuItem exit = new JMenuItem("退出");
exit.addActionListener(new quitListener());
begin.add(exit);
// 时间
Date date = new Date();
SimpleDateFormat a = new SimpleDateFormat("yyyy-MM-dd ");
String daTe = a.format(date);
JLabel dateJLabel = new JLabel(daTe);
dateJLabel.setBounds(320, 0, 400, 30);
jPanel.add(dateJLabel);
// 欢迎
JLabel jLabelWelcome = new JLabel(TeacherName + "老师欢迎您");
jLabelWelcome.setBounds(310, 20, 400, 20);
jPanel.add(jLabelWelcome);
this.add(jPanel);
this.setVisible(true);
}
public static void main(String[] args) {
new TeacherJFrame("");
}
}
class quitListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
// System.out.println("退出系统");
System.exit(0);
}
}
package com.wode.test;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Administrator
*建立一个学生类
*/
public class Student {
private String name;
private int age;
private String site ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSite() {
return site;
}
public void setSite(String site) {
this.site = site;
}
}
package com.wode.test;
import java.util.ArrayList;
import java.util.List;
public class StudentList {
public List<Student> get(){
List<Student> list = new ArrayList<>();
Student s = new Student();
s.setName("张三");
s.setAge(18);
s.setSite("成都");
list.add(s);
Student s1 = new Student();
s.setName("李四");
s.setAge(18);
s.setSite("西昌");
list.add(s1);
Student s2 = new Student();
s.setName("王五");
s.setAge(18);
s.setSite("上海");
list.add(s2);
Student s3 = new Student();
s.setName("狗蛋");
s.setAge(18);
s.setSite("北京");
list.add(s3);
return list;
}
}