第12个java作业,断断续续的用闲散时间写了十多天才算完成,一共大概1470行,记得上学期的C++课程设计也是写了1400多行,写着写着就发现上了瘾,不是为了应付作业而写,而是想探索自己未知的知识领域。
前身是:学生信息排序管理(JAVA文件操作) 用图形界面进行了重构
这是文件流版,数据库版参见 【GUI程序设计】学生信息管理系统(数据库版)
注意事项:
1.默认把学生信息放置于C盘根目录下,因为位置特殊请以管理员模式启动程序和eclipse(否则会导致保存失败)
2.共1473行代码
3.时间仓促,有部分bug以及影响体验的项目未修复(例如修改后请手动点击读取记录进行刷新)
运行截图:
源代码:(层次由低到高)
一、后台操作包(studentAndFileAction包)
1.Student类(表示学生的基本数据单元)
package studentAndFileAction; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Student{ String stuNo,name; double grade; public Student() { stuNo=name=""; grade=0.0; } public Student(String stuNo,String name,String grade) { this.stuNo=stuNo; this.name=name; this.grade=Double.valueOf(grade); } public Student(String stuNo,String name,double grade) { this.stuNo=stuNo; this.name=name; this.grade=grade; } public String stuInfo(){ if(this.name.length()=="占位符".length()) return String.format("%-12s %-30s %s",this.stuNo,this.name,Double.toString(this.grade)); else return String.format("%-12s %-33s %s",this.stuNo,this.name,Double.toString(this.grade)); } }
2.StudentGroup类(表示学生集合的基本数据单元)
同时实现了增改查删的功能
package studentAndFileAction; import java.math.BigInteger; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class StudentGroup{ public List<Student> dataBase =new ArrayList<Student>(); public void add(Student stu) { try { dataBase.add(stu); } catch (NumberFormatException e) { System.out.println("学号格式错误,本记录作废"); } } public String modify(String no,String name,String grade) { for(int i=0;i<dataBase.size();i++) if(dataBase.get(i).stuNo.equals(no)) { dataBase.get(i).name=name; dataBase.get(i).grade=Double.valueOf(grade); return "修改成功\r\n新记录是:\r\n"+dataBase.get(i).stuInfo(); } return "找不到"; } //查询------------------------------------------------ public String lookup(String no,String name) {//精准查询 int index_ans[]=new int [dataBase.size()]; int index_num=0; for(int i=0;i<dataBase.size();i++) if(dataBase.get(i).stuNo.equals(no)&&dataBase.get(i).name.equals(name)) index_ans[index_num++]=i; if(index_num==0) return "找不到"; else { String re=""; for(int i=0;i<index_num;i++) re+=(dataBase.get(index_ans[i]).stuInfo()+"\r\n"); return re; } } public String lookup(String infomation,int num) {//模糊查询 num=1学号查询 2:名字查询 3:成绩查询 int index_ans[]=new int [dataBase.size()]; int index_num=0; if(num==1) {//学号查询 for(int i=0;i<dataBase.size();i++) if(dataBase.get(i).stuNo.equals(infomation)) index_ans[index_num++]=i; }else if(num==2) { for(int i=0;i<dataBase.size();i++) if(dataBase.get(i).name.equals(infomation)) index_ans[index_num++]=i; }else if(num==3) { for(int i=0;i<dataBase.size();i++) if(dataBase.get(i).grade==Double.valueOf(infomation)) index_ans[index_num++]=i; } if(index_num==0) return "找不到"; else { String re=""; for(int i=0;i<index_num;i++) re+=(dataBase.get(index_ans[i]).stuInfo()+"\r\n"); return re; } } //删除------------------------------------------------------- public String delete(String no,String name) {//精准删除 int index_ans[]=new int [dataBase.size()]; int index_num=0; for(int i=0;i<dataBase.size();i++) if(dataBase.get(i).stuNo.equals(no)&&dataBase.get(i).name.equals(name)) index_ans[index_num++]=i; if(index_num==0) return "找不到"; else { String re=""; for(int i=0;i<index_num;i++) { re+=(dataBase.get(index_ans[i]).stuInfo()+"\r\n"); dataBase.remove(index_ans[i]); } return re; } } public String delete(String infomation,int num) {//模糊删除 num=1学号删除 2:名字删除 3:成绩删除 int index_ans[]=new int [dataBase.size()]; int index_num=0; if(num==1) {//学号删除 for(int i=0;i<dataBase.size();i++) if(dataBase.get(i).stuNo.equals(infomation)) index_ans[index_num++]=i; }else if(num==2) { for(int i=0;i<dataBase.size();i++) if(dataBase.get(i).name.equals(infomation)) index_ans[index_num++]=i; }else if(num==3) { for(int i=0;i<dataBase.size();i++) if(dataBase.get(i).grade==Double.valueOf(infomation)) index_ans[index_num++]=i; } if(index_num==0) return "找不到"; else { String re=""; for(int i=0;i<index_num;i++) { re+=(dataBase.get(index_ans[i]).stuInfo()+"\r\n"); dataBase.remove(index_ans[i]); } return re; } } public void sortSub() { Collections.sort(dataBase, new Comparator<Student>() { public int compare(Student stu1, Student stu2) { if(stu1.grade<stu2.grade) return 1; else if(stu1.grade>stu2.grade) return -1; else return 0; } }); } public void sortAdd() { Collections.sort(dataBase, new Comparator<Student>() { public int compare(Student stu1, Student stu2) { if(stu1.grade>stu2.grade) return 1; else if(stu1.grade<stu2.grade) return -1; else return 0; } }); } }
3.User类(表示用户的单元)
同时实现了文件操作相关功能(或许叫文件类更合适,懒的改了)
package studentAndFileAction; 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; public class User { int load_num=0; File file; public StudentGroup studentGroup=new StudentGroup(); public String file_open() {//文件打开 try { file=new File("C:/student.txt"); return "==>找到学生信息记录存储文件,程序正常启动\r\n==>请按读取记录获取学生信息\r\n"; } catch (Exception e) { return "无法在指定路径找到学生记录\r\n请保证学生记录在C盘根目录下!\r\n"; } } public void file_load_reset() {//存储后标志复位 load_num=0; } public void file_load() {//记录加载 try { FileReader cin=new FileReader(file);//打开文件 if(load_num==0) { try { BufferedReader input=new BufferedReader(cin); while(true) { String oneStu=input.readLine(); if(oneStu==null) break; String []infomation=oneStu.split(" "); studentGroup.add(new Student(infomation[0],infomation[1],infomation[2])); } cin.close(); input.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 System.out.println("读写异常"); } load_num++; } }catch (FileNotFoundException e) { System.out.println("文件找不到,系统退出"); e.printStackTrace(); } } public String file_store() {//存储 try { FileWriter writer=new FileWriter(file); BufferedWriter write = new BufferedWriter(writer); for(Student stu:studentGroup.dataBase) { String out=stu.stuNo+" "+stu.name+" "+stu.grade; write.write(out); write.newLine(); } write.close(); writer.close(); studentGroup.dataBase.clear(); return "保存成功!!\r\n"; } catch (IOException e) { // TODO 自动生成的 catch 块 return "很遗憾,保存失败!!\r\n因为文件位置在C盘根目录下,请以管理员模式打开本程序\r\n"; } } }
二、图形界面设计(graphical 包)
1.主窗体:MainFrame.java(主窗口)
package graphical; import javax.swing.JOptionPane; import java.awt.EventQueue; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JTextField; import javax.swing.JLabel; import java.awt.Font; import javax.swing.JMenu; import java.awt.Panel; import java.awt.Label; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JDialog; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.SwingConstants; import java.awt.Color; import org.eclipse.wb.swing.FocusTraversalOnArray; import studentAndFileAction.Student; import studentAndFileAction.StudentGroup; import studentAndFileAction.User; import java.awt.Component; import javax.swing.ImageIcon; import javax.swing.JTable; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.DropMode; import javax.swing.JTextArea; import java.awt.Button; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; import javax.swing.Box; import java.awt.Toolkit; public class Mainframe extends JFrame { private JPanel contentPane; static User yuheng=new User(); static JTextArea screen = new JTextArea(); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Mainframe frame = new Mainframe(); frame.setVisible(true); screen.append(yuheng.file_open()); } catch (Exception e) { e.printStackTrace(); } } }); } public void display(JTextArea screen) { screen.setText(""); for(Student stu:yuheng.studentGroup.dataBase) { screen.append(stu.stuInfo()+"\r\n"); } } /** * Create the frame. */ public Mainframe() { setResizable(false); setIconImage(Toolkit.getDefaultToolkit().getImage("img\\\u56FE\u6807.jpg")); setTitle("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF-\u4F5C\u4E1A12 by \u4E8E\u8861~"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 971, 603); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JPanel Maintitle = new JPanel(); Maintitle.setBackground(Color.WHITE); Maintitle.setBounds(26, 10, 905, 95); contentPane.add(Maintitle); Maintitle.setLayout(null); JLabel label = new JLabel(""); label.setBounds(86, 0, 713, 95); Maintitle.add(label); label.setIcon(new ImageIcon(Mainframe.class.getResource("/img/\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF.png"))); label.setFont(new Font("楷体", Font.BOLD, 41)); JPanel name_input = new JPanel(); name_input.setBounds(26, 125, 905, 45); contentPane.add(name_input); name_input.setLayout(null); JLabel label_1 = new JLabel("\u4FE1\u606F\u663E\u793A\uFF1A"); label_1.setBounds(10, 4, 140, 38); name_input.add(label_1); label_1.setFont(new Font("微软雅黑", Font.BOLD, 28)); JLabel label_2 = new JLabel("\u529F\u80FD\u5217\u8868\uFF1A"); label_2.setFont(new Font("微软雅黑", Font.BOLD, 28)); label_2.setBounds(464, 4, 145, 38); name_input.add(label_2); JPanel key_area = new JPanel(); key_area.setBounds(487, 214, 444, 340); contentPane.add(key_area); key_area.setLayout(null); JButton read_info = new JButton("\u8BFB\u53D6\u8BB0\u5F55"); read_info.setBounds(0, 7, 215, 77); key_area.add(read_info); read_info.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { yuheng.file_load(); display(screen); } }); read_info.setFont(new Font("方正姚体", Font.PLAIN, 25)); read_info.setIcon(new ImageIcon(Mainframe.class.getResource("/img/\u8BFB\u53D6\u6587\u4EF6.png"))); read_info.setBackground(Color.WHITE); JButton store_info = new JButton("\u4FDD\u5B58\u8BB0\u5F55"); store_info.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { screen.append(yuheng.file_store()); yuheng.file_load_reset();//重新可读 } }); store_info.setFont(new Font("方正姚体", Font.PLAIN, 25)); store_info.setBackground(Color.WHITE); store_info.setIcon(new ImageIcon(Mainframe.class.getResource("/img/\u4FDD\u5B58\u6587\u4EF6.png"))); store_info.setBounds(229, 7, 215, 77); key_area.add(store_info); JButton info_up = new JButton("\u5347\u5E8F\u6392\u5217"); info_up.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { yuheng.studentGroup.sortAdd(); display(screen); } }); info_up.setIcon(new ImageIcon(Mainframe.class.getResource("/img/\u5347\u5E8F.png"))); info_up.setFont(new Font("方正姚体", Font.PLAIN, 25)); info_up.setBackground(Color.WHITE); info_up.setBounds(0, 94, 215, 72); key_area.add(info_up); JButton info_down = new JButton("\u964D\u5E8F\u6392\u5217"); info_down.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { yuheng.studentGroup.sortSub(); display(screen); } }); info_down.setIcon(new ImageIcon(Mainframe.class.getResource("/img/\u964D\u5E8F.png"))); info_down.setFont(new Font("方正姚体", Font.PLAIN, 25)); info_down.setBackground(Color.WHITE); info_down.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { yuheng.studentGroup.sortSub(); display(screen); } }); info_down.setBounds(229, 94, 215, 70); key_area.add(info_down); JButton add_info = new JButton("\u589E\u52A0\u8BB0\u5F55"); add_info.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { new add_students(Mainframe.this).setVisible(true); } }); add_info.setIcon(new ImageIcon(Mainframe.class.getResource("/img/\u589E\u52A0.png"))); add_info.setBackground(Color.WHITE); add_info.setFont(new Font("方正姚体", Font.PLAIN, 25)); add_info.setBounds(0, 176, 215, 72); key_area.add(add_info); JButton delete_info = new JButton("\u5220\u9664\u8BB0\u5F55"); delete_info.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { new Delete_student(Mainframe.this).setVisible(true); } }); delete_info.setIcon(new ImageIcon(Mainframe.class.getResource("/img/\u5220\u9664.png"))); delete_info.setBackground(Color.WHITE); delete_info.setFont(new Font("方正姚体", Font.PLAIN, 25)); delete_info.setBounds(229, 176, 215, 72); key_area.add(delete_info); JButton query_info = new JButton("\u67E5\u627E\u8BB0\u5F55"); query_info.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { new Query_students(Mainframe.this).setVisible(true); } }); query_info.setIcon(new ImageIcon(Mainframe.class.getResource("/img/\u67E5\u8BE2.png"))); query_info.setBackground(Color.WHITE); query_info.setFont(new Font("方正姚体", Font.PLAIN, 25)); query_info.setBounds(0, 258, 215, 72); key_area.add(query_info); JButton modify_info = new JButton("\u4FEE\u6539\u8BB0\u5F55"); modify_info.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { new Alter_students_info(Mainframe.this).setVisible(true); } }); modify_info.setIcon(new ImageIcon(Mainframe.class.getResource("/img/\u4FEE\u6539.png"))); modify_info.setBackground(Color.WHITE); modify_info.setForeground(Color.BLACK); modify_info.setFont(new Font("方正姚体", Font.PLAIN, 25)); modify_info.setBounds(229, 258, 215, 72); key_area.add(modify_info); JPanel panel = new JPanel(); panel.setBounds(26, 180, 444, 374); contentPane.add(panel); panel.setLayout(null); JLabel info = new JLabel("\u5B66\u53F7 \u59D3\u540D \u6210\u7EE9"); info.setBounds(0, 10, 343, 24); info.setHorizontalAlignment(SwingConstants.CENTER); panel.add(info); info.setFont(new Font("微软雅黑", Font.PLAIN, 18)); info.setForeground(Color.BLACK); info.setBackground(Color.WHITE); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(0, 38, 444, 327); panel.add(scrollPane); scrollPane.setViewportView(screen); //JTextArea screen = new JTextArea(); screen.setFont(new Font("微软雅黑", Font.PLAIN, 18)); screen.setEditable(false); screen.setLineWrap(true); JLabel lblNewLabel = new JLabel("New label"); lblNewLabel.setBounds(312, 180, 294, 251); contentPane.add(lblNewLabel); } }
2.添加学生 Add_students.java(对话框)
package graphical; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import studentAndFileAction.Student; import javax.swing.JLabel; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.ImageIcon; import javax.swing.JTextArea; public class add_students extends JDialog { private final JPanel contentPanel = new JPanel(); private JTextField no; private JTextField name; private JTextField grade; JTextArea status = new JTextArea(); /** * Launch the application. */ public static void main(Mainframe gra) { try { add_students dialog = new add_students(gra); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); dialog.setLocation(392, 180); } catch (Exception e) { e.printStackTrace(); } } /** * Create the dialog. */ public add_students(Mainframe gra) { setResizable(false); getContentPane().setEnabled(false); this.setTitle("请输入需要添加学生的基本信息"); setBounds(100, 100, 549, 332); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); contentPanel.setLayout(null); { JButton okButton = new JButton("确定"); okButton.setFont(new Font("幼圆", Font.PLAIN, 28)); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { status.setText(""); if(no.getText().trim().equals("")||name.getText().trim().equals("")||grade.getText().trim().equals("")){ status.setText("数据非法!"); return; } Student stu=new Student(no.getText().trim(),name.getText().trim(),grade.getText().trim()); gra.yuheng.studentGroup.add(stu); status.setText("操作成功!"); } }); okButton.setBounds(261, 201, 96, 40); contentPanel.add(okButton); okButton.setActionCommand("OK"); getRootPane().setDefaultButton(okButton); } { JButton cancelButton = new JButton("清空"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { no.setText(""); grade.setText(""); name.setText(""); status.setText(""); } }); cancelButton.setFont(new Font("幼圆", Font.PLAIN, 28)); cancelButton.setBounds(413, 202, 102, 39); contentPanel.add(cancelButton); cancelButton.setActionCommand("Cancel"); } JPanel panel_4 = new JPanel(); panel_4.setBounds(22, 23, 216, 247); contentPanel.add(panel_4); panel_4.setLayout(null); JPanel stuName = new JPanel(); stuName.setBounds(0, 69, 216, 59); panel_4.add(stuName); stuName.setLayout(null); JLabel lblNewLabel_1 = new JLabel("\u59D3\u540D"); lblNewLabel_1.setBounds(0, 0, 61, 59); stuName.add(lblNewLabel_1); lblNewLabel_1.setBackground(Color.WHITE); lblNewLabel_1.setFont(new Font("方正姚体", Font.PLAIN, 25)); name = new JTextField(); name.setFont(new Font("宋体", Font.BOLD, 19)); name.setBounds(63, 14, 153, 33); stuName.add(name); name.setColumns(10); JPanel stuGrade = new JPanel(); stuGrade.setBounds(0, 124, 216, 59); panel_4.add(stuGrade); stuGrade.setLayout(null); JLabel lblNewLabel_2 = new JLabel("\u6210\u7EE9"); lblNewLabel_2.setBounds(0, 0, 50, 59); stuGrade.add(lblNewLabel_2); lblNewLabel_2.setBackground(Color.WHITE); lblNewLabel_2.setFont(new Font("方正姚体", Font.PLAIN, 25)); grade = new JTextField(); grade.setFont(new Font("宋体", Font.BOLD, 19)); grade.setBounds(63, 19, 153, 33); stuGrade.add(grade); grade.setColumns(10); JPanel stuNO = new JPanel(); stuNO.setBounds(0, 10, 216, 49); panel_4.add(stuNO); stuNO.setLayout(null); JLabel lblNewLabel = new JLabel("\u5B66\u53F7"); lblNewLabel.setBounds(0, 0, 81, 49); stuNO.add(lblNewLabel); lblNewLabel.setBackground(Color.WHITE); lblNewLabel.setFont(new Font("方正姚体", Font.PLAIN, 25)); no = new JTextField(); no.setFont(new Font("宋体", Font.BOLD, 19)); no.setBounds(64, 10, 152, 33); stuNO.add(no); no.setColumns(10); JPanel panel = new JPanel(); panel.setBounds(0, 193, 216, 44); panel_4.add(panel); panel.setLayout(null); status.setForeground(Color.BLACK); status.setFont(new Font("幼圆", Font.BOLD, 18)); status.setEditable(false); status.setBounds(64, 11, 152, 27); panel.add(status); JLabel label = new JLabel("\u72B6\u6001"); label.setBounds(0, 0, 64, 40); panel.add(label); label.setBackground(Color.WHITE); label.setFont(new Font("方正姚体", Font.PLAIN, 25)); JLabel lblNewLabel_3 = new JLabel(""); lblNewLabel_3.setIcon(new ImageIcon(add_students.class.getResource("/img/\u8F93\u5165.png"))); lblNewLabel_3.setBounds(291, 23, 181, 165); contentPanel.add(lblNewLabel_3); } }
3.删除记录 Delete_students.java(对话框)
package graphical; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import studentAndFileAction.Student; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.ImageIcon; import javax.swing.JTextArea; import javax.swing.SwingConstants; public class Delete_student extends JDialog { private final JPanel contentPanel = new JPanel(); private JTextField no; private JTextField name; private JTextField grade; JTextArea status = new JTextArea(); JTextArea screen = new JTextArea( "\r\n" + "注意:本程序提供精确删除和模糊删除两种删除方式 \r\n" + "1.精准删除模式\r\n" + " 输入学号,姓名等两项参数,返回删除结果。\r\n" + "2.模糊删除模式\r\n" + " 输入学号,姓名,成绩中的任意一个参数,返回全部的删除结果\r\n" + " ---------------------------------------\r\n" + " 建议在删除前进行查询操作,避免误删除(删除操作不可撤销)\r\n"); /** * Launch the application. */ public static void main(Mainframe gra) { try { Delete_student dialog = new Delete_student(gra); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); dialog.setLocation(392, 180); } catch (Exception e) { e.printStackTrace(); } } /** * Create the dialog. */ public Delete_student(Mainframe gra) { setResizable(false); this.setTitle("\u8BF7\u8F93\u5165\u9700\u8981\u5220\u9664\u5B66\u751F\u7684\u57FA\u672C\u4FE1\u606F"); setBounds(100, 100, 875, 397); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); { JButton delete = new JButton("\u5220\u9664"); delete.setBounds(620, 290, 90, 39); delete.setFont(new Font("幼圆", Font.PLAIN, 20)); delete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { status.setText(""); if(no.getText().trim().equals("")&&name.getText().trim().equals("")&&grade.getText().trim().equals("")){ status.setText("请输入数据"); return; }else if((!no.getText().trim().equals(""))&&!(name.getText().trim().equals(""))) { screen.setText(gra.yuheng.studentGroup.delete(no.getText().trim(),name.getText().trim())); status.setText("删除成功!"); } else if(!no.getText().trim().equals("")) { screen.setText(gra.yuheng.studentGroup.delete(no.getText().trim(),1)); status.setText("删除成功!"); } else if(!name.getText().trim().equals("")){ screen.setText(gra.yuheng.studentGroup.delete(name.getText().trim(),2)); status.setText("删除成功!"); } else if(!grade.getText().trim().equals("")){ screen.setText(gra.yuheng.studentGroup.delete(grade.getText().trim(),3)); status.setText("删除成功!"); } } }); contentPanel.setLayout(null); contentPanel.add(delete); delete.setActionCommand("OK"); getRootPane().setDefaultButton(delete); } { JButton cancelButton = new JButton("清空"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { no.setText(""); grade.setText(""); name.setText(""); status.setText(""); screen.setText(""); } }); cancelButton.setBounds(720, 290, 90, 39); cancelButton.setFont(new Font("幼圆", Font.PLAIN, 20)); contentPanel.add(cancelButton); cancelButton.setActionCommand("Cancel"); } JPanel panel_4 = new JPanel(); panel_4.setBounds(526, 27, 284, 247); contentPanel.add(panel_4); panel_4.setLayout(null); JPanel stuName = new JPanel(); stuName.setBounds(0, 69, 284, 59); panel_4.add(stuName); stuName.setLayout(null); JLabel lblNewLabel_1 = new JLabel("\u59D3\u540D"); lblNewLabel_1.setBounds(0, 0, 61, 59); stuName.add(lblNewLabel_1); lblNewLabel_1.setBackground(Color.WHITE); lblNewLabel_1.setFont(new Font("方正姚体", Font.PLAIN, 25)); name = new JTextField(); name.setFont(new Font("宋体", Font.BOLD, 19)); name.setBounds(63, 14, 211, 33); stuName.add(name); name.setColumns(10); JPanel stuGrade = new JPanel(); stuGrade.setBounds(0, 124, 284, 59); panel_4.add(stuGrade); stuGrade.setLayout(null); JLabel lblNewLabel_2 = new JLabel("\u6210\u7EE9"); lblNewLabel_2.setBounds(0, 0, 50, 59); stuGrade.add(lblNewLabel_2); lblNewLabel_2.setBackground(Color.WHITE); lblNewLabel_2.setFont(new Font("方正姚体", Font.PLAIN, 25)); grade = new JTextField(); grade.setFont(new Font("宋体", Font.BOLD, 19)); grade.setBounds(63, 19, 211, 33); stuGrade.add(grade); grade.setColumns(10); JPanel stuNO = new JPanel(); stuNO.setBounds(0, 10, 284, 49); panel_4.add(stuNO); stuNO.setLayout(null); JLabel lblNewLabel = new JLabel("\u5B66\u53F7"); lblNewLabel.setBounds(0, 0, 81, 49); stuNO.add(lblNewLabel); lblNewLabel.setBackground(Color.WHITE); lblNewLabel.setFont(new Font("方正姚体", Font.PLAIN, 25)); no = new JTextField(); no.setFont(new Font("宋体", Font.BOLD, 19)); no.setBounds(64, 10, 210, 33); stuNO.add(no); no.setColumns(10); JPanel panel = new JPanel(); panel.setBounds(0, 193, 284, 44); panel_4.add(panel); panel.setLayout(null); status.setForeground(Color.BLACK); status.setFont(new Font("幼圆", Font.BOLD, 18)); status.setEnabled(false); status.setEditable(false); status.setBounds(64, 11, 210, 27); panel.add(status); JLabel label = new JLabel("\u72B6\u6001"); label.setBounds(0, 0, 64, 40); panel.add(label); label.setBackground(Color.WHITE); label.setFont(new Font("方正姚体", Font.PLAIN, 25)); screen.setFont(new Font("微软雅黑", Font.PLAIN, 18)); screen.setEditable(false); screen.setLineWrap(true); screen.setBounds(10, 58, 458, 271); contentPanel.add(screen); JLabel label_1 = new JLabel("\u5B66\u53F7 \u59D3\u540D \u6210\u7EE9"); label_1.setHorizontalAlignment(SwingConstants.LEFT); label_1.setForeground(Color.BLACK); label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); label_1.setBackground(Color.WHITE); label_1.setBounds(10, 27, 458, 24); contentPanel.add(label_1); JButton button = new JButton("\u67E5\u8BE2"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { status.setText(""); if(no.getText().trim().equals("")&&name.getText().trim().equals("")&&grade.getText().trim().equals("")){ status.setText("请输入数据"); return; }else if((!no.getText().trim().equals(""))&&!(name.getText().trim().equals(""))) { screen.setText(gra.yuheng.studentGroup.lookup(no.getText().trim(),name.getText().trim())); status.setText("查询成功!"); } else if(!no.getText().trim().equals("")) { screen.setText(gra.yuheng.studentGroup.lookup(no.getText().trim(),1)); status.setText("查询成功!"); } else if(!name.getText().trim().equals("")){ screen.setText(gra.yuheng.studentGroup.lookup(name.getText().trim(),2)); status.setText("查询成功!"); } else if(!grade.getText().trim().equals("")){ screen.setText(gra.yuheng.studentGroup.lookup(grade.getText().trim(),3)); status.setText("查询成功!"); } } }); button.setFont(new Font("幼圆", Font.PLAIN, 20)); button.setActionCommand("OK"); button.setBounds(526, 290, 84, 39); contentPanel.add(button); } }
4.查询记录 Query_students.java(对话框)
package graphical; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import studentAndFileAction.Student; import javax.swing.JLabel; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.ImageIcon; import javax.swing.JTextArea; import javax.swing.SwingConstants; public class Query_students extends JDialog { private final JPanel contentPanel = new JPanel(); private JTextField no; private JTextField name; private JTextField grade; JTextArea status = new JTextArea(); JTextArea screen = new JTextArea( "\r\n\r\n" + "注意:本程序提供精确匹配和模糊匹配两种查询方式 \r\n" + "1.精准匹配模式\r\n" + " 输入学号,姓名等两项参数,返回查询结果。\r\n" + "2.模糊匹配模式\r\n" + " 输入学号,姓名,成绩中的任意一个参数,返回全部的查询结果\r\n"); /** * Launch the application. */ public static void main(Mainframe gra) { try { Query_students dialog = new Query_students(gra); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); dialog.setLocation(392, 180); } catch (Exception e) { e.printStackTrace(); } } /** * Create the dialog. */ public Query_students(Mainframe gra) { setResizable(false); this.setTitle("\u8BF7\u8F93\u5165\u9700\u8981\u67E5\u8BE2\u5B66\u751F\u7684\u57FA\u672C\u4FE1\u606F"); setBounds(100, 100, 763, 390); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); { JButton okButton = new JButton("确定"); okButton.setBounds(499, 289, 96, 40); okButton.setFont(new Font("幼圆", Font.PLAIN, 28)); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { status.setText(""); if(no.getText().trim().equals("")&&name.getText().trim().equals("")&&grade.getText().trim().equals("")){ status.setText("请输入数据"); return; }else if((!no.getText().trim().equals(""))&&!(name.getText().trim().equals(""))) { screen.setText(gra.yuheng.studentGroup.lookup(no.getText().trim(),name.getText().trim())); status.setText("查询成功!"); } else if(!no.getText().trim().equals("")) { screen.setText(gra.yuheng.studentGroup.lookup(no.getText().trim(),1)); status.setText("查询成功!"); } else if(!name.getText().trim().equals("")){ screen.setText(gra.yuheng.studentGroup.lookup(name.getText().trim(),2)); status.setText("查询成功!"); } else if(!grade.getText().trim().equals("")){ screen.setText(gra.yuheng.studentGroup.lookup(grade.getText().trim(),3)); status.setText("查询成功!"); } } }); contentPanel.setLayout(null); contentPanel.add(okButton); okButton.setActionCommand("OK"); getRootPane().setDefaultButton(okButton); } { JButton cancelButton = new JButton("清空"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { no.setText(""); grade.setText(""); name.setText(""); status.setText(""); screen.setText(""); } }); cancelButton.setBounds(635, 290, 102, 39); cancelButton.setFont(new Font("幼圆", Font.PLAIN, 28)); contentPanel.add(cancelButton); cancelButton.setActionCommand("Cancel"); } JPanel panel_4 = new JPanel(); panel_4.setBounds(502, 27, 216, 247); contentPanel.add(panel_4); panel_4.setLayout(null); JPanel stuName = new JPanel(); stuName.setBounds(0, 69, 216, 59); panel_4.add(stuName); stuName.setLayout(null); JLabel lblNewLabel_1 = new JLabel("\u59D3\u540D"); lblNewLabel_1.setBounds(0, 0, 61, 59); stuName.add(lblNewLabel_1); lblNewLabel_1.setBackground(Color.WHITE); lblNewLabel_1.setFont(new Font("方正姚体", Font.PLAIN, 25)); name = new JTextField(); name.setFont(new Font("宋体", Font.BOLD, 19)); name.setBounds(63, 14, 153, 33); stuName.add(name); name.setColumns(10); JPanel stuGrade = new JPanel(); stuGrade.setBounds(0, 124, 216, 59); panel_4.add(stuGrade); stuGrade.setLayout(null); JLabel lblNewLabel_2 = new JLabel("\u6210\u7EE9"); lblNewLabel_2.setBounds(0, 0, 50, 59); stuGrade.add(lblNewLabel_2); lblNewLabel_2.setBackground(Color.WHITE); lblNewLabel_2.setFont(new Font("方正姚体", Font.PLAIN, 25)); grade = new JTextField(); grade.setFont(new Font("宋体", Font.BOLD, 19)); grade.setBounds(63, 19, 153, 33); stuGrade.add(grade); grade.setColumns(10); JPanel stuNO = new JPanel(); stuNO.setBounds(0, 10, 216, 49); panel_4.add(stuNO); stuNO.setLayout(null); JLabel lblNewLabel = new JLabel("\u5B66\u53F7"); lblNewLabel.setBounds(0, 0, 81, 49); stuNO.add(lblNewLabel); lblNewLabel.setBackground(Color.WHITE); lblNewLabel.setFont(new Font("方正姚体", Font.PLAIN, 25)); no = new JTextField(); no.setFont(new Font("宋体", Font.BOLD, 19)); no.setBounds(64, 10, 152, 33); stuNO.add(no); no.setColumns(10); JPanel panel = new JPanel(); panel.setBounds(0, 193, 216, 44); panel_4.add(panel); panel.setLayout(null); status.setForeground(Color.BLACK); status.setFont(new Font("幼圆", Font.BOLD, 18)); status.setEnabled(false); status.setEditable(false); status.setBounds(64, 11, 152, 27); panel.add(status); JLabel label = new JLabel("\u72B6\u6001"); label.setBounds(0, 0, 64, 40); panel.add(label); label.setBackground(Color.WHITE); label.setFont(new Font("方正姚体", Font.PLAIN, 25)); screen.setFont(new Font("微软雅黑", Font.PLAIN, 18)); screen.setEditable(false); screen.setLineWrap(true); screen.setBounds(10, 58, 458, 271); contentPanel.add(screen); JLabel label_1 = new JLabel("\u5B66\u53F7 \u59D3\u540D \u6210\u7EE9"); label_1.setHorizontalAlignment(SwingConstants.LEFT); label_1.setForeground(Color.BLACK); label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); label_1.setBackground(Color.WHITE); label_1.setBounds(10, 27, 458, 24); contentPanel.add(label_1); } }
5.修改记录 Alter_students_info.java(对话框)
package graphical; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import studentAndFileAction.Student; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.ImageIcon; import javax.swing.JTextArea; import javax.swing.SwingConstants; public class Alter_students_info extends JDialog { private final JPanel contentPanel = new JPanel(); private JTextField no; private JTextField name; private JTextField grade; JTextArea status = new JTextArea(); JTextArea screen = new JTextArea( "\r\n" + "注意:对学生信息进行修改 \r\n" + "使用方法\r\n" + " 输入学号,再输入姓名和成绩,把学号对应的信息修改\r\n" + " 建议在修改前进行查询操作,避免误修改(修改操作不可撤销)\r\n"); /** * Launch the application. */ public static void main(Mainframe gra) { try { Alter_students_info dialog = new Alter_students_info(gra); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); dialog.setLocation(392, 180); } catch (Exception e) { e.printStackTrace(); } } /** * Create the dialog. */ public Alter_students_info(Mainframe gra) { setResizable(false); this.setTitle("\u8BF7\u8F93\u5165\u9700\u8981\u4FEE\u6539\u5B66\u751F\u7684\u57FA\u672C\u4FE1\u606F"); setBounds(100, 100, 875, 470); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); { JButton delete = new JButton("\u4FEE\u6539"); delete.setBounds(620, 347, 90, 39); delete.setFont(new Font("幼圆", Font.PLAIN, 20)); delete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { status.setText(""); if(no.getText().trim().equals("")){ status.setText("请输入数据"); return; }else if(!no.getText().trim().equals("")){ screen.setText(gra.yuheng.studentGroup.modify(no.getText().trim(),name.getText().trim(),grade.getText().trim())); status.setText("修改成功!"); } } }); contentPanel.setLayout(null); contentPanel.add(delete); delete.setActionCommand("OK"); getRootPane().setDefaultButton(delete); } { JButton cancelButton = new JButton("清空"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { no.setText(""); grade.setText(""); name.setText(""); status.setText(""); screen.setText(""); } }); cancelButton.setBounds(720, 347, 90, 39); cancelButton.setFont(new Font("幼圆", Font.PLAIN, 20)); contentPanel.add(cancelButton); cancelButton.setActionCommand("Cancel"); } JPanel panel_4 = new JPanel(); panel_4.setBounds(526, 25, 284, 312); contentPanel.add(panel_4); panel_4.setLayout(null); JPanel stuName = new JPanel(); stuName.setBounds(0, 134, 284, 59); panel_4.add(stuName); stuName.setLayout(null); JLabel lblNewLabel_1 = new JLabel("\u59D3\u540D"); lblNewLabel_1.setBounds(0, 0, 61, 59); stuName.add(lblNewLabel_1); lblNewLabel_1.setBackground(Color.WHITE); lblNewLabel_1.setFont(new Font("方正姚体", Font.PLAIN, 25)); name = new JTextField(); name.setFont(new Font("宋体", Font.BOLD, 19)); name.setBounds(63, 14, 211, 33); stuName.add(name); name.setColumns(10); JPanel stuGrade = new JPanel(); stuGrade.setBounds(0, 189, 284, 59); panel_4.add(stuGrade); stuGrade.setLayout(null); JLabel lblNewLabel_2 = new JLabel("\u6210\u7EE9"); lblNewLabel_2.setBounds(0, 0, 50, 59); stuGrade.add(lblNewLabel_2); lblNewLabel_2.setBackground(Color.WHITE); lblNewLabel_2.setFont(new Font("方正姚体", Font.PLAIN, 25)); grade = new JTextField(); grade.setFont(new Font("宋体", Font.BOLD, 19)); grade.setBounds(63, 19, 211, 33); stuGrade.add(grade); grade.setColumns(10); JPanel stuNO = new JPanel(); stuNO.setBounds(0, 49, 284, 49); panel_4.add(stuNO); stuNO.setLayout(null); JLabel lblNewLabel = new JLabel("\u5B66\u53F7"); lblNewLabel.setBounds(0, 0, 81, 49); stuNO.add(lblNewLabel); lblNewLabel.setBackground(Color.WHITE); lblNewLabel.setFont(new Font("方正姚体", Font.PLAIN, 25)); no = new JTextField(); no.setFont(new Font("宋体", Font.BOLD, 19)); no.setBounds(64, 10, 210, 33); stuNO.add(no); no.setColumns(10); JPanel panel = new JPanel(); panel.setBounds(0, 258, 284, 44); panel_4.add(panel); panel.setLayout(null); status.setForeground(Color.BLACK); status.setFont(new Font("幼圆", Font.BOLD, 18)); status.setEnabled(false); status.setEditable(false); status.setBounds(64, 11, 210, 27); panel.add(status); JLabel label = new JLabel("\u72B6\u6001"); label.setBounds(0, 0, 64, 40); panel.add(label); label.setBackground(Color.WHITE); label.setFont(new Font("方正姚体", Font.PLAIN, 25)); JLabel label_2 = new JLabel("\u8F93\u5165\u67E5\u8BE2\u56E0\u5B50"); label_2.setFont(new Font("宋体", Font.PLAIN, 16)); label_2.setBounds(0, 10, 132, 37); panel_4.add(label_2); JLabel label_3 = new JLabel("\u8F93\u5165\u4FEE\u6539\u540E\u7684\u4FE1\u606F"); label_3.setFont(new Font("宋体", Font.PLAIN, 16)); label_3.setBounds(0, 105, 184, 29); panel_4.add(label_3); screen.setFont(new Font("微软雅黑", Font.PLAIN, 18)); screen.setEditable(false); screen.setLineWrap(true); screen.setBounds(10, 58, 458, 328); contentPanel.add(screen); JLabel label_1 = new JLabel("\u5B66\u53F7 \u59D3\u540D \u6210\u7EE9"); label_1.setHorizontalAlignment(SwingConstants.LEFT); label_1.setForeground(Color.BLACK); label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); label_1.setBackground(Color.WHITE); label_1.setBounds(10, 27, 458, 24); contentPanel.add(label_1); JButton button = new JButton("\u67E5\u8BE2"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { status.setText(""); if(no.getText().trim().equals("")&&name.getText().trim().equals("")&&grade.getText().trim().equals("")){ status.setText("请输入数据"); return; }else if((!no.getText().trim().equals(""))&&!(name.getText().trim().equals(""))) { screen.setText(gra.yuheng.studentGroup.lookup(no.getText().trim(),name.getText().trim())); status.setText("查询成功!"); } else if(!no.getText().trim().equals("")) { screen.setText(gra.yuheng.studentGroup.lookup(no.getText().trim(),1)); status.setText("查询成功!"); } else if(!name.getText().trim().equals("")){ screen.setText(gra.yuheng.studentGroup.lookup(name.getText().trim(),2)); status.setText("查询成功!"); } else if(!grade.getText().trim().equals("")){ screen.setText(gra.yuheng.studentGroup.lookup(grade.getText().trim(),3)); status.setText("查询成功!"); } } }); button.setFont(new Font("幼圆", Font.PLAIN, 20)); button.setActionCommand("OK"); button.setBounds(526, 347, 84, 39); contentPanel.add(button); } }
资源文件:图标与标题(放于工程文件src下)
img(图标文件)
工程文件:
作业12-图形界面于衡
终于敲完啦,满满的自豪感
棒棒哒