本文共 15736 字,大约阅读时间需要 52 分钟。
项目要求,GUI界面,实现以下功能,能够录入学生的信息及成绩,具体为姓名、学号、应用数学、大学英语、java编程、计算机应用。还能够根据输入学生姓名返回学生的信息。
使用mysql数据库存储 参考这一篇博文:https://blog.csdn.net/qq_44973159/article/details/104119710
一、设置主界面 需要注意的是,在该程序时使用文件对数据进行保存,未连接数据库,记得根据自己电脑本身情况对路劲进行更改,以下代码都是保存在f盘下 我们首先需要简单注册,并且将注册的账号密码写入到文件当中,在登录的时候对账号密码进行判断,//主界面 登录界面,程序从此开始 public class MyFrame01 extends JFrame { private JButton btn1; private JButton btn2; private JTextField tf; private JPasswordField pf; public static void main(String[] args) { MyFrame01 my = new MyFrame01(); } public void init() { setTitle("学生登记系统"); setSize(300, 180); setLocationRelativeTo(null); tf = new JTextField(5); pf = new JPasswordField(5); } public void add1() { btn1 = new JButton("登陆"); JLabel name = new JLabel(" 账号"); JLabel password = new JLabel(" 密码"); JLabel bq1 = new JLabel(" "); JLabel bq2 = new JLabel(" "); JPanel p = new JPanel(); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); JPanel p4 = new JPanel(); JPanel p5 = new JPanel(); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { pd(); } catch (IOException e1) { e1.printStackTrace(); } } }); btn2 = new JButton("注册"); btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new MyFrame03();//使用注册的时候要跳转到第三个界面, MyFrame01.this.dispose(); } }); this.setContentPane(p); // 设置为内容面板 总布 p.setLayout(new BorderLayout()); p1.setLayout(new FlowLayout()); p2.setLayout(new GridLayout(2, 2)); p3.setLayout(new FlowLayout()); p1.add(new JLabel("登陆系统")); p2.add(name); p2.add(tf); p2.add(password); p2.add(pf); p3.add(btn1); p3.add(btn2); p4.add(bq1); p5.add(bq2); p.add(p1, BorderLayout.NORTH); p.add(p2, BorderLayout.CENTER); p.add(p3, BorderLayout.SOUTH); p.add(p4, BorderLayout.EAST); } public MyFrame01() { JPanel cp = (JPanel) getContentPane(); cp.setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); init(); add1(); setVisible(true); } public void pd() throws IOException { File f = new File("F:\\信息系统账号"); String fname[] = f.list(); String a = null; for (String s : fname) { if (s.equals(tf.getText() + ".txt")) { a = s; Reader r = new FileReader("f:\\信息系统账号\\" + s); String b = null; char cs[] = new char[10]; int len = 0; while ((len = r.read(cs)) != -1) { b = new String(cs, 0, len); } if (pf.getText().equals(b)) { JOptionPane.showConfirmDialog(MyFrame01.this, "输入正确!", "系统提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.CANCEL_OPTION); new MyFrame04(); MyFrame01.this.dispose(); } else { JOptionPane.showConfirmDialog(MyFrame01.this, "输入有误,请重新输入!", "系统提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.CANCEL_OPTION); MyFrame01.this.dispose(); new MyFrame01(); } } } }}
二、登录成功后的功能页面
//登录成功后的界面 public class MyFrame04 extends JFrame { private JButton btn1; private JButton btn2; private JButton btn3; private JButton btn4; public void init() { setLayout(new FlowLayout()); setTitle("选择"); setSize(275, 405); setLocationRelativeTo(null); } public void add() { btn1 = new JButton("录入学生信息"); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new MyFrame02(); MyFrame04.this.dispose(); } }); btn2 = new JButton("查找学生信息"); btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new MyFrame06(); MyFrame04.this.dispose(); } }); btn3=new JButton("查询成绩信息"); btn3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new MyFrame08(); MyFrame04.this.dispose(); } }); btn4 = new JButton("退出信息系统"); btn4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int x = JOptionPane.showConfirmDialog(MyFrame04.this, "是否退出系统?", "系统提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.CANCEL_OPTION); if (x == JOptionPane.OK_OPTION) { System.exit(0); } } }); add(btn1); add(btn2);// add(btn3); add(btn3); add(btn4); } public JPanel getFilePanel() { JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.add(new JLabel("选择你要进行的操作!")); return p; } public MyFrame04() { JPanel cp = (JPanel) getContentPane(); cp.setLayout(new FlowLayout()); cp.add(getFilePanel()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); init(); add(); setVisible(true); }}
三。录入学生信息 :录入后的信息将保存在f盘下的学生信息文件夹下,以学生名命名的txt文本文档
//信息录入,把学生基本信息录到进来,保存在f盘下学生信息中, public class MyFrame02 extends JFrame { private String xh; private String yysx; private String dxyy; private String java; private String jsjyy; private JMenuBar bar; private static JTextField tf1; private static JTextField tf2; private static JTextField tf3; private static JTextField tf4; private static JTextField tf5; private static JTextField tf6; private static JButton btn1; private static JButton btn2; public void init() { setLayout(new FlowLayout()); setTitle("学生信息录入"); setSize(500, 300); setLocationRelativeTo(null);}public void add(){ bar=new JMenuBar();}public MyFrame02() { JPanel p=new JPanel(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); p.setLayout(new BorderLayout()); p1.setLayout(new GridLayout(4,2)); p2.setLayout(new FlowLayout()); p1.add(new JLabel("姓名")); tf1=new JTextField(10); p1.add(tf1); p1.add(new JLabel("学号")); tf2=new JTextField(10); p1.add(tf2); p1.add(new JLabel("应用数学")); tf3=new JTextField(10); p1.add(tf3); p1.add(new JLabel("大学英语")); tf4=new JTextField(10); p1.add(tf4); p1.add(new JLabel("java编程")); tf5=new JTextField(10); p1.add(tf5); p1.add(new JLabel("计算机应用")); tf6=new JTextField(10); p1.add(tf6); p1.add(new JLabel());//再加上一个标签把整个代码布局撑起来 btn1=new JButton("保存"); btn2=new JButton("取消"); p2.add(btn1); p2.add(btn2); this.setContentPane(p); p.add(p1,BorderLayout.CENTER); p.add(p2,BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); init(); add(); setVisible(true); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int x=JOptionPane.showConfirmDialog(MyFrame02.this, "是否保存?", "系统提示", JOptionPane.OK_CANCEL_OPTION,JOptionPane.CANCEL_OPTION); if (x==JOptionPane.OK_OPTION) { try { cj(); test01();//test01 静态方法,将数据写入的文件中(文本文档) JOptionPane.showConfirmDialog(MyFrame02.this, "保存成功", "系统提示", JOptionPane.OK_CANCEL_OPTION,JOptionPane.CANCEL_OPTION); MyFrame02.this.dispose(); new MyFrame02();//再一次回到该界面(录入信息界面) } catch (IOException e1) { e1.printStackTrace(); } }else{ JOptionPane.showConfirmDialog(MyFrame02.this, "保存失败", "系统提示", JOptionPane.OK_CANCEL_OPTION,JOptionPane.CANCEL_OPTION); } } }); btn2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new MyFrame04(); MyFrame02.this.dispose(); } }); }public static void test01() throws IOException{ //使用 tf1即姓名+.txt进行保存文件 String name ="f:\\学生信息\\"+tf1.getText()+".txt"; OutputStream os=new FileOutputStream(name); os.write("姓名:".getBytes()); os.write(tf1.getText().getBytes()); os.write("。".getBytes()); os.write("学号:".getBytes()); os.write(tf2.getText().getBytes()); os.write("。".getBytes()); os.write("应用数学:".getBytes()); os.write(tf3.getText().getBytes()); os.write("。".getBytes()); os.write("大学英语:".getBytes()); os.write(tf4.getText().getBytes()); os.write("。".getBytes()); os.write("java:".getBytes()); os.write(tf5.getText().getBytes()); os.write("。".getBytes()); os.write("计算机应用:".getBytes()); os.write(tf6.getText().getBytes()); os.write("。".getBytes()); os.close();}public static void cj(){ File f1=new File("f:\\学生信息"); f1.mkdir(); //该文件不存在时,使用mkdir方法进行创建}}
四、注册界面 差点把这个忘了、、、
// 注册账号 界面 public class MyFrame03 extends JFrame { private JTextField tf; private JPasswordField pf1; private JPasswordField pf2; private JLabel lb1; private JLabel lb2; private JLabel lb3; private JLabel lb4; private JButton btn1; private JButton btn2; private JPanel p=new JPanel(); private JPanel p1=new JPanel(); private JPanel p2=new JPanel(); private JPanel p3=new JPanel(); private JPanel p4=new JPanel(); private JPanel p5=new JPanel(); public void init(){ setTitle("学生登记系统注册账号"); setSize(300, 200); setLocationRelativeTo(null); lb1=new JLabel(" 账号"); lb2=new JLabel(" 密码"); lb3=new JLabel(" 再次输入密码"); lb4=new JLabel("注册账号"); btn1=new JButton("确定"); btn2=new JButton("取消"); tf=new JTextField(20); pf1=new JPasswordField(20); pf2=new JPasswordField(20); this.setContentPane(p); p.setLayout(new BorderLayout()); p1.setLayout(new FlowLayout()); p2.setLayout(new GridLayout(3, 2)); p1.add(lb4); p2.add(lb1); p2.add(tf); p2.add(lb2); p2.add(pf1); p2.add(lb3); p2.add(pf2); p3.add(btn1); p3.add(btn2); p.add(p1,BorderLayout.NORTH); p.add(p2,BorderLayout.CENTER); p.add(p3,BorderLayout.SOUTH); p.add(p4,BorderLayout.EAST); p.add(p5,BorderLayout.WEST);}public void add(){ btn1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if (pf2.getText().toString().trim().equals(pf1.getText().toString().trim())) { try { save(); int x=JOptionPane.showConfirmDialog(MyFrame03.this, "注册成功", "系统提示", JOptionPane.OK_CANCEL_OPTION,JOptionPane.CANCEL_OPTION); if(x==JOptionPane.OK_OPTION){ fh(); } } catch (IOException e1) { e1.printStackTrace(); } }else { JOptionPane.showConfirmDialog(MyFrame03.this, "输入密码不一致,请重新输入", "系统提示", JOptionPane.OK_CANCEL_OPTION,JOptionPane.CANCEL_OPTION); MyFrame03.this.dispose(); new MyFrame03(); } } }); btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fh(); } });}public MyFrame03() { JPanel cp=(JPanel) getContentPane(); cp.setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); init(); add(); setVisible(true);}public void save() throws IOException{ File f1=new File("f:\\信息系统账号"); f1.mkdir(); String name ="f:\\信息系统账号\\"+tf.getText()+".txt"; OutputStream os=new FileOutputStream(name); os.write(pf1.getText().getBytes()); os.close();} public void fh(){ MyFrame03.this.dispose(); new MyFrame01(); }}
五。查找功能
//查找学生信息,并且将其信息输出在该界面上public class MyFrame06 extends JFrame { private JButton btn1; private JButton btn2; private JTextField tf; public void init() { setLayout(new FlowLayout()); setTitle("学生信息查看系统"); setSize(500, 500); setLocationRelativeTo(null); } public void add(){ btn1=new JButton("确定"); btn1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int x=JOptionPane.showConfirmDialog(MyFrame06.this, "是否查看", "系统提示", JOptionPane.OK_CANCEL_OPTION,JOptionPane.CANCEL_OPTION); if (x==JOptionPane.OK_OPTION) { try { ck(); MyFrame06.this.dispose(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }else{ JOptionPane.showConfirmDialog(MyFrame06.this, "查看失败", "系统提示", JOptionPane.OK_CANCEL_OPTION,JOptionPane.CANCEL_OPTION); new MyFrame06(); } } }); btn2=new JButton("取消"); btn2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { MyFrame06.this.dispose(); new MyFrame04(); } }); add(btn1); add(btn2); } public JPanel getFilePanel(){ JPanel p=new JPanel(); p.setLayout(new FlowLayout()); p.add(new JLabel("请输入要查找学生的名字")); tf=new JTextField(10); p.add(tf); return p; } public MyFrame06(){ JPanel cp=(JPanel) getContentPane(); cp.setLayout(new FlowLayout()); cp.add(getFilePanel()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(); init(); setVisible(true); } public void ck() throws IOException { File f=new File("f:\\学生信息"); String fname []=f.list(); boolean b=false; for(String s:fname){ if (s.equals(tf.getText()+".txt")) { new MyFrame07(tf.getText()+".txt"); b=true; } } if (!b) { JOptionPane.showConfirmDialog(MyFrame06.this, "查看失败:学生信息不存在", "系统提示", JOptionPane.OK_CANCEL_OPTION,JOptionPane.CANCEL_OPTION); new MyFrame06(); } MyFrame06.this.dispose(); }}
六。把查找的结果显示出来
// 显示学生信息 public class MyFrame07 extends JFrame { private String xh; private String yysx; private String dxyy; private String java; private String jsjyy; private JButton btn1; private JButton btn2; public void init() { setLayout(new FlowLayout()); setTitle("学生信息"); setSize(500, 500); setLocationRelativeTo(null); } public void add(){ btn1=new JButton("退出"); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); btn2=new JButton("返回"); btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { MyFrame07.this.dispose(); new MyFrame06(); } }); } public MyFrame07(String name) throws IOException { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); init(); add(); dq(name); setVisible(true); JPanel p=new JPanel(); JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JPanel p4=new JPanel(); setContentPane(p); p.setLayout(new BorderLayout()); p1.setLayout(new GridLayout(8,1)); p2.setLayout(new FlowLayout()); p1.add(new JLabel(w0)); p1.add(new JLabel(w1)); p1.add(new JLabel(w2)); p1.add(new JLabel(w3)); p1.add(new JLabel(w4)); p1.add(new JLabel(w5)); p2.add(btn1); p2.add(btn2); p.add(p1,BorderLayout.CENTER); p.add(p2,BorderLayout.SOUTH); p.add(p3,BorderLayout.EAST); p.add(p4,BorderLayout.WEST); } String w0=null; String w1=null; String w2=null; String w3=null; String w4=null; String w5=null; public void dq(String name) throws IOException{ File f=new File("f:\\学生信息"); String fname []=f.list(); Reader r=new FileReader("f:\\学生信息\\"+name); String b=null; char cs[]=new char[1024*1024]; int len=0; while ((len=r.read(cs))!=-1) { b=new String(cs,0,len); } r.close(); String[] s=b.split("。"); w0=s[0]; w1=s[1]; w2=s[2]; w3=s[3]; w4=s[4]; w5=s[5]; }}
七、查询成绩信息,还未实现,后续更新后将补齐
如果上述代码调试不成功的话,请私信我(或者在主页有我的联系方式),将把源码发送给你。 运行结果: