学生信息管理系统-GUI
来源:互联网 发布:单代号搭接网络计划sts 编辑:程序博客网 时间:2024/05/21 17:46
本人java 初学者,使用了已经过时的GUI图形用户界面。
程序运了IO进行数据的读写,没有涉及到数据库的使用。
写道最后发现有很多的缺点,面向对象的程序设计思想很多地方体现的不是很到位,还有频繁的操作磁盘文件。
也参考过许多网上许多大佬的方法。
算是给自己学java半年以来的一个说法吧。
最后将代码分享给大家,一起学习!
package MyStu;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;
class Login extends JFrame implements ActionListener{
static ArrayList<Student> as = new ArrayList<Student>();
JButton b1,b2=null;
JPanel jp1,jp2,jp3=null;
JTextField jtf=null;
JLabel jlb1,jlb2=null;
JPasswordField pwf=null;
final String user="123";
final String psd="123";
public Login(){
Image icon=Toolkit.getDefaultToolkit().getImage("E://1.gif");
this.setIconImage(icon);
b1=new JButton("登陆");
b1.setFont(new Font("华文中宋", Font.BOLD, 15));
b2=new JButton("清除");
b2.setFont(new Font("华文中宋", Font.BOLD, 15));
b1.setBorder(null);
b2.setBorder(null);
b1.addActionListener(this);
b2.addActionListener(this);
jp1=new JPanel();
jp1.setBackground(Color.cyan);
jp2=new JPanel();
jp2.setBackground(Color.YELLOW);
jp3=new JPanel();
jp3.setBackground(Color.orange);
jlb1=new JLabel("用户名:");
jlb2=new JLabel("密 码:");
jlb1.setFont(new Font("华文行楷", Font.BOLD, 20));
jlb2.setFont(new Font("华文行楷", Font.BOLD, 20));
jtf=new JTextField(10);
pwf=new JPasswordField(10);
jp1.add(jlb1);
jp1.add(jtf);
jp2.add(jlb2);
jp2.add(pwf);
jp3.add(b1);
jp3.add(b2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setLayout(new GridLayout(3,1));
this.setTitle("学生信息管理系统");
this.setSize(300,200);
this.setLocation(200, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="登陆"){
stulogin();
}else if (e.getActionCommand()=="清除") {
clear();
}
}
public void stulogin(){
if(user.equals(jtf.getText())&&psd.equals(pwf.getText())){
JOptionPane.showMessageDialog(null,"登录成功!","系统提示",JOptionPane.WARNING_MESSAGE);
clear();
dispose();
Menu1 ui=new Menu1();
}else if(jtf.getText().isEmpty()&&pwf.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"请输入用户名和密码!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(jtf.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"请输入用户名!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(pwf.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"请输入密码!","提示消息",JOptionPane.WARNING_MESSAGE);
}else
{
JOptionPane.showMessageDialog(null,"用户名或者密码错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
//清空输入框
clear();
}
}
public void clear(){
jtf.setText("");
pwf.setText("");
}
}
//---------------------------------------------------------
//Memu功能菜单栏
class Menu1 extends JFrame {
static ArrayList<Student> as = new ArrayList<Student>();
JButton jb1,jb2,jb3,jb4,jb5,jb6=null;
JPanel jp1,jp2,jp3=null;
JLabel jlb1=null;
public Menu1(){
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jb1=new JButton("添加信息");
jb2=new JButton("删除信息");
jb3=new JButton("查找信息");
jb5=new JButton("查看信息");
jb6=new JButton("退出");
jb1.setFont(new Font("华文中宋", Font.BOLD, 18));
jb2.setFont(new Font("华文中宋", Font.BOLD, 18));
jb3.setFont(new Font("华文中宋", Font.BOLD, 18));
jb5.setFont(new Font("华文中宋", Font.BOLD, 18));
jb6.setFont(new Font("华文中宋", Font.BOLD, 18));
jb1.setBorder(null);
jb2.setBorder(null);
jb3.setBorder(null);
jb5.setBorder(null);
jb6.setBorder(null);
jlb1=new JLabel("欢迎使用学生信息管理系统");
jlb1.setHorizontalAlignment(SwingConstants.CENTER);
jlb1.setFont(new Font("华文行楷", Font.BOLD, 40));
jp1. add(jlb1);
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
jp3.add(jb5);
jp3.add(jb6);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setLayout(new GridLayout(3,5));
this.setTitle("学生信息管理系统");
this.setSize(500,400);
//设置窗体初始位置
this.setLocation(200, 150);
//设置当关闭窗口时,保证JVM也退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//显示窗体
this.setVisible(true);
this.setResizable(false);
jb1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="添加信息"){
addinfo_Menu ai=new addinfo_Menu();
ArrayList<Student>as=new ArrayList<Student>();
ai.infor(as);
}
}
});
jb2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="删除信息"){
Delete_info di=new Delete_info();
di.delete_info();
}
}
});
jb3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="查找信息"){
scaninfo si=new scaninfo();
si.Scaninfo();
}
}
});
jb5.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="查看信息"){
readinfo ri=new readinfo();
ri.Readinfo();
}
}
});
jb6.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="退出"){
dispose();
Login lg=new Login();
}
}
});
}
public void addinfo(){
}
}
//主类--------------------------------------------------
public class Wenfan{
public static void main(String[] args){
Login lg=new Login();
}
}
package MyStu;
import java.awt.*;
import java.awt.event.*;
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 java.util.*;
import javax.swing.*;
class addinfo_Menu extends JFrame implements ActionListener {
ArrayList<Student>as=new ArrayList<Student>();
JButton jb1,jb2,jb3,jb4=null;
JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7=null;
JTextField jtf1,jtf2,jtf3,jtf4,jtf5=null;
JLabel jlb1,jlb2,jlb3,jlb4,jlb5,jlb6=null;
public void infor(final ArrayList<Student>as){
jb1=new JButton("保存");
jb2=new JButton("返回");
jb3=new JButton("继续添加");
jb4=new JButton("清除文件数据");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb1.setFont(new Font("华文中宋", Font.BOLD, 15));
jb2.setFont(new Font("华文中宋", Font.BOLD, 15));
jb3.setFont(new Font("华文中宋", Font.BOLD, 15));
jb4.setFont(new Font("华文中宋", Font.BOLD, 15));
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jp5=new JPanel();
jp6=new JPanel();
jp7=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jp4.setBackground(Color.cyan);
jp5.setBackground(Color.cyan);
jp6.setBackground(Color.cyan);
jp7.setBackground(Color.cyan);
jlb1=new JLabel("输入学生信息");
jlb1.setFont(new Font("华文行楷", Font.BOLD, 30));
jlb2=new JLabel("姓名:");
jlb3=new JLabel("性别:");
jlb4=new JLabel("年龄:");
jlb5=new JLabel("学号:");
jlb6=new JLabel("成绩:");
jlb1.setFont(new Font("华文行楷", Font.BOLD, 25));
jlb2.setFont(new Font("华文行楷", Font.BOLD, 25));
jlb3.setFont(new Font("华文行楷", Font.BOLD, 25));
jlb4.setFont(new Font("华文行楷", Font.BOLD, 25));
jlb5.setFont(new Font("华文行楷", Font.BOLD, 25));
jlb6.setFont(new Font("华文行楷", Font.BOLD, 25));
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
jtf4=new JTextField(10);
jtf5=new JTextField(10);
jp1.add(jlb1);
jp2.add(jlb2);
jp2.add(jtf1);
jp3.add(jlb3);
jp3.add(jtf2);
jp4.add(jlb4);
jp4.add(jtf3);
jp5.add(jlb5);
jp5.add(jtf4);
jp6.add(jlb6);
jp6.add(jtf5);
jp7.add(jb1);
jp7.add(jb3);
jp7.add(jb4);
jp7.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.add(jp7);
//设置布局管理器
this.setLayout(new GridLayout(7,1));
//给窗口设置标题
this.setTitle("学生成绩管理系统");
//设置窗体大小
this.setSize(500,400);
//设置窗体初始位置
this.setLocation(200, 150);
//显示窗体
this.setVisible(true);
this.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="保存"){
try {
File stu_csv = new File("E://stu.txt");
if(stu_csv.exists()) //
stu_csv.delete();
stu_csv.createNewFile();
String stu_name,stu_gendar,stu_age,stu_number,stu_score;
stu_name=jtf1.getText();
stu_gendar=jtf2.getText();
stu_age=jtf3.getText();
stu_number=jtf4.getText();
stu_score=jtf5.getText();
as.add(new Student(stu_name,stu_gendar,stu_age,stu_number,stu_score));
BufferedWriter bfw = new BufferedWriter(new FileWriter(stu_csv,true));
//bfw.newLine();
for (Student stu : as) {
System.out.println(stu.name+","+stu.gendar+","+stu.age+","+stu.number+","+stu.score);
bfw.write(stu.name+","+stu.gendar+","+stu.age+","+stu.number+","+stu.score+"\r\n");
}
bfw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null,"已保存姓名为:"+jtf1.getText()+"的信息","系统提示",JOptionPane.INFORMATION_MESSAGE);
}else if(e.getActionCommand()=="继续添加"){
reset();
}else if(e.getActionCommand()=="清除文件数据"){
try {
File stu_csv = new File("E://stu.txt");
if(stu_csv.exists())
stu_csv.delete();
stu_csv.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(e.getActionCommand()=="返回"){
dispose();
}
}
public void reset(){
jtf1.setText("");
jtf2.setText("");
jtf3.setText("");
jtf4.setText("");
}
}
class Student{
String name;
String gendar;
String number;
String age;
String score;
public Student(String name,String gendar,String age,String number,String score){
this.name=name;
this.gendar=gendar;
this.age=age;
this.number=number;
this.score=score;
}
}
package MyStu;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 java.util.ArrayList;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
class Delete_info extends JFrame implements ActionListener{
JButton jb1,jb2,jb3,jb4,jb5;
JLabel jlb1,jlb2,jlb3,jlb4;
JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;
JTextField jtf1;
public void delete_info(){
jp1=new JPanel();
jp2=new JPanel();
jp7=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp7.setBackground(Color.cyan);
jb1=new JButton("按学号删除");
jb2=new JButton("按姓名删除");
jb5=new JButton("返回");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb5.addActionListener(this);
jb1.setBorder(null);
jb2.setBorder(null);
jb5.setBorder(null);
jb1.setFont(new Font("华文中宋", Font.BOLD, 20));
jb2.setFont(new Font("华文中宋", Font.BOLD, 20));
jb5.setFont(new Font("华文中宋", Font.BOLD, 15));
jlb1=new JLabel("删除信息");
jlb1.setFont(new Font("华文行楷", Font.BOLD, 30));
jp1.add(jlb1);
jp2.add(jb1);
jp2.add(jb2);
jp7.add(jb5);
this.add(jp1);
this.add(jp2);
this.add(jp7);
this.setLayout(new GridLayout(3,1));
this.setTitle("删除");
this.setSize(500,400);
this.setLocation(200,150);
this.setVisible(true);
this.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="按学号删除"){
Delete_Number dn=new Delete_Number();
dn.delete_number();
}else if(e.getActionCommand()=="按姓名删除"){
Delete_Name dne=new Delete_Name();
dne.delete_name();
}else if(e.getActionCommand()=="返回"){
dispose();
}
}
}
class Delete_Number extends JFrame implements ActionListener{
JButton jb1,jb2;
JPanel jp1,jp2,jp3,jp4,jp5;
JTextField jtf1;
JLabel jlb1,jlb2,jlb3;
ArrayList<Student3> as3 = new ArrayList<Student3>();
BufferedWriter bfw;
public void delete_number(){
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jp4.setBackground(Color.cyan);
jb1=new JButton("删除");
jb2=new JButton("返回");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb1.setBorder(null);
jb2.setBorder(null);
jb1.setFont(new Font("华文中宋", Font.BOLD, 20));
jb2.setFont(new Font("华文中宋", Font.BOLD, 20));
jtf1=new JTextField(10);
jlb1=new JLabel("删除信息");
jlb1.setFont(new Font("华文行楷", Font.BOLD, 30));
jlb2=new JLabel("请输入学号:");
jlb2.setFont(new Font("华文行楷", Font.BOLD, 20));
jp1.add(jlb1);
jp2.add(jlb2);
jp2.add(jtf1);
jp2.add(jb1);
jp3.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setLayout(new GridLayout(3,1));
this.setTitle("删除");
this.setSize(500,400);
this.setLocation(200,150);
this.setVisible(true);
this.setResizable(false);
try {
File csv = new File("E://stu.txt"); // CSV文件
BufferedReader br = new BufferedReader(new FileReader(csv));
// 读取直到最后一行
String line = "";
while ((line = br.readLine()) != null) {
// 把一行数据分割成多个字段
StringTokenizer st1 = new StringTokenizer(line, ",");
Student3 st2= new Student3(st1.nextToken(),st1.nextToken(),st1.nextToken(),st1.nextToken(),st1.nextToken());
as3.add(st2);
}
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
boolean flag=false;
if(e.getActionCommand()=="删除"){
if(jtf1.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"请输入用户名!","提示消息",JOptionPane.WARNING_MESSAGE);
}
for(Student3 student :as3){
if(student.number.equals(jtf1.getText())){
as3.remove(student);
File txt = new File("E://stu.txt"); // CSV文件
txt.delete();
try {
txt.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
flag=true;
break;
}
}
if(flag){
try {
File txt = new File("E://stu.txt");
bfw = new BufferedWriter(new FileWriter(txt,true));
for(Student3 student1 :as3){
bfw.write(student1.name+","+student1.gendar+","+student1.age+","+student1.number+","+student1.score+"\r\n");
System.out.println(student1.name+","+student1.gendar+","+student1.age+","+student1.number+","+student1.score);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null,"已删除学号为:"+jtf1.getText()+"的学生信息","系统提示",JOptionPane.INFORMATION_MESSAGE);
try {
bfw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
JOptionPane.showMessageDialog(null,"无学号为:"+jtf1.getText()+"的信息!","提示消息",JOptionPane.WARNING_MESSAGE);
}
}else if(e.getActionCommand()=="返回"){
dispose();
}
}
}
//--------------------------按姓名删除菜单----------------、
class Delete_Name extends JFrame implements ActionListener{
JButton jb1,jb2;
JPanel jp1,jp2,jp3,jp4,jp5;
JTextField jtf1;
JLabel jlb1,jlb2,jlb3;
ArrayList<Student3> as3 = new ArrayList<Student3>();
BufferedWriter bfw;
public void delete_name(){
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jb1=new JButton("删除");
jb2=new JButton("返回");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb1.setFont(new Font("华文中宋", Font.BOLD, 20));
jb2.setFont(new Font("华文中宋", Font.BOLD, 20));
jb1.setBorder(null);
jb2.setBorder(null);
jtf1=new JTextField(10);
jlb1=new JLabel("删除信息");
jlb1.setFont(new Font("华文行楷", Font.BOLD, 30));
jlb2=new JLabel("请输入姓名:");
jlb2.setFont(new Font("华文行楷", Font.BOLD, 20));
jp1.add(jlb1);
jp2.add(jlb2);
jp2.add(jtf1);
jp2.add(jb1);
jp3.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setLayout(new GridLayout(3,1));
this.setTitle("删除");
this.setSize(500,400);
this.setLocation(200,150);
this.setVisible(true);
this.setResizable(false);
try {
File csv = new File("E://stu.txt"); // CSV文件
BufferedReader br = new BufferedReader(new FileReader(csv));
// 读取直到最后一行
String line = "";
while ((line = br.readLine()) != null) {
// 把一行数据分割成多个字段
StringTokenizer st1 = new StringTokenizer(line, ",");
Student3 st2= new Student3(st1.nextToken(),st1.nextToken(),st1.nextToken(),st1.nextToken(),st1.nextToken());
as3.add(st2);
}
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
boolean flag=false;
if(e.getActionCommand()=="删除"){
if(jtf1.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"请输入姓名!","提示消息",JOptionPane.WARNING_MESSAGE);
}
for(Student3 student :as3){
if(student.name.equals(jtf1.getText())){
as3.remove(student);
File txt = new File("E://stu.txt"); // CSV文件
txt.delete();
try {
txt.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jtf1.setText("");
flag=true;
break;
}
}
if(flag){
try {
File txt = new File("E://stu.txt");
bfw = new BufferedWriter(new FileWriter(txt,true));
for(Student3 student1 :as3){
bfw.write(student1.name+","+student1.gendar+","+student1.age+","+student1.number+","+student1.score+"\r\n");
System.out.println(student1.name+","+student1.gendar+","+student1.age+","+student1.number+","+student1.score);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null,"已删除姓名为:"+jtf1.getText()+"的学生信息","系统提示",JOptionPane.INFORMATION_MESSAGE);
try {
bfw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
JOptionPane.showMessageDialog(null,"无姓名为:"+jtf1.getText()+"的信息!","提示消息",JOptionPane.WARNING_MESSAGE);
}
}else if(e.getActionCommand()=="返回"){
dispose();
}
}
}
class Student3{
String name;
String gendar;
String number;
String age;
String score;
public Student3(String name,String gendar,String age,String number,String score){
this.name=name;
this.gendar=gendar;
this.age=age;
this.number=number;
this.score=score;
}
}
package MyStu;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
class readinfo extends JFrame implements ActionListener {
JLabel jlb;
JButton jb1;
JPanel jp1,jp2;
public void Readinfo(){
jp1=new JPanel();
jp2=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jb1=new JButton("返回");
jb1.addActionListener(this);
jb1.setFont(new Font("华文中宋", Font.BOLD, 18));
jb1.setBorder(null);
jlb=new JLabel("所有信息如下");
jlb.setFont(new Font("华文行楷", Font.BOLD, 30));
JTableHeader tableH;
String col[]={"姓名","性别","年龄","学号","成绩"};
final DefaultTableModel tableModel = new DefaultTableModel(col, 0);
JTable table = new JTable(tableModel);
table.setEnabled(false);
JScrollPane pane = new JScrollPane(table);
pane.setBackground(Color.cyan);
jp1.add(jlb);
jp2.add(jb1);
ArrayList<Student1> as1 = new ArrayList<Student1>();
try {
File csv = new File("E://stu.txt"); // CSV文件
BufferedReader br = new BufferedReader(new FileReader(csv));
String line = "";
while ((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, ",");
Student1 st1= new Student1(st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken());
as1.add(st1);
}
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.add(jp1);
this.add(pane);
this.add(jp2);
this.setBackground(Color.cyan);
this.setLayout(new GridLayout(3,1));
this.setTitle("显示信息");
this.setSize(500,400);
this.setLocation(200, 150);
this.setVisible(true);
this.setResizable(false);
table.setBackground(new Color(144,238,144));
table.setForeground(new Color(100,100,100)) ;
table.setGridColor(new Color(105 ,105, 105));
tableH = table.getTableHeader();
tableH.setBackground(new Color(200, 200, 200));
tableH.setForeground(new Color(0,0,205));
for (Student1 stu : as1) {
Object [] data ={stu.name,stu.gendar,stu.age,stu.number,stu.score};
tableModel.addRow(data);
}
tableModel.fireTableDataChanged();
RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);
table.setRowSorter(sorter);
this.getContentPane().add(pane, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="返回"){
dispose();
}
}
}
class Student1{
String name;
String gendar;
String number;
String age;
String score;
public Student1(String name,String gendar,String age,String number,String score){
this.name=name;
this.gendar=gendar;
this.age=age;
this.number=number;
this.score=score;
}
}
package MyStu;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
class scaninfo extends JFrame implements ActionListener {
JButton jb1,jb2,jb5;
JLabel jlb1,jlb2,jlb3,jlb4;
JPanel jp1,jp2,jp7;
public void Scaninfo(){
jp1=new JPanel();
jp2=new JPanel();
jp7=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp7.setBackground(Color.cyan);
jb1=new JButton("按学号查找");
jb2=new JButton("按姓名查找");
jb5=new JButton("返回");
jb1.setFont(new Font("华文中宋", Font.BOLD, 20));
jb2.setFont(new Font("华文中宋", Font.BOLD, 20));
jb5.setFont(new Font("华文中宋", Font.BOLD, 15));
jb1.addActionListener(this);
jb2.addActionListener(this);
jb5.addActionListener(this);
jb1.setBorder(null);
jb2.setBorder(null);
jb5.setBorder(null);
jlb1=new JLabel("查找信息");
jlb1.setFont(new Font("华文行楷", Font.BOLD, 30));
jp1.add(jlb1);
jp2.add(jb1);
jp2.add(jb2);
jp7.add(jb5);
this.add(jp1);
this.add(jp2);
this.add(jp7);
this.setLayout(new GridLayout(3,1));
this.setTitle("查询");
this.setSize(500,400);
this.setLocation(200,150);
this.setVisible(true);
this.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="按学号查找"){
Lookup_number look_n=new Lookup_number();
look_n.lookup_number();
}else if(e.getActionCommand()=="按姓名查找"){
Lookup_name look_ne=new Lookup_name();
look_ne.lookup_name();
}else if(e.getActionCommand()=="返回"){
dispose();
}
}
//---------------按学号查询界面----------------
}
//---------------按学号查询界面----------------
class Lookup_number extends JFrame implements ActionListener{
JButton jb1,jb2;
JPanel jp1,jp2,jp3,jp4,jp5;
JTextField jtf1;
JLabel jlb1,jlb2,jlb3;
JTable jtb;
ArrayList<Student2> as2 = new ArrayList<Student2>();
String col[]={"姓名","性别","年龄","学号","成绩"};
final DefaultTableModel tableModel = new DefaultTableModel(col, 0);
public void lookup_number(){
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jp4.setBackground(Color.cyan);
JTableHeader tableH;
JTable table = new JTable(tableModel);
table.setEnabled(false);
table.setBackground(new Color(144,238,144));
table.setForeground(new Color(100,100,100)) ;
table.setGridColor(new Color(105 ,105, 105));
tableH = table.getTableHeader();
tableH.setBackground(new Color(200, 200, 200));
tableH.setForeground(new Color(0,0,205));
JScrollPane pane = new JScrollPane(table);
jb1=new JButton("查询");
jb2=new JButton("返回");
jb1.setFont(new Font("华文中宋", Font.BOLD, 18));
jb2.setFont(new Font("华文中宋", Font.BOLD, 18));
jb1.addActionListener(this);
jb2.addActionListener(this);
jb1.setBorder(null);
jb2.setBorder(null);
jtf1=new JTextField(10);
jlb1=new JLabel("按学号查询信息");
jlb1.setFont(new Font("华文行楷", Font.BOLD, 30));
jlb2=new JLabel("请输入学号:");
jlb2.setFont(new Font("华文行楷", Font.BOLD, 20));
jlb3=new JLabel("查询信息如下");
jlb2.setFont(new Font("华文行楷", Font.BOLD, 25));
jp1.add(jlb1);
jp2.add(jlb2);
jp2.add(jtf1);
jp2.add(jb1);
jp3.add(jlb3);
jp4.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(pane);
this.add(jp4);
this.setLayout(new GridLayout(5,1));
//给窗口设置标题
this.setTitle("查询结果");
//设置窗体大小
this.setSize(500,400);
//设置窗体初始位置
this.setLocation(200, 150);
//显示窗体
this.setVisible(true);
this.setResizable(false);
try {
File csv = new File("E://stu.txt"); // CSV文件
BufferedReader br = new BufferedReader(new FileReader(csv));
// 读取直到最后一行
String line = "";
while ((line = br.readLine()) != null) {
// 把一行数据分割成多个字段
StringTokenizer st = new StringTokenizer(line, ",");
Student2 st1= new Student2(st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken());
as2.add(st1);
}
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="查询"){
boolean flag=true;
tableModel.setRowCount(0); // 之前的table一定要清零!
for (Student2 student : as2) {
System.out.println(student.name+student.gendar+student.age+student.number+student.score);
if(student.number.equals(jtf1.getText())){
Object [] data1 ={student.name,student.gendar,student.age,student.number,student.score};
tableModel.addRow(data1);
flag=false;
JOptionPane.showMessageDialog(null,"已查询到学号为:"+jtf1.getText()+"的学生信息","系统提示",JOptionPane.INFORMATION_MESSAGE);
}
}
if(flag){
JOptionPane.showMessageDialog(null,"无学号为:"+jtf1.getText()+"的学生信息","系统提示",JOptionPane.INFORMATION_MESSAGE);
}
}else if(e.getActionCommand()=="返回"){
dispose();
}
}
}
//------------------按姓名查找界面------------------------
class Lookup_name extends JFrame implements ActionListener{
JButton jb1,jb2;
JPanel jp1,jp2,jp3,jp4,jp5;
JTextField jtf1;
JLabel jlb1,jlb2,jlb3;
JTable jtb;
ArrayList<Student2> as2 = new ArrayList<Student2>();
String col[]={"姓名","性别","年龄","学号","成绩"};
final DefaultTableModel tableModel = new DefaultTableModel(col, 0);
public void lookup_name(){
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jp1.setBackground(Color.cyan);
jp2.setBackground(Color.cyan);
jp3.setBackground(Color.cyan);
jp4.setBackground(Color.cyan);
JTableHeader tableH;
JTable table = new JTable(tableModel);
table.setEnabled(false);
table.setEnabled(false);
table.setBackground(new Color(144,238,144));
table.setForeground(new Color(100,100,100)) ;
table.setGridColor(new Color(105 ,105, 105));
tableH = table.getTableHeader();
//设置表头的背景色
tableH.setBackground(new Color(200, 200, 200));
//设置表头的文字颜色
tableH.setForeground(new Color(0,0,205));
JScrollPane pane = new JScrollPane(table);
jb1=new JButton("查询");
jb2=new JButton("返回");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb1.setBorder(null);
jb2.setBorder(null);
jtf1=new JTextField(10);
jlb1=new JLabel("按姓名查询信息");
jlb1.setFont(new Font("华文行楷", Font.BOLD, 30));
jlb2=new JLabel("请输入姓名:");
jlb2.setFont(new Font("华文行楷", Font.BOLD, 20));
jlb3=new JLabel("查询信息如下");
jlb3.setFont(new Font("华文行楷", Font.BOLD, 25));
jp1.add(jlb1);
jp2.add(jlb2);
jp2.add(jtf1);
jp2.add(jb1);
jp3.add(jlb3);
jp4.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(pane);
this.add(jp4);
this.setLayout(new GridLayout(5,1));
this.setTitle("查询结果");
this.setSize(500,400);
this.setLocation(200, 150);
this.setVisible(true);
this.setResizable(false);
try {
File txt = new File("E://stu.txt");
BufferedReader br = new BufferedReader(new FileReader(txt));
String line = "";
while ((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, ",");
Student2 st1= new Student2(st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken(),st.nextToken());
as2.add(st1);
}
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="查询"){
tableModel.setRowCount(0);
boolean flag=true;
for (Student2 student : as2) {
System.out.println(student.name+student.gendar+student.age+student.number+student.score);
if(student.name.equals(jtf1.getText())){
Object [] data1 ={student.name,student.gendar,student.age,student.number,student.score};
tableModel.addRow(data1);
JOptionPane.showMessageDialog(null,"已查询到姓名为:"+jtf1.getText()+"的学生信息","系统提示",JOptionPane.INFORMATION_MESSAGE);
flag=false;
}
}
if(flag){
JOptionPane.showMessageDialog(null,"无姓名为:"+jtf1.getText()+"的学生信息","系统提示",JOptionPane.INFORMATION_MESSAGE);
}
}else if(e.getActionCommand()=="返回"){
dispose();
}
}
}
class Student2{
String name;
String gendar;
String number;
String age;
String score;
public Student2(String name,String gendar,String age,String number,String score){
this.name=name;
this.gendar=gendar;
this.age=age;
this.number=number;
this.score=score;
}
}
- 学生信息管理系统-GUI
- GUI JDBC 学生信息管理系统
- 学生信息管理系统
- 学生学籍信息管理系统
- 学生信息管理系统
- 学生信息管理系统
- 学生成绩信息管理系统
- 学生信息管理系统
- 【学生信息管理系统 总结】
- 学生信息管理系统--毕业论文
- 学生信息管理系统
- 学生信息管理系统
- 学生信息管理系统~~~~~
- 学生信息管理系统
- 学生信息管理系统总结
- 学生信息管理系统
- 学生信息管理系统
- 学生信息管理系统
- 金融企业软件测试中心筹备书-软件度量篇
- 变量提升的思考
- aop的理解
- 由异步回调引发的线程/进程“血案”
- 如何在iOS中使用SM2进行数字签名及校验
- 学生信息管理系统-GUI
- rocketmq
- 组合-聚合,关联-依赖
- unity 在移动平台中,文件操作路径详解
- 两个CPU核分配任务问题
- Linux 搭建hexo博客
- 分解质因数
- Java工程读取resources中资源文件路径问题
- ftp