java学生信息管理系统(图形界面)

本程序借鉴自:https://blog.csdn.net/weixin_43851639/article/details/89338494   //图形界面部分

本程序借鉴自:https://blog.csdn.net/qq_40819918/article/details/79947068   //业务逻辑部分

主类:Test.java

package gui_stu;

public class Test {

	public static void main(String[] args) {
		//调用登录界面
		Login log = new Login();
		log.setBounds(200,200,600,400);
		log.setTitle("学生管理系统");
	}

}

登录界面类:Login.java

package gui_stu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
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.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.SwingConstants;
import java.awt.FlowLayout;
import java.awt.Font;

//登录界面
public class Login extends JFrame implements ActionListener{


	private static final long serialVersionUID = 1L;
	
	private Login log;
	public Login(Login login) {
		// TODO 自动生成的构造函数存根
		this.log=login;
	}
	
	//定义控件
	JPanel jp1;
	JLabel id,pw,title;
	JButton login,restart;
	JTextField id_t;
	JPasswordField pw_t;
	SpringLayout springLayout = new SpringLayout();
	
	public Login() {
		init();
		this.setVisible(true);
		//窗口大小不可变
		setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
 
	private void init() {
		id = new JLabel("账号:");
		pw = new JLabel("密码:");
		title = new JLabel("学生信息管理系统");
		jp1 = new JPanel(springLayout);
		id_t = new JTextField(15);
		pw_t = new JPasswordField(15);
		login = new JButton("登录");
		restart = new JButton("重置");
		//监听jb1按钮
		login.addActionListener(this);
		restart.addActionListener(this);
		
		//获取内容面板
		Container contentPane = getContentPane();
		id_t.setPreferredSize(new Dimension(200,30));
		pw_t.setPreferredSize(new Dimension(200,30));
		//jpanel.add(title,jpanel.CENTER_ALIGNMENT);
		jp1.add(id);
		id.setPreferredSize(new Dimension(200,30));
		jp1.add(id_t);
		jp1.add(pw);
		pw_t.setPreferredSize(new Dimension(200,30));
		jp1.add(pw_t);
		jp1.add(login);
		jp1.add(restart);
		jp1.add(title);
		
		id.setFont(new Font("微软雅黑",Font.PLAIN,18));
		pw.setFont(new Font("微软雅黑",Font.PLAIN,18));
		title.setFont(new Font("微软雅黑",Font.PLAIN,28));
		
		springLayout.putConstraint(springLayout.WEST, id, -100, springLayout.HORIZONTAL_CENTER, jp1);
		springLayout.putConstraint(springLayout.NORTH, id, -200, springLayout.HORIZONTAL_CENTER, jp1);

		//五个参数,控件的边,控件,距离,相对物品的边,相对物品
		springLayout.putConstraint(springLayout.WEST, id_t, -150, springLayout.EAST, id);
		springLayout.putConstraint(springLayout.NORTH, id_t, 0, springLayout.NORTH, id);
		
		springLayout.putConstraint(springLayout.WEST, pw_t, 0, springLayout.WEST, id_t);
		springLayout.putConstraint(springLayout.NORTH, pw_t, 0, springLayout.NORTH, pw);
		springLayout.putConstraint(springLayout.WEST, pw, -100, springLayout.HORIZONTAL_CENTER, jp1);
		springLayout.putConstraint(springLayout.NORTH, pw, -140, springLayout.HORIZONTAL_CENTER, jp1);
		//放置button,相对于中心
		springLayout.putConstraint(springLayout.EAST, login, 10, springLayout.HORIZONTAL_CENTER, jp1);
		springLayout.putConstraint(springLayout.NORTH, login, 40, springLayout.SOUTH, pw);
		springLayout.putConstraint(springLayout.WEST, restart, 40, springLayout.HORIZONTAL_CENTER, jp1);
		springLayout.putConstraint(springLayout.NORTH, restart, 40, springLayout.SOUTH, pw);
		//放置title
		springLayout.putConstraint(springLayout.WEST, title, -5, springLayout.WEST, id);
		springLayout.putConstraint(springLayout.SOUTH, title, -40, springLayout.NORTH, id);
		//将中间容器放入内容面板
		contentPane.add(jp1,BorderLayout.CENTER);

	}
 
	//事件监听
	@Override
	public void actionPerformed(ActionEvent e) {
		String ad = "admin";
		String psw = "166479";
		//登录
		if (e.getSource() == login) {
			String id_s = id_t.getText();
			String pw_s = pw_t.getText();
			
			if (id_s.equals(ad)) {
				if(pw_s.equals(psw)) {
					WindowOfStudent win = new WindowOfStudent();
					win.setBounds(200,200,600,400);
					win.setTitle("学生管理系统");
					this.dispose();
				}else {
					JOptionPane.showMessageDialog(login, "密码错误!!!");
				}
			} else {
				JOptionPane.showMessageDialog(login, "用户名错误!!!");
			}
		} 
		//重置
		if (e.getSource() == restart) {
			id_t.setText("");
			pw_t.setText("");
		} 
		
	}
}

学生信息模板类:Info.java

package gui_stu;

//学生属性
public class Info {
	private String id;
	private String name;
	private String tel;
	private String score;
	private String qq;
	
	
	public Info() {
		
	}
	
	
	public String getQq() {
		return qq;
	}


	public void setQq(String qq) {
		this.qq = qq;
	}


	public void setId(String id) {
		this.id = id;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setTel(String tel) {
		this.tel = tel;
	}
	
	public void setScore(String score) {
		this.score = score;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getTel() {
		return tel;
	}

	public String getScore() {
		return score;
	}
	
	
}

学生列表及部分操作类:ScoreList.java

package gui_stu;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;
import java.io.IOException;

//本程序学生信息数组因为没有填满,所以不能使用 数组名.length 获取数组长度
//只好定义一个length变量来记录数组元素数量(在插入元素时加一)
public class ScoreList {
	private int maxSize = 100;//最大存储容量
	int length = 0;
	Info[] scoreList = new Info[maxSize]; // 用信息类创建数组
	Scanner sc = new Scanner(System.in);
	Random rand = new Random();
	public ScoreList() {
		
	}
	
	//测试demo,每次测试不需要重新输入数据
	public void demo() {
		for(int i=0;i<9;i++) {
			scoreList[i] = new Info();
			scoreList[i].setId((String)("00"+(i+1)));
			scoreList[i].setName((String)((char)(i+65)+""));//先加上65转换为ASCII的大写英文字母((char)(i+65)) ,然后加上""转换为字符串
			scoreList[i].setTel((String)(rand.nextInt(900000)+100000+""));
			scoreList[i].setScore(rand.nextInt(100) + "");
			scoreList[i].setQq((String)(rand.nextInt(900000000)+100000000+""));
			length++;
		}
	}

	//读取数据
	public void loadData() throws IOException {
		File file = new File("student_info_gui.txt");//D:/Student_java_data/student_info.txt
		//File file = new File("D:/java_data/student_info.txt");
		if(!file.exists()) {
			try {
				file.createNewFile();
				System.out.println("初始化成功");
			}catch (Exception e) {
				e.printStackTrace();
				System.out.println("初始化失败");
			}
		}
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;
			while((line = br.readLine()) != null) {
				String[] str = line.split("\t\t");
				Info stu = new Info();
				stu.setId(str[0]);
				stu.setName(str[1]);
				stu.setTel(str[2]);
				stu.setScore(str[3]);
				stu.setQq(str[4]);
				scoreList[length++] = stu;
				
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	//写入数据
	public void writeData() throws IOException {
		File file = new File("student_info_gui.txt");
		//File file = new File("D:/java_data/student_info.txt");
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			for(int i = 0; i < length; i++) {
				Info stu = scoreList[i];
				bw.write(stu.getId());
				bw.write("\t\t");
				bw.write(stu.getName());
				bw.write("\t\t");
				bw.write(stu.getTel());
				bw.write("\t\t");
				bw.write(stu.getScore());
				bw.write("\t\t");
				bw.write(stu.getQq());
				bw.newLine();
				
			}
			bw.flush();
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

增删改查操作类:no_sql_data.java

package gui_stu;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

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;
import javax.swing.border.EmptyBorder;

public class no_sql_data extends JFrame implements ActionListener{
	public no_sql_data() {
	}
	boolean f = false;
	private static final long serialVersionUID = 1L;
	JLabel jlId,jlName,jlTel,jlScore,jlQQ,jlHelp;
	JTextField jtfId,jtfName,jtfTel,jtfScore,jtfQQ;
	JPanel jp1,jp2,jp3;
	JButton submit,cancel_1,delete,cancel_2,change,cancel_3;
	ScoreList stu = new ScoreList();
	Info data = new Info();
	
	//删除学生
	public void delete() {
		try {
			stu.loadData();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		jlId = new JLabel("请输入要删除学生的学号:");
		jtfId = new JTextField(10);
		
		delete = new JButton("删除");
		//事件监听
		delete.addActionListener(this);
		cancel_2 = new JButton("取消");
		cancel_2.addActionListener(this);
		
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		
		//设置布局
		jp1.setLayout(new GridLayout(6,2));
		jp2.setLayout(new GridLayout(6,2));
		
		jp3.add(delete);
		jp3.add(cancel_2);
		
		jp1.add(jlId);
		
		jp1.add(jtfId);
	
		getContentPane().add(jp1,BorderLayout.CENTER);
		getContentPane().add(jp2,BorderLayout.EAST);
		getContentPane().add(jp3,BorderLayout.SOUTH);
		
		setBounds(500,400,300,200);
		//窗口大小不可变
		setResizable(false);
		setVisible(true);
	}
	
	//添加学生
	public void add() {
		try {
			stu.loadData();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		jlId = new JLabel("学号");
		jlName = new JLabel("姓名");
		jlTel = new JLabel("电话");
		jlScore = new JLabel("成绩");
		jlQQ = new JLabel("QQ");
		
		jtfId = new JTextField(10);
		jtfName = new JTextField(10);
		jtfTel = new JTextField(10);
		jtfScore = new JTextField(10);
		jtfQQ = new JTextField(10);
		
		submit = new JButton("添加");
		//事件监听
		submit.addActionListener(this);
		cancel_1 = new JButton("取消");
		cancel_1.addActionListener(this);
		
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		
		//设置布局
		jp1.setLayout(new GridLayout(6,2));
		jp2.setLayout(new GridLayout(6,2));
		
		jp3.add(submit);
		jp3.add(cancel_1);
		
		jp1.add(jlId);
		jp1.add(jlName);
		jp1.add(jlTel);
		jp1.add(jlScore);
		jp1.add(jlQQ);
		
		jp2.add(jtfId);
		jp2.add(jtfName);
		jp2.add(jtfTel);
		jp2.add(jtfScore);
		jp2.add(jtfQQ);
		
		getContentPane().add(jp1,BorderLayout.WEST);
		getContentPane().add(jp2,BorderLayout.CENTER);
		getContentPane().add(jp3,BorderLayout.SOUTH);
		
		setBounds(500,400,300,200);
		//窗口大小不可变
		setResizable(false);
		setVisible(true);
	}
	
	//修改学生
	public void change() {
		try {
			stu.loadData();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		jlHelp = new JLabel("(学号不可更改,不更改的属性留空即可)");
		jlId = new JLabel("需要修改的学生的学号");
		jlName = new JLabel("姓名");
		jlTel = new JLabel("电话");
		jlScore = new JLabel("成绩");
		jlQQ = new JLabel("QQ");
		
		jtfId = new JTextField(10);
		jtfName = new JTextField(10);
		jtfTel = new JTextField(10);
		jtfScore = new JTextField(10);
		jtfQQ = new JTextField(10);
		
		change = new JButton("修改");
		//事件监听
		change.addActionListener(this);
		cancel_3 = new JButton("取消");
		cancel_3.addActionListener(this);
		
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		
		//设置布局
		jp1.setLayout(new GridLayout(6,2));
		jp2.setLayout(new GridLayout(6,2));
		
		jp3.add(change);
		jp3.add(cancel_3);
		
		
		jp1.add(jlId);
		jp1.add(jlName);
		jp1.add(jlTel);
		jp1.add(jlScore);
		jp1.add(jlQQ);
		jp1.add(jlHelp);
		
		jp2.add(jtfId);
		jp2.add(jtfName);
		jp2.add(jtfTel);
		jp2.add(jtfScore);
		jp2.add(jtfQQ);
		
		getContentPane().add(jp1,BorderLayout.WEST);
		getContentPane().add(jp2,BorderLayout.CENTER);
		getContentPane().add(jp3,BorderLayout.SOUTH);
		
		setBounds(500,400,300,200);
		//窗口大小不可变
		setResizable(false);
		setVisible(true);
	}
	
	//事件监听方法
	@Override
	public void actionPerformed(ActionEvent e) {
		//确认添加
		if(e.getSource() == submit) {
			boolean flag = true;
			for(int i = 0; i < stu.length; i++) {
				//学号不可以重复
				if(stu.scoreList[i].getId().equals(jtfId.getText()))
				{
					flag = false;
					break;
				}
				
			}
			//如果学号没有重复且分数区间无误且学号不为空
			if(!jtfId.getText().equals("") && flag == true && Integer.parseInt(jtfScore.getText()) >= 0 && Integer.parseInt(jtfScore.getText()) <= 100) {
				data.setId(jtfId.getText());
				data.setName(jtfName.getText());
				data.setTel(jtfTel.getText());
				data.setScore(jtfScore.getText());
				data.setQq(jtfQQ.getText());
				stu.scoreList[stu.length++] = data;
				try {
					stu.writeData();//添加完成重新导入数据
					JOptionPane.showMessageDialog(this, "添加成功!!!");
					f = true;
				} catch (IOException e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
				this.dispose();
			}
			else {
				JOptionPane.showMessageDialog(this, "学号不可以重复或者空或者成绩区间[0,100]!!!");
			}
		}
		
		//取消添加
		else if(e.getSource() == cancel_1){
			this.dispose();
		}
		
		//确认删除
		else if(e.getSource() == delete) {
			int flag = -1;
			String id = jtfId.getText();
			if(stu.length>0) {
				for(int i = 0;i<stu.length;i++) {
					if(stu.scoreList[i].getId().equals(id)) {
						flag = i;
						break;//寻找到直接退出,只删除第一个
					}
				}
				if(flag != -1) {
					for(int k = flag; k < stu.length - 1; k++) {
						stu.scoreList[k] = stu.scoreList[k + 1];
					}
					stu.length--;
					try {
						stu.writeData();//删除完成重新导入数据
						JOptionPane.showMessageDialog(this, "删除成功!!!");
					} catch (IOException e1) {
						// TODO 自动生成的 catch 块
						e1.printStackTrace();
					}
				}
				else {
					JOptionPane.showMessageDialog(this, "没有此学生!!!");
				}
			}
			else {
				JOptionPane.showMessageDialog(this, "学生信息为空!!!");
				
			}
			this.dispose();
		}
		
		//取消删除
		else if(e.getSource() == cancel_2) {
			this.dispose();
		}
		
		//确认修改
		else if(e.getSource() == change) {
			int flag = -1;
			String id = jtfId.getText();
			if(stu.length>0) {
				for(int i = 0;i<stu.length;i++) {
					if(stu.scoreList[i].getId().equals(id)) {
						flag = i;
						break;//寻找到直接退出,只删除第一个
					}
				}
			}
			else {
				JOptionPane.showMessageDialog(this, "列表为空,无学生信息!!!");
				this.dispose();
			}
			if(flag != -1) {
				//空下的属性默认不修改
				if(!jtfName.getText().equals("")) {//不为空时才修改
					stu.scoreList[flag].setName(jtfName.getText());
				}
				if(!jtfTel.getText().equals("")) {
					stu.scoreList[flag].setTel(jtfTel.getText());
				}
				
				//分数区间有要求
				if(jtfScore.getText().equals("")) {
					stu.scoreList[flag].setScore(stu.scoreList[flag].getScore());
				}
				else if((Integer.parseInt(jtfScore.getText()) >= 0 && Integer.parseInt(jtfScore.getText()) <= 100)) {
					stu.scoreList[flag].setScore(jtfScore.getText());
					
				}
				else if (Integer.parseInt(jtfScore.getText()) < 0 || Integer.parseInt(jtfScore.getText()) >100){
					JOptionPane.showMessageDialog(this, "成绩区间[0,100]!!!");
				}
				if(!jtfQQ.getText().equals("")) {
					stu.scoreList[flag].setQq(jtfQQ.getText());
				}
				
				try {
					stu.writeData();
					JOptionPane.showMessageDialog(this, "修改成功!!!");
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				
			}
			else {
				JOptionPane.showMessageDialog(this, "无学生信息!!!");
			}
			this.dispose();
		}
		//取消修改
		else if(e.getSource() == cancel_3) {
			this.dispose();
		}
	}
	
}

数据表格模板类:StuModel.java

package gui_stu;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Vector;
 
import javax.swing.table.AbstractTableModel;

public class StuModel extends AbstractTableModel{
	//不知道啥意思
	private static final long serialVersionUID = 1L;
	
	Vector rowData;	//存放数据行
	Vector columnNames;	//存放列名
	
	//创建学生信息数组
	ScoreList stulist = new ScoreList();
	
	//常规导入数据初始化
	public StuModel() {
		try {
			stulist.loadData();
			init();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}
	
	//升序,降序,统计方法初始化
	public StuModel(int k) {
		try {
			stulist.loadData();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(k == 1) {//降序
			down();
		}
		if(k == 2) {//升序
			up();
		}
		if(k == 3) {//统计
			score();
		}
	}
	
	//查询方法初始化
	public StuModel(String str) {
		rowData = new Vector();
		//读取数据
		try {
			stulist.loadData();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//寻找该学号
		for(int i = 0; i < stulist.length; i++) {
			if(stulist.scoreList[i].getId().equals(str)) {
				columnNames = new Vector();
				//设置列名
				columnNames.add("学号");
				columnNames.add("姓名");
				columnNames.add("电话");
				columnNames.add("成绩");
				columnNames.add("QQ");
				
				Vector hang = new Vector<>();
				hang.add(stulist.scoreList[i].getId());
				hang.add(stulist.scoreList[i].getName());
				hang.add(stulist.scoreList[i].getTel());
				hang.add(stulist.scoreList[i].getScore());
				hang.add(stulist.scoreList[i].getQq());
				rowData.add(hang);
				break;
			}
		}
	}
	
	//平均分数等
	public void score() {
		int max=0,min=0;//最大分数,最小分数序号
		double avg=0.0,count_ = 0;//平均分和及格率		
		if(stulist.length>0) {
			for(int i=0;i<stulist.length;i++) {
				//统计及格人数
				if(Integer.parseInt(stulist.scoreList[i].getScore()) >= 60) {
					count_++;
				}
				//寻找最大分数序号
				if(Integer.parseInt(stulist.scoreList[i].getScore()) > Integer.parseInt(stulist.scoreList[max].getScore())) {
					max = i;
				}
				//寻找最小分数序号
				if(Integer.parseInt(stulist.scoreList[i].getScore()) < Integer.parseInt(stulist.scoreList[min].getScore())) {
					min = i;
				}
				//统计平均分
				avg += Integer.parseInt(stulist.scoreList[i].getScore());
			}
			
			columnNames = new Vector();
			//设置列名
			columnNames.add("最高分");
			columnNames.add("最低分");
			columnNames.add("平均分");
			columnNames.add("及格率");
			rowData = new Vector();
			Vector hang = new Vector<>();
			hang.add(stulist.scoreList[max].getScore());
			hang.add(stulist.scoreList[min].getScore());
			hang.add(String.format("%.2f", (avg / stulist.length)));
			hang.add(String.format("%.2f", (count_ / (stulist.length + 1))*100) + "%");
			rowData.add(hang);
		}
	}
	
	//导入测试数据 重载构造方法
	public StuModel(boolean flag) {//flag单纯是为了区分构造函数
		if(flag) {
			try {
				stulist.demo();//导入随机数据
				stulist.writeData();//写入文件
				stulist.loadData();//读取数据
				init();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	private void init() {
		columnNames = new Vector();
		//设置列名
		columnNames.add("序号");
		columnNames.add("学号");
		columnNames.add("姓名");
		columnNames.add("电话");
		columnNames.add("成绩");
		columnNames.add("QQ");
		
		//获取数据行
		rowData = new Vector();
		int i=0;
			
		int num = 1;//因为未来可能会做排序,所以单独设置一个计数器
		while(i<stulist.length) {
			Vector hang = new Vector<>();
			hang.add(num);
			hang.add(stulist.scoreList[i].getId());
			hang.add(stulist.scoreList[i].getName());
			hang.add(stulist.scoreList[i].getTel());
			hang.add(stulist.scoreList[i].getScore());
			hang.add(stulist.scoreList[i].getQq());
			rowData.add(hang);
			i++;
			num++;
		}
	}
	
	//升序
		public void up() {
			if(stulist.length > 0) {
				Info demo = new Info();
				for(int i = 1;i<stulist.length; i++) {
					for(int j=0;j<stulist.length-1;j++) {
						if(Integer.parseInt(stulist.scoreList[j].getScore()) > Integer.parseInt(stulist.scoreList[j + 1].getScore())) {
							demo = stulist.scoreList[j];
							stulist.scoreList[j] = stulist.scoreList[j+1];
							stulist.scoreList[j+1] = demo;
						}
					}
				}
				init();
			}
		}
	
	//降序
	public void down() {
		if(stulist.length > 0) {
			Info demo = new Info();
			for(int i = 1;i<stulist.length; i++) {
				for(int j=0;j<stulist.length-1;j++) {
					if(Integer.parseInt(stulist.scoreList[j].getScore()) < Integer.parseInt(stulist.scoreList[j + 1].getScore())) {
						demo = stulist.scoreList[j];
						stulist.scoreList[j] = stulist.scoreList[j+1];
						stulist.scoreList[j+1] = demo;
					}
				}
			}
			init();
			
		}
	}
	//下面的看不懂
	@Override
	public int getRowCount() {
		return rowData.size();
	}
 
	@Override
	public int getColumnCount() {
		return columnNames.size();
	}
 
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return ((Vector)(rowData.get(rowIndex))).get(columnIndex);
	}
	
	@Override
	public String getColumnName(int columnIndex) {
		return (String)columnNames.get(columnIndex);
	}
	
}


主程序界面类:WindowOfStudent.java

package gui_stu;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

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.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
 
public class WindowOfStudent extends JFrame implements ActionListener{ 
	
	private static final long serialVersionUID = 1L;
	//定义控件
	JPanel jp1,jp2;
	JLabel jl1,jl2;
	JButton jb1,jb2,jb3,jb4,jbflu,demo,up,down,sum;
	JTable jt;	//表格
	JScrollPane jsp;
	JTextField jtf;
	
	StuModel sm;
	//StuListen listener;
	
	public WindowOfStudent() {
		init();
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
 
	private void init() {
		
		sum = new JButton("统计");
		sum.addActionListener(this);
		up = new JButton("升序");
		up.addActionListener(this);
		down = new JButton("降序");
		down.addActionListener(this);
		demo = new JButton("DEMO");
		demo.addActionListener(this);
		jbflu = new JButton("刷新");
		jbflu.addActionListener(this);
		jp1 = new JPanel();
		jtf = new JTextField(15);
		jb1 = new JButton("查询");
		//监听jb1按钮
		jb1.addActionListener(this);
		
		jl1 = new JLabel("请输入学号:");	//按学号查询
		
		
		jp1.add(jl1);
		jp1.add(jtf);
		jp1.add(jb1);
		jp1.add(demo);
		jp1.add(jbflu);
		
		jb2 = new JButton("添加");
		jb2.addActionListener(this);
		jb3 = new JButton("修改");
		jb3.addActionListener(this);
		jb4 = new JButton("删除");
		jb4.addActionListener(this);
		
		jp2 = new JPanel();
		jp2.add(jb2);
		jp2.add(jb3);
		jp2.add(jb4);
		jp2.add(up);
		jp2.add(down);
		jp2.add(sum);
		
		
		//创建模型
		sm = new StuModel();
		jt = new JTable(sm);
		jsp = new JScrollPane(jt);
		
		this.add(jsp);
		this.add(jp1,"North");
		this.add(jp2,"South");
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		//查询
		if (e.getSource() == jb1) {
			//数据刷新
			sm = new StuModel(jtf.getText());
			jt.setModel(sm);
			
		} 
		
		//添加
		else if (e.getSource() == jb2) {
			//调用添加函数
			no_sql_data add = new no_sql_data();
			add.add();
			sm = new StuModel();
			jt.setModel(sm);
		}
		
		//修改
		else if (e.getSource() == jb3) {
			//调用修改函数
			no_sql_data add = new no_sql_data();
			add.change();
			sm = new StuModel();
			jt.setModel(sm);
			
		}
		//删除
		else if (e.getSource() == jb4) {
			//调用删除函数
			no_sql_data add = new no_sql_data();
			add.delete();
			sm = new StuModel();
			jt.setModel(sm);
		}
		
		else if(e.getSource() == jbflu) {
			//刷新
			sm = new StuModel();
			jt.setModel(sm);
		}
		
		else if(e.getSource() == demo) {
			//导入测试数据
			sm = new StuModel(true);
			jt.setModel(sm);
		}
		else if(e.getSource() == up) {
			//升序
			sm = new StuModel(2);
			jt.setModel(sm);
		}
		else if(e.getSource() == down) {
			//降序
			sm = new StuModel(1);
			jt.setModel(sm);
		}
		else if(e.getSource() == sum) {
			//统计
			sm = new StuModel(3);
			jt.setModel(sm);
		}
	}
}

以上七个java文件就是全部内容,部分包没有使用是因为复制过来的东西不好细改,现在大概能看懂借鉴的图形界面部分的大佬的文章的80%,删去了他的数据库部分,电脑莫名其妙连不上。

以下是源程序文件(eclipse):

链接:https://pan.baidu.com/s/1iOwJFDvZBjSCKgDPRlcAZg 
提取码:dian 
复制这段内容后打开百度网盘手机App,操作更方便哦

以下是已经打好的jar包(可执行文件):

链接:https://pan.baidu.com/s/1WgzGxns57poIwlEjdfl12w 
提取码:wtav 
复制这段内容后打开百度网盘手机App,操作更方便哦

请大家多多支持欧!!!

 

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值