写在前面:
这学期学了java面向对象程序设计这门课(非计算机专业),这是本人的结课大作业,学的都是一些很基础很基础的东西,在网上找了一些资料,然后扩展了一下,最终做出来了一个最简单的学生教务管理系统。其中主要就是用到了Java中的swing做的界面,结合数据库来实现信息的查询,内容很简单,可能还会有些错误,仅供大家参考。
本系统用到的工具:IntelliJ IDEA、SqlServer 2008、sqljdbc4.jar包
系统主要功能:
1、此管理系统可以让用户以学生或者老师的身份进行登录;
2、学生界面显示学生的姓名和学号,并且可以查询自己的个人信息、考试成绩、学期课程,还可以根据自己的需求修改登录密码;
3、教师界面显示教师的姓名和教工号,并且可以查询个人信息,包括:性别、学院、职称、所教学的科目等等,还可以输入学生的学号来查询自己所教课程的分数并且对其进行修改;
4、学生和教师的信息都存储在数据库中,要求Java程序可以连接数据库来实现登录、查询、修改等操作。
安装配置参考:
下载SqlServer2008:打开迅雷,复制下面这行链接下载
ed2k://|file|cn_sql_server_2008_r2_enterprise_x86_x64_ia64_dvd_522233.iso|4662884352|1DB025218B01B48C6B76D6D88630F541|/
在已完成下载里找到,右击打开文件夹,找到一个文件类型是光盘映像文件的文件,右击装载,找到setup进行安装,安装指导:B站视频 BV1Pb41187hr
下面是教你如何连接数据库:
在这一步中需要用到sqljdbc4.jar包:点击下载jar包(密码:7ht3)
创建数据库:打开数据库SQL Server Management Studio,点击连接,右击数据库创建一个名叫 教务系统数据库 的数据库,打开,右击表,创建三个表如下:
每个表的列如下:
注意一定要调整好长度,像学号用户名密码等一定是你想要设置多长的就设置多少长度(即nchar(n)中n的数值),后面的像课程这些列中多余出来的长度在代码中将用空格代替
数据表内容自己填写,这里只放一部分我的数据库
![](https://i-blog.csdnimg.cn/blog_migrate/bf1fc4c70be5d26a8a5b9b1e66dd4f1f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/09d5ec0a92a54fa7f33603c6770a4ae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ba8547c519ec29e7129f6f98dcaa0cf3.png)
类设计:
(1) 登录界面
1、功能:登录界面可以输入用户名和密码来实现用户的登录,而且可以选择不同的身份来登录(学生或教师);在输入用户名和密码登录的过程中,要对用户名和密码进行匹配判断此用户是否可以登录进入学生或者教师界面。
2、设计:此处用到了Java的swing包和awt包等来实现可视化界面,利用了其中的各种组件来实现自己想要的界面形式。登录界面要有一个普通文本框(j_zhangHao)来输入用户名,一个特殊的密码文本框(j_miMa)来输入登录密码,一行普通文本框(j_yanZhengMa)来输入验证码,需要有两个单选按钮(j_jiaoShi、j_xueSheng)来判断登录者是学生或者教师,2个单击按钮(b_dengLu、b_tuiChu)来进行动作的监听与响应,分别为登录、重置和退出3个按钮,并且最后用面板(JPanel)将各组件结合起来显示在窗口上。
(2) 学生界面
1、 功能:学生界面可以显示该学生的姓名和学号,并可以通过单击查询个人信息、查询成绩、课程查询按钮来查询自己的个人信息、考试成绩和本学期课程信息,还可以单击修改密码按钮来修改自己的登录密码,单击退出程序按钮退出该程序。
2、 设计:首先在登录时获取学生的姓名和学号,并作为创建学生界面的参数,使用标签将学生姓名和学号显示在界面上方;接着需要有5个单击按钮分别为查询个人信息、查询成绩、课程查询、修改密码和退出系统;有两个表格分别显示个人信息和成绩信息,当点击个人信息查询时该学生的个人信息会显示在上方表格中,当点击查询成绩按钮时该学生的考试成绩会显示在下方表格中;当点击课程查询按钮时会弹出一个本学期课程信息的界面,下面将会详细介绍;当点击修改密码按钮时会弹出一个修改密码的界面,下面将会详细介绍;当点击退出系统的时候,所有界面都会关闭。
(3) 教师界面
1、 功能:教师界面可以显示教师的姓名和教工号,并可以通过单击个人信息来查询自己的信息,例如:学院、职称、年龄、所教课程等;通过输入某个学生的学号来查询该名老师所教课程这个学生的分数;单击修改成绩可以修改该学生的成绩。
2、 设计:首先在登录时获取教师的姓名和教工号,并作为创建教师界面的参数,使用标签将姓名和教工号显示在界面上方,接着需要5个单击按钮分别为查询个人信息、查询成绩、修改密码、修改成绩和退出系统;在查询个人信息按钮下方插入一个表格用来显示教师的个人信息,当单击个人信息按钮时该教师的个人信息会显示在表格中;在查询成绩按钮前面加入一个普通文本框用来输入学生学号,在下面插入一个表格用来显示成绩信息,当点击查询成绩时,会根据输入的学号来查询该名同学的成绩信息并显示在下方的表格中;当点击修改密码按钮时会弹出一个修改密码的界面,下面将会详细介绍;当单击修改成绩按钮时会弹出一个修改成绩的界面,下面会详细介绍;当点击退出系统按钮时,所有界面将会关闭。
(4) 修改密码界面(学生用户修改密码)
1、 功能:通过输入原密码和新密码、确认密码来实现学生登录密码的修改;
2、设计:主要是使用3个文本框,1个普通的文本框来输入原密码,另外2个密码文本框用来输入新密码和确认密码,还有1个普通文本框用来输入验证码,后面还设有2个单击按钮分别为确认修改和退出修改,当4个文本框中的输入的密码均满足要求,点击确认修改即可修改当前密码为新密码。
(5)修改密码界面(教师用户修改密码)
功能与设计同学生用户修改密码界面。
(6) 修改成绩界面
1、 功能:通过输入一个分数来对学生原分数进行修改;
2、设计:使用到1个普通文本框输入想要修改的成绩,并且此处成绩不能小于0大于100;后面设有3个单击按钮分别为确认修改和退出修改,当在文本框中输入一个满足条件的分数,点击确认修改之后就可以将此学生成绩修改。
(7)课程表界面
1、 功能:可以显示该学生的姓名和学号,并可以通过单击查询按钮来查询自己的本学期课程信息,可以单击返回上一页按钮返回学生界面,单击退出程序按钮退出该程序。
2、 设计:首先在学生界面获取学生的姓名和学号,并作为创建课程查询界面的参数,使用标签将学生姓名和学号显示在界面上方;接着需要有3个单击按钮分别为查询课程信息、返回上一页和退出系统;有1个表格显示半学期课程信息,当点击查询按钮时该学生的本学期课程会显示在上方表格中;当点击返回上一页按钮是将返回学生用户界面;当点击退出系统的时候,所有界面都会关闭。
(8)验证码设置
该设置将随机生成4位验证码,在登录界面和修改密码界面使用。
代码及展现:(注意所有类都放在 student_system包里)
- Login类
package student_system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Login extends JFrame implements ActionListener{
yanzhengcode vcode = new yanzhengcode();
JPanel p1,p2,p3,p4,p5,p6 = null;
JLabel yongHuMing, miMa, shenFen,yanZhengMa,huanying = null;
JTextField j_zhangHao,j_yanZhengMa = null;
JPasswordField j_miMa = null;
JRadioButton j_jiaoShi, j_xueSheng = null;
ButtonGroup anNiuZu = null;
JButton b_dengLu, b_tuiChu = null;
String user_name;
String password;
String stu_name;
String stu_xueHao;
String tea_name;
String jiaoGongHao;
static Connection ct = null;
PreparedStatement ps = null;
ResultSet rs = null;
public static void main(String[] args){
Login login = new Login();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=教务系统数据库", "sa", "这个地方输入你自己的数据库密码");
} catch (Exception e) {
e.printStackTrace();
}
}
public Login() {
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
p6 = new JPanel();
yongHuMing = new JLabel("用户名: ");
miMa = new JLabel("密 码: ");
shenFen = new JLabel("身 份: ");
yanZhengMa = new JLabel("验证码: ");
huanying = new JLabel(new ImageIcon("这个地方添加一个照片的地址"));
huanying.setBounds(0, 150, 600, 230);
j_zhangHao = new JTextField(20);
j_yanZhengMa = new JTextField(10);
j_miMa = new JPasswordField(20);
j_jiaoShi = new JRadioButton("教师");
j_xueSheng = new JRadioButton("学生");
anNiuZu = new ButtonGroup();
anNiuZu.add(j_jiaoShi);
anNiuZu.add(j_xueSheng);
j_xueSheng.setSelected(true);
b_dengLu = new JButton("登录");
b_tuiChu = new JButton("退出");
b_dengLu.addActionListener(this);
b_tuiChu.addActionListener(this);
yongHuMing.setHorizontalAlignment(JLabel.CENTER);
miMa.setHorizontalAlignment(JLabel.CENTER);
shenFen.setHorizontalAlignment(JLabel.CENTER);
yanZhengMa.setHorizontalAlignment(JLabel.CENTER);
yongHuMing.setFont(new Font("宋体", Font.PLAIN, 30) );
miMa.setFont(new Font("宋体", Font.PLAIN, 30) );
shenFen.setFont(new Font("宋体", Font.PLAIN, 30) );
yanZhengMa.setFont(new Font("宋体", Font.PLAIN, 30) );
j_jiaoShi.setFont(new Font("黑体", Font.PLAIN, 20) );
j_xueSheng.setFont(new Font("黑体", Font.PLAIN, 20) );
b_dengLu.setFont(new Font("楷体", Font.PLAIN, 50) );
b_tuiChu.setFont(new Font("楷体", Font.PLAIN, 50) );
p1.add(yongHuMing);
p1.add(j_zhangHao);
p2.add(miMa);
p2.add(j_miMa);
p3.add(shenFen);
p3.add(j_jiaoShi);
p3.add(j_xueSheng);
p4.add(b_dengLu);
p4.add(b_tuiChu);
p5.add(yanZhengMa);
p5.add(j_yanZhengMa);
p5.add(vcode);
p6.add(huanying);
this.add(p6);
this.add(p1);
this.add(p2);
this.add(p5);
this.add(p3);
this.add(p4);
this.setLayout(new GridLayout(6,1));
this.setTitle("xx大学学生管理系统");
this.setSize(800, 850);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(true);
}
public void clear(){
j_zhangHao.setText("");
j_miMa.setText("");
j_yanZhengMa.setText("");
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == "登录"){
if(j_jiaoShi.isSelected()){
try {
ps = ct.prepareStatement("select * from Teacher where 身份=? and 用户名=?");
ps.setString(1, "教师");
ps.setString(2, j_zhangHao.getText());
rs = ps.executeQuery();
if(rs.next()){
user_name = rs.getString(2);
password = rs.getString(3);
tea_name = rs.getString(5);
jiaoGongHao = rs.getString(4);
tea_login();
}else {
JOptionPane.showMessageDialog(null, "没有此用户或用户名为空!\n请重新输入", "提示消息", JOptionPane.WARNING_MESSAGE);
}
}catch (SQLException e1) {
e1.printStackTrace();
}
}else if(j_xueSheng.isSelected()) {
try {
ps = ct.prepareStatement("select * from Student where 身份=? and 用户名=?");
ps.setString(1, "学生");
ps.setString(2, j_zhangHao.getText());
rs=ps.executeQuery();
if(rs.next()){
user_name = rs.getString(2);
password = rs.getString(3);
stu_name = rs.getString(5);
stu_xueHao = rs.getString(4);
stu_login();
}else {
JOptionPane.showMessageDialog(null, "没有此用户或用户名为空!\n请重新输入", "提示消息", JOptionPane.WARNING_MESSAGE);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}else if(e.getActionCommand() == "退出"){
System.exit(0);
}
}
public void stu_login(){
if(user_name.equals(j_zhangHao.getText()) && password.equals(String.valueOf(j_miMa.getPassword()))&&vcode.getCode() .equals(j_yanZhengMa.getText())){
JOptionPane.showMessageDialog(null, "登陆成功!", "提示消息", JOptionPane.WARNING_MESSAGE);
clear();
dispose();
Student stu = new Student(stu_name, stu_xueHao);
}else if(j_zhangHao.getText().isEmpty() && String.valueOf(j_miMa.getPassword()).isEmpty()){
JOptionPane.showMessageDialog(null, "请输入用户名和密码!", "提示消息", JOptionPane.WARNING_MESSAGE);
}else if(j_zhangHao.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "请输入用户名!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else if(String.valueOf(j_miMa.getPassword()).isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入密码!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else if(String.valueOf(j_yanZhengMa.getText()).isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入验证码!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else{
JOptionPane.showMessageDialog(null, "登录失败!用户名、密码或者验证码错误!\n请重新输入", "提示信息", JOptionPane.ERROR_MESSAGE);
clear();
}
}
public void tea_login() {
if(user_name.equals(j_zhangHao.getText()) && password.equals(String.valueOf(j_miMa.getPassword()))&&vcode.getCode() .equals(j_yanZhengMa.getText())) {
JOptionPane.showMessageDialog(null, "登录成功", "提示信息", JOptionPane.WARNING_MESSAGE);
clear();
dispose();
Teacher tea = new Teacher(tea_name, jiaoGongHao);
}else if(j_zhangHao.getText().isEmpty() && String.valueOf(j_miMa.getPassword()).isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入用户名和密码!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else if(j_zhangHao.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入用户名!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else if(String.valueOf(j_miMa.getPassword()).isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入密码!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else if(String.valueOf(j_yanZhengMa.getText()).isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入验证码!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else {
JOptionPane.showMessageDialog(null, "登录失败!用户名、密码或者验证码错误!\n请重新输入", "提示信息", JOptionPane.ERROR_MESSAGE);
clear();
}
}
}
2、Student类
package student_system;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Student extends JFrame implements ActionListener{
JPanel p1, p2, p3, p4, p5, p6 ,p7 = null;
JLabel xingMin1, xingMin2, xueHao1, xueHao2 = null;
JButton anNiu1, anNiu2, anNiu3, anNiu4,anNiu5 = null;
JTable biaoGe1, biaoGe2 = null;
DefaultTableModel model_1, model_2 = null;
JScrollPane jsp_1, jsp_2 = null;
// 存储学生的学号和各科的成绩
String Sno;
String math;
String PE;
String java;
String english;
String xingBie;
String xueYuan;
String zhuanYe;
String banJi;
String shengYuanDi;
String stu_name;
String stu_xueHao;
// 设置与数据库连接的对象、sql语句、查询的结果集
static Connection ct = null;
PreparedStatement ps = null;
ResultSet rs = null;
public static void main(String[] args)
{
Student stu = new Student("测试页面","请从首页登录界面打开!");
}
Student(String name,String sno){
Sno = sno; // 得到登录学生学号
// 连接数据库(加载驱动、建立连接)
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=教务系统数据库", "sa","自己的数据库密码");
} catch (Exception e) {
e.printStackTrace();
}
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
p6 = new JPanel();
p7 = new JPanel();
xingMin1 = new JLabel("姓名:");
xingMin2 = new JLabel(name);
xueHao1 = new JLabel("学号:");
xueHao2 = new JLabel(sno);
xingMin1.setFont(new java.awt.Font("Dialog", 1, 30));
xingMin2.setFont(new java.awt.Font("Dialog", 1, 25));
xueHao1.setFont(new java.awt.Font("Dialog", 1, 30));
xueHao2.setFont(new java.awt.Font("Dialog", 1, 25));
anNiu1 = new JButton("个人信息查询");
anNiu2 = new JButton("本学期成绩查询");
anNiu3 = new JButton("退出系统");
anNiu4 = new JButton("修改密码");
anNiu5 = new JButton("课程查询");
anNiu1.setFont(new java.awt.Font("楷体", 1, 20));
anNiu2.setFont(new java.awt.Font("楷体", 1, 20));
anNiu3.setFont(new java.awt.Font("楷体", 1, 20));
anNiu4.setFont(new java.awt.Font("楷体", 1, 20));
anNiu5.setFont(new java.awt.Font("楷体", 1, 23));
anNiu1.addActionListener(this);
anNiu2.addActionListener(this);
anNiu3.addActionListener(this);
anNiu4.addActionListener(this);
anNiu5.addActionListener(this);
String[] colnames_1 = {"性别","学院", "专业", "班级", "生源地"};
model_1 = new DefaultTableModel(colnames_1, 1);
biaoGe1 = new JTable(model_1);
jsp_1 = new JScrollPane(biaoGe1);
String[] colnames = {"高数成绩", "体育成绩", "Java成绩", "英语成绩" };
model_2 = new DefaultTableModel(colnames,1);
biaoGe2 = new JTable(model_2);
jsp_2 = new JScrollPane(biaoGe2);
p1.add(xingMin1);
p1.add(xingMin2);
p2.add(xueHao1);
p2.add(xueHao2);
p3.add(anNiu1);
p3.add(anNiu2);
p7.add(jsp_1);
p4.add(jsp_2);
p5.add(anNiu3);
p5.add(anNiu4);
p6.add(anNiu5);
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p7);
this.add(p4);
this.add(p6);
this.add(p5);
// 设置布局管理器(网格布局和流式布局)
this.setLayout(new GridLayout(7, 1));
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
this.setTitle("xx大学学生页面");
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(true);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == "个人信息查询"){
try{
ps = ct.prepareStatement("select * from Student where 学号 =? "); // 将编译好的SQL语句赋值给ps
ps.setString(1, Sno); // 给?赋值,用setString将设置SQL语句按照学号Sno查询
rs = ps.executeQuery(); // ResultSet结果集,将对应学号的每一行信息放在ResultSet中
// 获取对应学号的学生信息
while(rs.next()){
xingBie = rs.getString(10);
xueYuan = rs.getString(11);
zhuanYe = rs.getString(12);
banJi = rs.getString(13);
shengYuanDi = rs.getString(14);
stu_name = rs.getString(5);
stu_xueHao = rs.getString(4);
}
}catch (Exception e1) {
e1.printStackTrace();
}
biaoGe1.setValueAt(xingBie, 0, 0);
biaoGe1.setValueAt(xueYuan, 0, 1);
biaoGe1.setValueAt(zhuanYe, 0, 2);
biaoGe1.setValueAt(banJi, 0, 3);
biaoGe1.setValueAt(shengYuanDi, 0, 4);
}else if(e.getActionCommand() == "本学期成绩查询"){
try{
ps = ct.prepareStatement("select * from Student where 学号 =?");
ps.setString(1, Sno);
rs = ps.executeQuery();
while(rs.next()){
math = rs.getString(6);
PE = rs.getString(7);
java = rs.getString(8);
english = rs.getString(9);
}
}catch (Exception e1) {
e1.printStackTrace();
}
biaoGe2.setValueAt(math, 0, 0);
biaoGe2.setValueAt(PE, 0, 1);
biaoGe2.setValueAt(java, 0, 2);
biaoGe2.setValueAt(english, 0, 3);
}else if(e.getActionCommand() == "课程查询"){
dispose();
Subject sub = new Subject(stu_name, stu_xueHao);
}else if(e.getActionCommand() == "修改密码"){
stuChangePassword cp = new stuChangePassword(Sno);
}else if(e.getActionCommand() == "退出系统"){
System.exit(0);
}
}
}
3.Teacher类
package student_system;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Teacher extends JFrame implements ActionListener{
JPanel p1, p2, p3, p4, p5 ,p6 ,p7,p8 = null;
JLabel xingMin1, xingMin2, jiaoGongHao1, jiaoGongHao2, xueHao,beiZhu = null;
JButton j_geRen, j_chaXun, j_xiuGai, j_tuiChu,j_miMa = null;
JTextField f_chaXun;
JTable biaoGe1, biaoGe2 = null;
DefaultTableModel model_1, model_2 = null;
JScrollPane jsp_1, jsp_2 = null;
String Tno;
String tea_name;
String sexual;
String age;
String xueYuan;
String position;
String subject;
String sno;
String stu_name;
String grade;
static Connection ct = null;
PreparedStatement ps = null;
ResultSet rs = null;
public static void main(String args[]){
Teacher tea = new Teacher("测试界面", "请从主界面进入!");
}
public Teacher(String name, String tno){
Tno = tno;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=教务系统数据库", "sa", "自己数据库密码");
} catch (Exception e) {
e.printStackTrace();
}
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
p6 = new JPanel();
p7 = new JPanel();
p8 = new JPanel();
xingMin1 = new JLabel("姓名:");
xingMin2 = new JLabel(name);
jiaoGongHao1 = new JLabel("教工号:");
jiaoGongHao2 = new JLabel(tno);
xueHao = new JLabel("请输入学号");
beiZhu = new JLabel("注:查询修改学生成绩前请先点击个人信息!");
xingMin1.setFont(new java.awt.Font("Dialog", 1, 30));
xingMin2.setFont(new java.awt.Font("Dialog", 1, 25));
jiaoGongHao1.setFont(new java.awt.Font("Dialog", 1, 30));
jiaoGongHao2.setFont(new java.awt.Font("Dialog", 1, 25));
xueHao.setFont(new java.awt.Font("Dialog", 1, 20));
beiZhu.setFont(new java.awt.Font("楷体", 1, 20));
j_geRen = new JButton("个人信息");
j_chaXun = new JButton("查询成绩");
j_xiuGai = new JButton("修改成绩");
j_tuiChu = new JButton("退出系统");
j_miMa = new JButton("修改密码");
j_geRen.setFont(new java.awt.Font("Dialog", 1, 20));
j_miMa.setFont(new java.awt.Font("Dialog", 1, 20));
j_chaXun.setFont(new java.awt.Font("Dialog", 1, 15));
j_xiuGai.setFont(new java.awt.Font("Dialog", 1, 20));
j_tuiChu.setFont(new java.awt.Font("Dialog", 1, 20));
j_geRen.addActionListener(this);
j_xiuGai.addActionListener(this);
j_chaXun.addActionListener(this);
j_tuiChu.addActionListener(this);
j_miMa.addActionListener(this);
f_chaXun = new JTextField(10);
String[] colnames_1 = {"姓名", "性别", "年龄","学院", "职称", "所教科目"};
model_1 = new DefaultTableModel(colnames_1, 1);
biaoGe1 = new JTable(model_1);
jsp_1 = new JScrollPane(biaoGe1);
String[] colnames_2 = {"姓名", "学号", "科目", "成绩"};
model_2 = new DefaultTableModel(colnames_2, 1);
biaoGe2 = new JTable(model_2);
jsp_2 = new JScrollPane(biaoGe2);
p1.add(xingMin1);
p1.add(xingMin2);
p2.add(jiaoGongHao1);
p2.add(jiaoGongHao2);
p6.add(j_geRen);
p6.add(j_miMa);
p3.add(jsp_1);
p4.add(xueHao);
p4.add(f_chaXun);
p4.add(j_chaXun);
p7.add(jsp_2);
p5.add(j_xiuGai);
p5.add(j_tuiChu);
p8.add(beiZhu);
this.add(p1);
this.add(p2);
this.add(p6);
this.add(p3);
this.add(p4);
this.add(p7);
this.add(p8);
this.add(p5);
this.setLayout(new GridLayout(8, 1));
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
this.setTitle("xx大学教师页面");
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(true);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == "个人信息"){
try{
ps = ct.prepareStatement("select * from Teacher where 教工号 =? ");
ps.setString(1, Tno);
rs = ps.executeQuery();
while(rs.next()){
tea_name = rs.getString(5);
sexual = rs.getString(6);
age = rs.getString(9);
xueYuan = rs.getString(7);
position = rs.getString(8);
subject = rs.getString(10);
}
}catch (Exception e1) {
e1.printStackTrace();
}
biaoGe1.setValueAt(tea_name, 0, 0);
biaoGe1.setValueAt(sexual, 0, 1);
biaoGe1.setValueAt(age, 0, 2);
biaoGe1.setValueAt(xueYuan, 0, 3);
biaoGe1.setValueAt(position, 0, 4);
biaoGe1.setValueAt(subject, 0, 5);
}else if(e.getActionCommand() == "查询成绩"){
sno = f_chaXun.getText();
try{
ps = ct.prepareStatement("select * from Student where 学号 =? ");
ps.setString(1, sno);
rs = ps.executeQuery();
while(rs.next()){
stu_name = rs.getString(5);
if(subject.equals("高数 "))
grade = rs.getString(6);
else if(subject.equals("体育 "))
grade = rs.getString(7);
else if(subject.equals("Java "))
grade = rs.getString(8);
else if(subject.equals("英语 "))
grade = rs.getString(9);
}
}catch (Exception e1) {
e1.printStackTrace();
}
biaoGe2.setValueAt(stu_name, 0, 0);
biaoGe2.setValueAt(sno, 0, 1);
biaoGe2.setValueAt(subject, 0, 2);
biaoGe2.setValueAt(grade, 0, 3);
}else if(e.getActionCommand() == "修改密码"){
teaChangePassword cp = new teaChangePassword(Tno);
}else if(e.getActionCommand() == "修改成绩"){
ChangeGrade cg = new ChangeGrade(sno, subject);
}else if(e.getActionCommand() == "退出系统"){
System.exit(0);
}
}
}
4.Subject类
package student_system;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Subject extends JFrame implements ActionListener {
JPanel p1, p2, p3, p4, p5 = null;
JLabel xingMin1, xingMin2, xueHao1, xueHao2,keCheng = null;
JButton anNiu1, anNiu2,anNiu3 = null;
JTable biaoGe1= null;
DefaultTableModel model_1 = null;
JScrollPane jsp_1 = null;
String Sno;
String k1;
String k2;
String k3;
String k4;
String s1;
String s2;
String s3;
String s4;
String j1;
String j2;
String j3;
String j4;
String sname;
String ssno;
// 设置与数据库连接的对象、sql语句、查询的结果集
static Connection ct = null;
PreparedStatement ps = null;
ResultSet rs = null;
public static void main(String[] args)
{
Subject sub = new Subject("测试页面","请从首页登录界面打开!");
}
Subject(String name,String sno) {
Sno = sno;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=教务系统数据库", "sa", "自己的数据库密码");
} catch (Exception e) {
e.printStackTrace();
}
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
xingMin1 = new JLabel("姓名:");
xingMin2 = new JLabel(name);
xueHao1 = new JLabel("学号:");
xueHao2 = new JLabel(sno);
keCheng = new JLabel("这是您本学期的课程:");
xingMin1.setFont(new java.awt.Font("Dialog", 1, 30));
xingMin2.setFont(new java.awt.Font("Dialog", 1, 25));
xueHao1.setFont(new java.awt.Font("Dialog", 1, 30));
xueHao2.setFont(new java.awt.Font("Dialog", 1, 25));
anNiu1 = new JButton("查询");
anNiu2 = new JButton("退出系统");
anNiu3 = new JButton("返回上一页");
anNiu1.setFont(new java.awt.Font("楷体", 1, 20));
anNiu2.setFont(new java.awt.Font("楷体", 1, 20));
anNiu3.setFont(new java.awt.Font("楷体", 1, 20));
anNiu1.addActionListener(this);
anNiu2.addActionListener(this);
anNiu3.addActionListener(this);
String[] colnames_1 = {"课程", "教师", "上课教室"};
model_1 = new DefaultTableModel(colnames_1, 4);
biaoGe1 = new JTable(model_1);
jsp_1 = new JScrollPane(biaoGe1);
p1.add(xingMin1);
p1.add(xingMin2);
p2.add(xueHao1);
p2.add(xueHao2);
p3.add(keCheng);
p3.add(anNiu1);
p4.add(jsp_1);
p5.add(anNiu2);
p5.add(anNiu3);
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
this.add(p5);
this.setLayout(new GridLayout(7, 1));
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
this.setTitle("课程查询");
this.setSize(500, 700);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "查询"){
try{
ps = ct.prepareStatement("select * from keCheng where 学号 =? ");
ps.setString(1, Sno);
rs = ps.executeQuery();
while(rs.next()){
k1 = rs.getString(3);
s1 = rs.getString(4);
j1 = rs.getString(5);
k2 = rs.getString(6);
s2 = rs.getString(7);
j2 = rs.getString(8);
k3 = rs.getString(9);
s3 = rs.getString(10);
j3 = rs.getString(11);
k4 = rs.getString(12);
s4 = rs.getString(13);
j4 = rs.getString(14);
sname = rs.getString(2);
ssno = rs.getString(1);
}
}catch (Exception e1) {
e1.printStackTrace();
}
biaoGe1.setValueAt(k1, 0, 0);
biaoGe1.setValueAt(s1, 0, 1);
biaoGe1.setValueAt(j1, 0, 2);
biaoGe1.setValueAt(k2, 1, 0);
biaoGe1.setValueAt(s2, 1, 1);
biaoGe1.setValueAt(j2, 1, 2);
biaoGe1.setValueAt(k3, 2, 0);
biaoGe1.setValueAt(s3, 2, 1);
biaoGe1.setValueAt(j3, 2, 2);
biaoGe1.setValueAt(k4, 3, 0);
biaoGe1.setValueAt(s4, 3, 1);
biaoGe1.setValueAt(j4, 3, 2);
}else if(e.getActionCommand() == "返回上一页"){
dispose();
Student stu = new Student(sname, ssno);
}else if(e.getActionCommand() == "退出系统"){
System.exit(0);
}
}}
5.ChangeGrade类
package student_system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ChangeGrade extends JFrame implements ActionListener{
JPanel p1, p2 = null;
JLabel jlb = null;
JTextField jtf = null;
JButton b_queRen, b_chongZhi, b_tuiChu = null;
String Sno;
String subject;
static Connection ct = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void clear(){
jtf.setText("");
}
public ChangeGrade(String sno, String sub){
Sno = sno;
subject = sub;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=教务系统数据库", "sa","自己的数据库密码");
} catch (Exception e) {
e.printStackTrace();
}
p1 = new JPanel();
p2 = new JPanel();
jlb = new JLabel("请输入修改后成绩");
jlb.setFont(new java.awt.Font("Dialog", 1, 20));
jtf = new JTextField(10);
b_queRen = new JButton("确认修改");
b_chongZhi = new JButton("重置");
b_tuiChu = new JButton("退出修改");
b_queRen.setFont(new java.awt.Font("Dialog", 1, 20));
b_chongZhi.setFont(new java.awt.Font("Dialog", 1, 20));
b_tuiChu.setFont(new java.awt.Font("Dialog", 1, 20));
b_queRen.addActionListener(this);
b_chongZhi.addActionListener(this);
b_tuiChu.addActionListener(this);
p1.add(jlb);
p1.add(jtf);
p1.add(b_chongZhi);
p2.add(b_queRen);
p2.add(b_tuiChu);
this.add(p1);
this.add(p2);
this.setLayout(new GridLayout(2,1));
this.setTitle("修改所教科目成绩");
this.setSize(500, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(true);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == "确认修改"){
change_grade();
}else if(e.getActionCommand() == "重置"){
clear();
}else if(e.getActionCommand() == "退出修改"){
dispose();
}
}
public void change_grade(){
String new_grade = String.valueOf(jtf.getText());
double num_grade = 0;
if(!new_grade.isEmpty())
num_grade = Double.valueOf(new_grade);
if(new_grade.isEmpty()){
JOptionPane.showMessageDialog(null, "修改成绩为空\n请重新输入!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else if(num_grade > 100 || num_grade < 0){
JOptionPane.showMessageDialog(null, "分数必须在0~100之间\n请重新输入!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else {
try{
String sql;
if(subject.equals("高数 ")) {
sql = "update Student set 高数=? where 学号=?";
ps = ct.prepareStatement(sql);
}else if(subject.equals("体育 ")) {
sql = "update Student set 体育 = ? where 学号=?";
ps = ct.prepareStatement(sql);
}else if(subject.equals("Java ")) {
sql = "update Student set java=? where 学号=?";
ps = ct.prepareStatement(sql);
} else if(subject.equals("英语 ")){
sql = "update Student set 英语=? where 学号=?";
ps = ct.prepareStatement(sql);
} else if(subject.equals("会计 ")){
sql = "update Student set 会计=? where 学号=?";
ps = ct.prepareStatement(sql);
}
ps.setString(1, new_grade);
ps.setString(2, Sno);
int count=ps.executeUpdate();
ct.close();
}
catch (SQLException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "修改成功", "提示信息", JOptionPane.WARNING_MESSAGE);
dispose();
}
}
}
6.stuChangePassword类
package student_system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class stuChangePassword extends JFrame implements ActionListener{
yanzhengcode vcode = new yanzhengcode();
JPanel p1, p2, p3, p4, p5 = null;
JLabel yuanMiMa, xingMiMa, queRenMiMa ,yanZhengMa= null;
JTextField f_yuanMiMa,f_yanZhengMa = null;
JPasswordField f_xingMiMa, f_queRenMiMa = null;
JButton b_queRen, b_tuiChu = null;
String Sno;
String old_password;
static Connection ct = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void clear(){
f_yuanMiMa.setText("");
f_xingMiMa.setText("");
f_queRenMiMa.setText("");
f_yanZhengMa.setText("");
}
public stuChangePassword(String sno){
Sno = sno;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=教务系统数据库", "sa", "自己的数据库密码");
} catch (Exception e) {
e.printStackTrace();
}
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
yuanMiMa = new JLabel("原密码");
xingMiMa = new JLabel("新密码(请设置六位数密码)");
queRenMiMa = new JLabel("确认密码(请输入六位数新密码)");
yanZhengMa = new JLabel("验证码");
f_yuanMiMa = new JTextField(10);
f_xingMiMa = new JPasswordField(10);
f_queRenMiMa = new JPasswordField(10);
f_yanZhengMa = new JTextField(4);
b_queRen = new JButton("确认修改");
b_tuiChu = new JButton("退出修改");
b_queRen.addActionListener(this);
b_tuiChu.addActionListener(this);
p1.add(yuanMiMa);
p1.add(f_yuanMiMa);
p2.add(xingMiMa);
p2.add(f_xingMiMa);
p3.add(queRenMiMa);
p3.add(f_queRenMiMa);
p4.add(b_queRen);
p4.add(b_tuiChu);
p5.add(yanZhengMa);
p5.add(f_yanZhengMa);
p5.add(vcode);
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p5);
this.add(p4);
this.setLayout(new GridLayout(5,1));
this.setTitle("修改密码");
this.setSize(500, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(true);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == "确认修改"){
try {
ps = ct.prepareStatement("select * from Student where 学号=? ");
ps.setString(1, Sno);
rs=ps.executeQuery();
while(rs.next()){
old_password = rs.getString(3);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
change_password();
}else if(e.getActionCommand() == "退出修改"){
dispose();
}
}
public void change_password(){
String new_password_1 = String.valueOf(f_xingMiMa.getPassword());
String new_password_2 = String.valueOf(f_queRenMiMa.getPassword());
if(old_password.equals(f_yuanMiMa.getText()) && new_password_1.equals(new_password_2)&&vcode.getCode() .equals(f_yanZhengMa.getText()) && !new_password_1.isEmpty() && !new_password_2.isEmpty()){
try{
String sql="update Student set 密码=? where 学号=? ";
ps = ct.prepareStatement(sql);
ps.setString(1, new_password_1);
ps.setString(2, Sno);
int count=ps.executeUpdate();
ct.close();
}catch (SQLException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "修改成功", "提示信息", JOptionPane.WARNING_MESSAGE);
dispose();
}else if(f_yuanMiMa.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "请输入原密码!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else if(!old_password.equals(f_yuanMiMa.getText()))
{
JOptionPane.showMessageDialog(null, "原密码错误\n请重新输入!", "提示信息", JOptionPane.ERROR_MESSAGE);
clear();
}else if(new_password_1.isEmpty() || new_password_2.isEmpty())
{
JOptionPane.showMessageDialog(null, "新密码或者确认密码为空\n请重新输入!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else if(!new_password_1.equals(new_password_2))
{
JOptionPane.showMessageDialog(null, "确认密码与新密码不符\n请重新输入!", "提示信息", JOptionPane.ERROR_MESSAGE);
}else if(!f_yanZhengMa.equals(vcode.getCode()))
{
JOptionPane.showMessageDialog(null, "验证码错误\n请重新输入!", "提示信息", JOptionPane.ERROR_MESSAGE);
}
}}
7.teaChangePassword类
package student_system;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class teaChangePassword extends JFrame implements ActionListener{
yanzhengcode vcode = new yanzhengcode();
JPanel p1, p2, p3, p4, p5 = null;
JLabel yuanMiMa, xingMiMa, queRenMiMa ,yanZhengMa= null;
JTextField f_yuanMiMa,f_yanZhengMa = null;
JPasswordField f_xingMiMa, f_queRenMiMa = null;
JButton b_queRen, b_tuiChu = null;
String Tno;
String old_password;
static Connection ct = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void clear(){
f_yuanMiMa.setText("");
f_xingMiMa.setText("");
f_queRenMiMa.setText("");
f_yanZhengMa.setText("");
}
public teaChangePassword(String tno){
Tno = tno;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=教务系统数据库", "sa", "自己的数据库密码");
} catch (Exception e) {
e.printStackTrace();
}
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
yuanMiMa = new JLabel("原密码");
xingMiMa = new JLabel("新密码(请设置六位数密码)");
queRenMiMa = new JLabel("确认密码(请输入六位数新密码)");
yanZhengMa = new JLabel("验证码");
f_yuanMiMa = new JTextField(10);
f_xingMiMa = new JPasswordField(10);
f_queRenMiMa = new JPasswordField(10);
f_yanZhengMa = new JTextField(4);
b_queRen = new JButton("确认修改");
b_tuiChu = new JButton("退出修改");
b_queRen.addActionListener(this);
b_tuiChu.addActionListener(this);
p1.add(yuanMiMa);
p1.add(f_yuanMiMa);
p2.add(xingMiMa);
p2.add(f_xingMiMa);
p3.add(queRenMiMa);
p3.add(f_queRenMiMa);
p4.add(b_queRen);
p4.add(b_tuiChu);
p5.add(yanZhengMa);
p5.add(f_yanZhengMa);
p5.add(vcode);
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p5);
this.add(p4);
this.setLayout(new GridLayout(5,1));
this.setTitle("修改密码");
this.setSize(500, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(true);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == "确认修改"){
try {
ps = ct.prepareStatement("select * from Teacher where 教工号=? ");
ps.setString(1, Tno);
rs=ps.executeQuery();
while(rs.next()){
old_password = rs.getString(3);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
change_password();
}else if(e.getActionCommand() == "退出修改"){
dispose();
}
}
public void change_password(){
String new_password_1 = String.valueOf(f_xingMiMa.getPassword());
String new_password_2 = String.valueOf(f_queRenMiMa.getPassword());
if(old_password.equals(f_yuanMiMa.getText()) && new_password_1.equals(new_password_2)&&vcode.getCode() .equals(f_yanZhengMa.getText()) && !new_password_1.isEmpty() && !new_password_2.isEmpty()){
try{
String sql="update Teacher set 密码=? where 教工号=? ";
ps = ct.prepareStatement(sql);
ps.setString(1, new_password_1);
ps.setString(2, Tno);
int count=ps.executeUpdate();
ct.close();
}catch (SQLException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "修改成功", "提示信息", JOptionPane.WARNING_MESSAGE);
dispose();
}else if(f_yuanMiMa.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "请输入原密码!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else if(!old_password.equals(f_yuanMiMa.getText()))
{
JOptionPane.showMessageDialog(null, "原密码错误\n请重新输入!", "提示信息", JOptionPane.ERROR_MESSAGE);
clear();
}else if(new_password_1.isEmpty() || new_password_2.isEmpty())
{
JOptionPane.showMessageDialog(null, "新密码或者确认密码为空\n请重新输入!", "提示信息", JOptionPane.WARNING_MESSAGE);
}else if(!new_password_1.equals(new_password_2))
{
JOptionPane.showMessageDialog(null, "确认密码与新密码不符\n请重新输入!", "提示信息", JOptionPane.ERROR_MESSAGE);
}else if(!f_yanZhengMa.equals(vcode.getCode()))
{
JOptionPane.showMessageDialog(null, "验证码错误\n请重新输入!", "提示信息", JOptionPane.ERROR_MESSAGE);
}
}}
8.yanzhengcode类
package student_system;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JComponent;
public class yanzhengcode extends JComponent implements MouseListener {
private String codes; //自动生成的验证码
private int width, height = 40; //设置验证码高度、宽度
private int codesLength = 4; //设置代码长度
private Random random = new Random(); //生成数字的方法
public yanzhengcode() {
width = this.codesLength * 16 + (this.codesLength - 1) * 10; //根据验证码长度设置宽度
setPreferredSize(new Dimension(width, height)); //设置背景大小
setSize(width, height); //设置验证码长度和宽度
this.addMouseListener(this);
setToolTipText("点击可更换验证码");
}
//得到生成的验证码
public int getCodesLength() {
return codesLength;
}
//设置验证码的长度
public void setCodesLength(int codeLength) {
if(codesLength < 4) {
this.codesLength = 4;
} else {
this.codesLength = codeLength;
}
}
public String getCode() {
return codes;
}
//让验证码产生随机的颜色
public Color getRandColor(int min, int max) {
if (min > 255)
min = 255;
if (max > 255)
max = 255;
int red = random.nextInt(max - min) + min;
int green = random.nextInt(max - min) + min;
int blue = random.nextInt(max - min) + min;
return new Color(red, green, blue);
}
// 设置验证码具体的数字或字母是什么
protected String generateCode() {
char[] codes = new char[this.codesLength];
for (int i = 0, len = codes.length; i < len; i++) {
if (random.nextBoolean()) {
codes[i] = (char) (random.nextInt(10) + 48);
} else {
codes[i] = (char) (random.nextInt(26) + 97);
}
}
this.codes = new String(codes);
return this.codes;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(this.codes == null || this.codes.length() != this.codesLength) { //判断生成的验证码是否为空或超出长度
this.codes = generateCode();
}
width = this.codesLength * 16 + (this.codesLength - 1) * 10;
super.setSize(width, height); //接口使用,验证码字体大小
super.setPreferredSize(new Dimension(width, height));//接口使用,验证码背景大小
Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25); //设置字体和字体大小
g.setFont(mFont); //设置对象
//绘制出验证码的背景的矩形轮廓
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(getRandColor(200, 250));
g2d.fillRect(0, 0, width, height);
g2d.setColor(getRandColor(180, 200));
g2d.drawRect(0, 0, width - 1, height - 1);
//绘制出验证码背景的线
int i = 0, len = 150;
for (; i < len; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(width - 10) + 10;
int y1 = random.nextInt(height - 4) + 4;
g2d.setColor(getRandColor(180, 200));
g2d.drawLine(x, y, x1, y1);
}
//绘制出验证码的具体字母
i = 0; len = this.codesLength;
FontMetrics fm = g2d.getFontMetrics();
int base = (height - fm.getHeight())/2 + fm.getAscent();
for(;i<len;i++) {
int b = random.nextBoolean() ? 1 : -1;
g2d.rotate(random.nextInt(10)*0.01*b);
g2d.setColor(getRandColor(20, 130));
g2d.drawString(codes.charAt(i)+"", 16 * i + 10, base);
}
}
//下一个验证码
public void nextCode() {
generateCode();
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
nextCode();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
总结与心得
经过几周的课设,我所做的大作业学生成绩管理系统部分功能基本完成。其功能基本符合的用户要求,能够对学生成绩的基本信息进行查询、修改。通过此课程设计,使我们学到了以前没有学过的很多知识。熟悉了 SqlServer和 SQL 语句,Swing GUI 工具包和 Intelij IDEA 开发工具等。编程过程中遇到了很多问题,最后通过求助老师和同学以及上网查询成功解决。课本的知识还是不够的,我应该扩展自己的课外知识,多多阅读课外的相关知识,这样才能对 Java 更加熟悉。最后感谢我的老师和同学们在书写大作业过程中给我的帮助。
写在最后
以上就是全部内容了,动手实现的过程中如果有不懂的地方一定要查资料。。。(当然如果有错请见谅,毕竟只是一个小白)
好了,如果觉得有帮助点个赞也行,哈哈哈,加油,奥里给!