本文实例为大家分享了java+io+swing实现学生信息管理系统的具体代码,供大家参考,具体内容如下
说明:
1.开发环境基于eclipse外加windowbuilder插件。
2.采用io流持续储存文件到本地磁盘。
3.Arrylist对文件信息进行操作。
一、界面展示
本地储存学生信息的txt文件,可在此对学生信息增删改查但需要注意格式。
二、项目构架
三、相关代码及介绍
1.IO部分
学生实体类
package stuManager; public class StuInfo { private int stuId;// 学号 private String stuName;// 姓名 private int stuAge;// 年龄 private String stuPrefession;// 专业 private int stuHomeNumber;// 宿舍 // Alt+Shift+S快速创建构造方法: public StuInfo() { } public StuInfo(int stuId, String stuName, int stuAge, String stuPrefession, int stuHomeNumber) { this.stuId = stuId; this.stuName = stuName; this.stuAge = stuAge; this.stuPrefession = stuPrefession; this.stuHomeNumber = stuHomeNumber; } public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public String getStuPrefession() { return stuPrefession; } public void setStuPrefession(String stuPrefession) { this.stuPrefession = stuPrefession; } public int getStuHomeNumber() { return stuHomeNumber; } public void setStuHomeNumber(int stuHomeNumber) { this.stuHomeNumber = stuHomeNumber; } }
学生信息写入本地磁盘txt文件类
package stuManager; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import stuView.StuManagerFrm; public class ArrayListToFile { public static void main(String[] args) throws IOException { // 创建输出缓冲流对象 BufferedWriter bw = new BufferedWriter(new FileWriter("e://a.txt")); for (int i = 0; i < StuManagerFrm.list.size(); i++) { //向txt文件中写入 bw.write(StuManagerFrm.list.get(i).getStuId() + "," + StuManagerFrm.list.get(i).getStuName() + "," + StuManagerFrm.list.get(i).getStuAge() + "," + StuManagerFrm.list.get(i).getStuPrefession() + "," + StuManagerFrm.list.get(i).getStuHomeNumber()); bw.newLine(); bw.flush(); } // 释放资源 bw.close(); } }
读取本地磁盘txt文件信息类
package stuManager; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import stuView.StuAdd; import stuView.StuManagerFrm; public class FileToArrayList { public static boolean a = true; public static void main(String[] args) throws IOException { // 创建一个输入缓冲对象 BufferedReader br = new BufferedReader(new FileReader("e:\\a.txt")); String line; while ((line = br.readLine()) != null) { // br.read.line读一行 String[] strArray = line.split(","); StuInfo stu = new StuInfo(); stu.setStuId(Integer.parseInt(strArray[0])); stu.setStuName(strArray[1]); stu.setStuAge(Integer.parseInt(strArray[2])); stu.setStuPrefession(strArray[3]); stu.setStuHomeNumber(Integer.parseInt(strArray[4])); StuManagerFrm.list.add(stu); } // 释放资源 br.close(); } }
删除txt文件所有信息类
package stuManager; import java.io.*; public class ClearFile { //清空文件内容 public static void clearInfoForFile(String fileName) { File file = new File("e:\\a.txt"); try { if (!file.exists()) { file.createNewFile(); } FileWriter fileWriter = new FileWriter(file); fileWriter.write(""); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
2.Arrylist及窗口部分
登陆窗体(初始账号:admin 密码:123456)
package stuView; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; import javax.swing.JTextField; import javax.swing.JPasswordField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Color; public class StuLogin extends JFrame { static String user = "admin"; static int userPassword = 123456; static String userPassword0 = String.valueOf(userPassword); private JPanel contentPane; static JTextField textField; private JLabel lblNewLabel_2; static JPasswordField passwordField; private JButton btnNewButton; /** * 所有jframe窗体借用eclipse插件Windowbuilder绘制 * */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { StuLogin frame = new StuLogin(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public StuLogin() { setTitle("登陆"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 409, 252); contentPane = new JPanel(); contentPane.setBackground(Color.LIGHT_GRAY); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(114, 28, 165, 35); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(28, 73, 76, 35); contentPane.add(lblNewLabel_1); textField = new JTextField(); textField.setBounds(114, 80, 136, 21); contentPane.add(textField); textField.setColumns(10); lblNewLabel_2 = new JLabel("\u5BC6\u7801"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(28, 129, 76, 35); contentPane.add(lblNewLabel_2); passwordField = new JPasswordField(); passwordField.setBounds(114, 136, 136, 21); contentPane.add(passwordField); btnNewButton = new JButton("\u767B\u9646"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 登陆按钮监听 public void actionPerformed(ActionEvent e) { if (textField.getText().equals("admin") && passwordField.getText().equals(userPassword0)) { JOptionPane.showMessageDialog(null, "登陆成功!"); dispose(); StuManagerFrm.main(null); } else { JOptionPane.showMessageDialog(null, "账号或密码错误"); } } }); btnNewButton.setBounds(133, 182, 97, 23); contentPane.add(btnNewButton); } }
主界面
package stuView; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import stuManager.StuInfo; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import java.awt.event.ActionEvent; import javax.swing.JTextArea; import javax.swing.JScrollBar; import java.awt.ScrollPane; import javax.swing.JScrollPane; import java.awt.Color; public class StuManagerFrm extends JFrame { public static Listlist = new ArrayList ();// 创建静态类型的动态数组,方便调用 private JPanel contentPane; //有关面板窗体都设置为静态,方便调用 static JFrame frame; static JPanel passwordChangePanel; static JPanel stuAddPanel; static JPanel stuQueryPanel; static JPanel stuUpdatePanel; static JPanel stuDeletePanel; static JTextArea textArea; static JPanel userInfoPanel; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { frame = new StuManagerFrm(); frame.setResizable(false);// 窗体不可放大 frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame.(由windowbuilder绘制) */ public StuManagerFrm() { setTitle("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF"); setForeground(Color.WHITE); setBackground(Color.WHITE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 672, 440); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu mnNewMenu = new JMenu("\u7528\u6237\u64CD\u4F5C"); menuBar.add(mnNewMenu); JMenuItem mntmNewMenuItem = new JMenuItem("\u4FEE\u6539\u5BC6\u7801"); mntmNewMenuItem.addActionListener(new ActionListener() { // 完成修改密码监听 public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); passwordChangePanel = new UserNameChange(); passwordChangePanel.setBounds(0, 0, 230, 380); frame.getContentPane().add(passwordChangePanel); passwordChangePanel.updateUI(); } }); mnNewMenu.add(mntmNewMenuItem); JMenuItem mntmNewMenuItem_1 = new JMenuItem("\u9000\u51FA\u767B\u5F55"); mntmNewMenuItem_1.addActionListener(new ActionListener() { // 完成退出登陆监听: public void actionPerformed(ActionEvent e) { dispose(); JOptionPane.showMessageDialog(null, "退出成功请重新登陆!"); StuLogin.main(null); } }); mnNewMenu.add(mntmNewMenuItem_1); JMenuItem mntmNewMenuItem_8 = new JMenuItem("\u7528\u6237\u4FE1\u606F"); mntmNewMenuItem_8.addActionListener(new ActionListener() { // 用户信息监听 public void actionPerformed(ActionEvent e) { //有关移除非选定的面板持续并显示文本区(以下各个监听同理) StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); //显示当前选定元素对应的面板(以下各个监听同理) userInfoPanel = new UserInfo(); userInfoPanel.setBounds(0, 0, 230, 380); frame.getContentPane().add(userInfoPanel); userInfoPanel.updateUI(); UserInfo.lblNewLabel_2.setText(StuLogin.user); UserInfo.lblNewLabel_2_1.setText(String.valueOf(StuLogin.userPassword0)); } }); mnNewMenu.add(mntmNewMenuItem_8); JMenu mnNewMenu_1 = new JMenu("\u5B66\u751F\u4FE1\u606F\u64CD\u4F5C"); menuBar.add(mnNewMenu_1); JMenuItem mntmNewMenuItem_2 = new JMenuItem("\u6DFB\u52A0\u5B66\u751F\u4FE1\u606F"); mntmNewMenuItem_2.addActionListener(new ActionListener() { // 增添学生信息监听: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); stuAddPanel = new StuAdd(); stuAddPanel.setBounds(0, 0, 230, 380); frame.getContentPane().add(stuAddPanel); stuAddPanel.updateUI(); } }); mnNewMenu_1.add(mntmNewMenuItem_2); JMenuItem mntmNewMenuItem_3 = new JMenuItem("\u4FEE\u6539\u5B66\u751F\u4FE1\u606F"); mntmNewMenuItem_3.addActionListener(new ActionListener() { // 修改学生信息监听 public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); stuUpdatePanel = new StuUpdate(); stuUpdatePanel.setBounds(0, 0, 227, 380); frame.getContentPane().add(stuUpdatePanel); stuUpdatePanel.updateUI(); } }); mnNewMenu_1.add(mntmNewMenuItem_3); JMenuItem mntmNewMenuItem_4 = new JMenuItem("\u67E5\u8BE2\u5B66\u751F\u4FE1\u606F"); mntmNewMenuItem_4.addActionListener(new ActionListener() { // 查询学生信息监听: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); stuQueryPanel = new StuQuery(); stuQueryPanel.setBounds(0, 0, 230, 380); frame.getContentPane().add(stuQueryPanel); stuQueryPanel.updateUI(); } }); mnNewMenu_1.add(mntmNewMenuItem_4); JMenuItem mntmNewMenuItem_5 = new JMenuItem("\u5220\u9664\u5B66\u751F\u4FE1\u606F"); mntmNewMenuItem_5.addActionListener(new ActionListener() { // 删除学生信息监听: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().removeAll(); StuManagerFrm.frame.repaint(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); stuDeletePanel = new StuDelete(); stuDeletePanel.setBounds(0, 0, 230, 380); frame.getContentPane().add(stuDeletePanel); stuDeletePanel.updateUI(); } }); mnNewMenu_1.add(mntmNewMenuItem_5); JMenu mnNewMenu_2 = new JMenu("\u5173\u4E8E\u4F5C\u8005"); menuBar.add(mnNewMenu_2); JMenuItem mntmNewMenuItem_6 = new JMenuItem("\u8054\u7CFB\u6211\u4EEC"); mntmNewMenuItem_6.addActionListener(new ActionListener() { // 关于作者部分监听 public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "电话:" + "110"); } }); mnNewMenu_2.add(mntmNewMenuItem_6); JMenuItem mntmNewMenuItem_7 = new JMenuItem("\u8054\u7CFB\u5730\u5740"); mntmNewMenuItem_7.addActionListener(new ActionListener() { // 联系地址监听 public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "地址:" + "公安局"); } }); mnNewMenu_2.add(mntmNewMenuItem_7); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); // 向窗体添加文本区并添加滑轮 JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(228, 0, 424, 381); contentPane.add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); } }
修改密码面板
package stuView; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import java.awt.Font; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Color; public class UserNameChange extends JPanel { private JPasswordField passwordField; /** * Create the panel. */ public UserNameChange() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u8BF7\u8F93\u5165\u5BC6\u7801\uFF1A"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(0, 73, 99, 32); add(lblNewLabel); passwordField = new JPasswordField(); passwordField.setBounds(83, 79, 122, 21); add(passwordField); JLabel lblNewLabel_1 = new JLabel("\u4FEE\u6539\u5BC6\u7801"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(0, 10, 122, 32); add(lblNewLabel_1); JButton btnNewButton = new JButton("\u786E\u5B9A"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 确定修改密码监听 public void actionPerformed(ActionEvent e) { StuLogin.userPassword0 = passwordField.getText(); StuManagerFrm.frame.dispose(); JOptionPane.showMessageDialog(null, "修改成功,请重新登陆!"); StuLogin.main(null); } }); btnNewButton.setBounds(0, 127, 77, 23); add(btnNewButton); JButton btnNewButton_1 = new JButton("\u53D6\u6D88"); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.addActionListener(new ActionListener() { // 取消修改密码监听 public void actionPerformed(ActionEvent e) { // 删除修改密码面板 StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.passwordChangePanel); // 重画窗口 StuManagerFrm.frame.repaint(); } }); btnNewButton_1.setBounds(128, 127, 77, 23); add(btnNewButton_1); } }
用户信息面板
package stuView; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.Font; import java.awt.Color; public class UserInfo extends JPanel { static JLabel lblNewLabel_2; static JLabel lblNewLabel_2_1; /** * Create the panel. */ public UserInfo() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u5F53\u524D\u7528\u6237\u4FE1\u606F"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(10, 10, 114, 31); add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D\uFF1A"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(10, 79, 95, 31); add(lblNewLabel_1); JLabel lblNewLabel_1_1 = new JLabel("\u5BC6 \u7801\uFF1A"); lblNewLabel_1_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1_1.setBounds(10, 156, 95, 31); add(lblNewLabel_1_1); lblNewLabel_2 = new JLabel("New label"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(81, 83, 93, 23); add(lblNewLabel_2); lblNewLabel_2_1 = new JLabel("New label"); lblNewLabel_2_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2_1.setBounds(81, 160, 93, 23); add(lblNewLabel_2_1); } }
添加学生信息面板
package stuView; import javax.swing.JPanel; import java.awt.Color; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; import javax.swing.JTextField; import stuManager.ArrayListToFile; import stuManager.FileToArrayList; import stuManager.StuInfo; import javax.swing.JButton; import java.awt.event.ActionListener; import java.io.IOException; import java.awt.event.ActionEvent; public class StuAdd extends JPanel { static JTextField textField; static JTextField textField_1; static JTextField textField_2; static JTextField textField_3; static JTextField textField_4; /** * Create the panel. */ public StuAdd() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u5B66 \u53F7\uFF1A"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(10, 46, 78, 32); add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u59D3 \u540D\uFF1A"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(10, 88, 78, 32); add(lblNewLabel_1); JLabel lblNewLabel_2 = new JLabel("\u5E74 \u9F84\uFF1A"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(10, 130, 78, 32); add(lblNewLabel_2); JLabel lblNewLabel_3 = new JLabel("\u4E13 \u4E1A\uFF1A"); lblNewLabel_3.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_3.setBounds(10, 172, 78, 32); add(lblNewLabel_3); JLabel lblNewLabel_4 = new JLabel("\u5BBF \u820D \u53F7\uFF1A"); lblNewLabel_4.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_4.setBounds(10, 214, 90, 32); add(lblNewLabel_4); textField = new JTextField(); textField.setBounds(90, 52, 130, 21); add(textField); textField.setColumns(10); textField_1 = new JTextField(); textField_1.setColumns(10); textField_1.setBounds(90, 94, 130, 21); add(textField_1); textField_2 = new JTextField(); textField_2.setColumns(10); textField_2.setBounds(90, 136, 130, 21); add(textField_2); textField_3 = new JTextField(); textField_3.setColumns(10); textField_3.setBounds(90, 172, 130, 21); add(textField_3); textField_4 = new JTextField(); textField_4.setColumns(10); textField_4.setBounds(90, 220, 130, 21); add(textField_4); JButton btnNewButton = new JButton("\u786E\u5B9A"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 确定添加学生信息监听: public void actionPerformed(ActionEvent e) { // 实例化学生对象stu并添加信息 if (textField.getText() == null || textField_1.getText() == null|| textField_2.getText() == null && textField_3.getText() == null|| textField_4.getText() == null) { JOptionPane.showMessageDialog(null, "添加失败,请检查输入!"); } else { // 先把文件读取出来在进行添加 if (FileToArrayList.a == true) { try { FileToArrayList.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } FileToArrayList.a = false; StuInfo stu = new StuInfo(Integer.parseInt(textField.getText()), textField_1.getText(),Integer.parseInt(textField_2.getText()), textField_3.getText(),Integer.parseInt(textField_4.getText())); StuManagerFrm.list.add(stu); // 写入到文件 try { ArrayListToFile.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //关闭当前面板 StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuAddPanel); StuManagerFrm.frame.repaint(); JOptionPane.showMessageDialog(null, "添加成功!"); } } }); btnNewButton.setBounds(10, 275, 97, 23); add(btnNewButton); JButton btnNewButton_1 = new JButton("\u53D6\u6D88"); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.addActionListener(new ActionListener() { // 取消添加学生信息监听: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuAddPanel); // 重画窗口 StuManagerFrm.frame.repaint(); } }); btnNewButton_1.setBounds(123, 275, 97, 23); add(btnNewButton_1); JLabel lblNewLabel_5 = new JLabel("\u6DFB\u52A0\u5B66\u751F\u4FE1\u606F"); lblNewLabel_5.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_5.setBounds(10, 0, 130, 32); add(lblNewLabel_5); } }
修改学生信息面板
package stuView; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; import javax.swing.JTextField; import stuManager.ArrayListToFile; import stuManager.FileToArrayList; import javax.swing.JButton; import java.awt.event.ActionListener; import java.io.IOException; import java.awt.event.ActionEvent; import java.awt.Color; public class StuUpdate extends JPanel { private JTextField textField; private JTextField textField_1; private JTextField textField_2; private JTextField textField_3; private JTextField textField_4; /** * Create the panel. */ public StuUpdate() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u4F9D\u636E\u5B66\u53F7\u4FEE\u6539\uFF1A"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(0, 70, 106, 27); add(lblNewLabel); textField = new JTextField(); textField.setBounds(102, 73, 118, 21); add(textField); textField.setColumns(10); JLabel lblNewLabel_1 = new JLabel("\u4FEE\u6539\u540E\u7684\u59D3\u540D\uFF1A"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(0, 115, 106, 27); add(lblNewLabel_1); textField_1 = new JTextField(); textField_1.setColumns(10); textField_1.setBounds(102, 118, 118, 21); add(textField_1); JLabel lblNewLabel_1_1 = new JLabel("\u4FEE\u6539\u540E\u7684\u5E74\u9F84\uFF1A"); lblNewLabel_1_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1_1.setBounds(0, 165, 106, 27); add(lblNewLabel_1_1); JLabel lblNewLabel_1_1_1 = new JLabel("\u4FEE\u6539\u540E\u7684\u4E13\u4E1A\uFF1A"); lblNewLabel_1_1_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1_1_1.setBounds(0, 202, 106, 27); add(lblNewLabel_1_1_1); JLabel lblNewLabel_1_1_2 = new JLabel("\u4FEE\u6539\u540E\u7684\u5BDD\u5BA4\uFF1A"); lblNewLabel_1_1_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1_1_2.setBounds(0, 239, 106, 27); add(lblNewLabel_1_1_2); textField_2 = new JTextField(); textField_2.setColumns(10); textField_2.setBounds(102, 168, 118, 21); add(textField_2); textField_3 = new JTextField(); textField_3.setColumns(10); textField_3.setBounds(102, 202, 118, 21); add(textField_3); textField_4 = new JTextField(); textField_4.setColumns(10); textField_4.setBounds(102, 242, 118, 21); add(textField_4); JButton btnNewButton = new JButton("\u786E\u5B9A"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 确定修改学生信息 public void actionPerformed(ActionEvent e) { if (textField.getText() != null && textField_2.getText() != null && textField_1.getText() != null && textField_3.getText() != null || textField_4.getText() != null) { for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (Integer.parseInt(textField.getText()) == StuManagerFrm.list.get(i).getStuId()) { StuManagerFrm.list.get(i).setStuName(textField_1.getText()); StuManagerFrm.list.get(i).setStuAge(Integer.parseInt(textField_2.getText())); StuManagerFrm.list.get(i).setStuPrefession(textField_3.getText()); StuManagerFrm.list.get(i).setStuHomeNumber(Integer.parseInt(textField_4.getText())); } } JOptionPane.showMessageDialog(null, "修改成功!"); try { ArrayListToFile.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane.showMessageDialog(null, "修改失败,请检查输入!"); } StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuUpdatePanel); StuManagerFrm.frame.repaint(); } }); btnNewButton.setBounds(10, 298, 97, 23); add(btnNewButton); JButton btnNewButton_1 = new JButton("\u53D6\u6D88"); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.addActionListener(new ActionListener() { // 取消修改监听: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuUpdatePanel); StuManagerFrm.frame.repaint(); } }); btnNewButton_1.setBounds(123, 298, 97, 23); add(btnNewButton_1); JLabel lblNewLabel_2 = new JLabel("\u5B66\u751F\u4FE1\u606F\u4FEE\u6539"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(0, 10, 106, 27); add(lblNewLabel_2); } }
查询学生信息面板
package stuView; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JLabel; import javax.swing.JTextField; import stuManager.FileToArrayList; import javax.swing.JButton; import java.awt.event.ActionListener; import java.io.IOException; import java.awt.event.ActionEvent; import java.awt.Color; import java.awt.Font; public class StuQuery extends JPanel { private JTextField textField; private JTextField textField_1; private JTextField textField_2; private JTextField textField_3; private JTextField textField_4; /** * Create the panel. */ public StuQuery() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u8F93\u5165\u5B66\u53F7\u67E5\u8BE2:"); lblNewLabel.setBounds(0, 78, 86, 34); add(lblNewLabel); textField = new JTextField(); textField.setBounds(81, 85, 57, 21); add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("\u67E5\u8BE2"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 学号查询监听: public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "专业" + "\t" + "宿舍号" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (StuManagerFrm.list.get(i).getStuId() == (Integer.parseInt(textField.getText()))) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 学号 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年龄 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 专业 StuManagerFrm.textArea .append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍号 StuManagerFrm.textArea.append("\n"); } } } }); btnNewButton.setBounds(140, 84, 73, 23); add(btnNewButton); JButton btnNewButton_1 = new JButton("\u5168\u90E8\u67E5\u8BE2"); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.addActionListener(new ActionListener() { // 全部查询监听: public void actionPerformed(ActionEvent e) { if (FileToArrayList.a == true) { try { FileToArrayList.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } FileToArrayList.a = false; StuManagerFrm.textArea.setText("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "专业" + "\t" + "宿舍号" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 学号 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年龄 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 专业 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍号 StuManagerFrm.textArea.append("\n"); } } }); btnNewButton_1.setBounds(10, 297, 97, 23); add(btnNewButton_1); JLabel lblNewLabel_1 = new JLabel("\u8F93\u5165\u59D3\u540D\u67E5\u8BE2:"); lblNewLabel_1.setBounds(0, 122, 86, 34); add(lblNewLabel_1); textField_1 = new JTextField(); textField_1.setColumns(10); textField_1.setBounds(81, 129, 57, 21); add(textField_1); JButton btnNewButton_2 = new JButton("\u67E5\u8BE2"); btnNewButton_2.setBackground(Color.LIGHT_GRAY); btnNewButton_2.addActionListener(new ActionListener() { // 姓名查询监听: public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "专业" + "\t" + "宿舍号" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (StuManagerFrm.list.get(i).getStuName().equals(textField_1.getText())) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 学号 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年龄 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 专业 StuManagerFrm.textArea .append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍号 StuManagerFrm.textArea.append("\n"); } } } }); btnNewButton_2.setBounds(140, 128, 73, 23); add(btnNewButton_2); JLabel lblNewLabel_1_1 = new JLabel("\u8F93\u5165\u5E74\u9F84\u67E5\u8BE2:"); lblNewLabel_1_1.setBounds(0, 166, 86, 34); add(lblNewLabel_1_1); textField_2 = new JTextField(); textField_2.setColumns(10); textField_2.setBounds(81, 173, 57, 21); add(textField_2); JButton btnNewButton_2_1 = new JButton("\u67E5\u8BE2"); btnNewButton_2_1.setBackground(Color.LIGHT_GRAY); btnNewButton_2_1.addActionListener(new ActionListener() { // 年龄查询监听: public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "专业" + "\t" + "宿舍号" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (StuManagerFrm.list.get(i).getStuAge() == (Integer.parseInt(textField_2.getText()))) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 学号 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年龄 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 专业 StuManagerFrm.textArea .append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍号 StuManagerFrm.textArea.append("\n"); } } } }); btnNewButton_2_1.setBounds(140, 172, 73, 23); add(btnNewButton_2_1); JLabel lblNewLabel_1_1_1 = new JLabel("\u8F93\u5165\u4E13\u4E1A\u67E5\u8BE2:"); lblNewLabel_1_1_1.setBounds(0, 205, 86, 34); add(lblNewLabel_1_1_1); textField_3 = new JTextField(); textField_3.setColumns(10); textField_3.setBounds(81, 212, 57, 21); add(textField_3); JButton btnNewButton_2_1_1 = new JButton("\u67E5\u8BE2"); btnNewButton_2_1_1.setBackground(Color.LIGHT_GRAY); btnNewButton_2_1_1.addActionListener(new ActionListener() { // 专业查询监听 public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "专业" + "\t" + "宿舍号" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (StuManagerFrm.list.get(i).getStuPrefession().equals(textField_3.getText())) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 学号 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年龄 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 专业 StuManagerFrm.textArea .append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍号 StuManagerFrm.textArea.append("\n"); } } } }); btnNewButton_2_1_1.setBounds(140, 211, 73, 23); add(btnNewButton_2_1_1); JLabel lblNewLabel_1_1_1_1 = new JLabel("\u8F93\u5165\u5BDD\u5BA4\u67E5\u8BE2:"); lblNewLabel_1_1_1_1.setBounds(0, 242, 86, 34); add(lblNewLabel_1_1_1_1); textField_4 = new JTextField(); textField_4.setColumns(10); textField_4.setBounds(81, 249, 57, 21); add(textField_4); JButton btnNewButton_2_1_1_1 = new JButton("\u67E5\u8BE2"); btnNewButton_2_1_1_1.setBackground(Color.LIGHT_GRAY); btnNewButton_2_1_1_1.addActionListener(new ActionListener() { // 宿舍号查询监听: public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "专业" + "\t" + "宿舍号" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (StuManagerFrm.list.get(i).getStuHomeNumber() == (Integer.parseInt(textField_4.getText()))) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 学号 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年龄 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 专业 StuManagerFrm.textArea .append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍号 StuManagerFrm.textArea.append("\n"); } } } }); btnNewButton_2_1_1_1.setBounds(140, 248, 73, 23); add(btnNewButton_2_1_1_1); JButton btnNewButton_1_1 = new JButton("\u53D6\u6D88\u67E5\u8BE2"); btnNewButton_1_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1_1.addActionListener(new ActionListener() { // 取消查询: public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuQueryPanel); StuManagerFrm.frame.repaint(); } }); btnNewButton_1_1.setBounds(116, 297, 97, 23); add(btnNewButton_1_1); JLabel lblNewLabel_2 = new JLabel("\u5B66\u751F\u4FE1\u606F\u67E5\u8BE2"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(10, 10, 114, 34); add(lblNewLabel_2); } }
删除学生信息面板
package stuView; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import stuManager.ArrayListToFile; import stuManager.ClearFile; import javax.swing.JButton; import java.awt.Font; import java.awt.event.ActionListener; import java.io.IOException; import java.awt.event.ActionEvent; import java.awt.Color; public class StuDelete extends JPanel { private JTextField textField; private JTextField textField_1; /** * Create the panel. */ public StuDelete() { setBackground(Color.LIGHT_GRAY); setLayout(null); JLabel lblNewLabel = new JLabel("\u901A\u8FC7\u5B66\u53F7\u5220\u9664\uFF1A"); lblNewLabel.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel.setBounds(0, 81, 119, 25); add(lblNewLabel); textField = new JTextField(); textField.setBounds(102, 83, 55, 21); add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("\u5220\u9664"); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.addActionListener(new ActionListener() { // 通过学号删除: public void actionPerformed(ActionEvent e) { boolean a = false;// 判断是否进行了删除操作 for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (Integer.parseInt(textField.getText()) == StuManagerFrm.list.get(i).getStuId()) { StuManagerFrm.list.remove(i); a = true; } } if (a == true) { JOptionPane.showMessageDialog(null, "删除成功!"); try { ArrayListToFile.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else JOptionPane.showMessageDialog(null, "删除失败,请确认学号是否正确!"); } }); btnNewButton.setBounds(157, 82, 63, 23); add(btnNewButton); JLabel lblNewLabel_1 = new JLabel("\u901A\u8FC7\u59D3\u540D\u5220\u9664\uFF1A"); lblNewLabel_1.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_1.setBounds(0, 125, 119, 25); add(lblNewLabel_1); textField_1 = new JTextField(); textField_1.setColumns(10); textField_1.setBounds(102, 127, 55, 21); add(textField_1); JButton btnNewButton_1 = new JButton("\u5220\u9664"); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.addActionListener(new ActionListener() { // 通过姓名删除监听 public void actionPerformed(ActionEvent e) { boolean a = false; for (int i = 0; i < StuManagerFrm.list.size(); i++) { if (textField_1.getText().equals(StuManagerFrm.list.get(i).getStuName())) { StuManagerFrm.list.remove(i); a = true; } } if (a == true) { JOptionPane.showMessageDialog(null, "删除成功!"); try { ArrayListToFile.main(null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else JOptionPane.showMessageDialog(null, "删除失败,请确认姓名是否正确!"); } }); btnNewButton_1.setBounds(157, 126, 63, 23); add(btnNewButton_1); JButton btnNewButton_2 = new JButton("\u5168\u90E8\u5220\u9664"); btnNewButton_2.setBackground(Color.LIGHT_GRAY); btnNewButton_2.addActionListener(new ActionListener() { // 全部删除监听 public void actionPerformed(ActionEvent e) { StuManagerFrm.list.clear(); ClearFile.clearInfoForFile(null); JOptionPane.showMessageDialog(null, "删除成功!"); } }); btnNewButton_2.setBounds(0, 253, 97, 23); add(btnNewButton_2); JButton btnNewButton_3 = new JButton("\u53D6\u6D88"); btnNewButton_3.setBackground(Color.LIGHT_GRAY); btnNewButton_3.addActionListener(new ActionListener() { // 取消删除监听 public void actionPerformed(ActionEvent e) { StuManagerFrm.frame.getContentPane().remove(StuManagerFrm.stuDeletePanel); StuManagerFrm.frame.repaint(); } }); btnNewButton_3.setBounds(123, 253, 97, 23); add(btnNewButton_3); JButton btnNewButton_4 = new JButton("\u5237\u65B0"); btnNewButton_4.setBackground(Color.LIGHT_GRAY); btnNewButton_4.addActionListener(new ActionListener() { // 删除页面刷新数据监听 public void actionPerformed(ActionEvent e) { StuManagerFrm.textArea.setText("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "专业" + "\t" + "宿舍号" + "\n"); for (int i = 0; i < StuManagerFrm.list.size(); i++) { StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuId()) + "\t");// 学号 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuName() + "\t");// 姓名 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuAge()) + "\t");// 年龄 StuManagerFrm.textArea.append(StuManagerFrm.list.get(i).getStuPrefession() + "\t");// 专业 StuManagerFrm.textArea.append(String.valueOf(StuManagerFrm.list.get(i).getStuHomeNumber()) + "\t");// 宿舍号 StuManagerFrm.textArea.append("\n"); } } }); btnNewButton_4.setBounds(123, 184, 97, 23); add(btnNewButton_4); JLabel lblNewLabel_2 = new JLabel("\u5B66\u751F\u4FE1\u606F\u5220\u9664"); lblNewLabel_2.setFont(new Font("SimSun", Font.PLAIN, 15)); lblNewLabel_2.setBounds(0, 22, 119, 25); add(lblNewLabel_2); } }
总结
1.增删改查的基本操作都能正常完成。
2.对于窗口界面部分的开发运用eclipse插件WindowBuilder对项目效果上有了很大的帮助,使得可以自由设计自己想要的界面。
3.设计框架不够清晰,部分学生信息操作在监听内完成。
4.没有采用数据库存储数据而是通过io流在本地完成对文件的存储。
改进点:
1.采用MVC设计方式完成本项目。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。