📄 buttonlistener2.java
字号:
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;/* * To change this template, choose Tools | Templates * and open the template in the editor. */import java.io.*;import java.lang.String;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.*;/** * * @author HSM */public class ButtonListener2 implements ActionListener { public void actionPerformed(ActionEvent e) { final JFrame frame1 = new JFrame("Date_Input"); Container container = frame1.getContentPane(); Font font1 = new Font("微软雅黑", Font.BOLD, 14); Font font2 = new Font("SansSerif", Font.BOLD, 30); JLabel jLabel1 = new JLabel(" 年级:"); jLabel1.setFont(font1); JLabel jLabel2 = new JLabel(" 班级:"); jLabel2.setFont(font1); JLabel jLabel3 = new JLabel(" 专业:"); jLabel3.setFont(font1); JLabel jLabel4 = new JLabel("考试时间:"); jLabel4.setFont(font1); JLabel jLabel9 = new JLabel("课程:"); jLabel9.setFont(font1); JLabel jLabel5 = new JLabel("成绩输入"); JLabel jLabel7 = new JLabel(""); JLabel jLabel8 = new JLabel(""); JLabel jLabel = new JLabel(""); JLabel jLabel10 = new JLabel(""); JLabel jLabel11 = new JLabel("请检测输入数据是否正确?"); JPanel p5 = new JPanel(new FlowLayout()); p5.add(jLabel5); JPanel p4 = new JPanel(new FlowLayout()); p4.add(jLabel7); JPanel p7 = new JPanel(new FlowLayout()); JPanel p8 = new JPanel(new FlowLayout()); p8.add(jLabel10); final JTextField jtf1 = new JTextField(10); final JTextField jtf2 = new JTextField(10); final JTextField jtf3 = new JTextField(10); final JTextField jtf4 = new JTextField(10); JPanel p1 = new JPanel(new FlowLayout()); p1.add(jLabel1); p1.add(jtf1); p1.add(jLabel2); p1.add(jtf2); JPanel p2 = new JPanel(new FlowLayout()); p2.add(jLabel3); p2.add(jtf3); p2.add(jLabel4); p2.add(jtf4); JPanel p3 = new JPanel(new FlowLayout()); p3.add(jLabel9); final JComboBox jckecheng = new JComboBox(); jckecheng.addItem("请你选择"); jckecheng.setMaximumRowCount(15); p3.add(jckecheng); JButton jbtOk = new JButton(" 确定 "); JButton jbtQx = new JButton(" 取消 "); JButton jbtJc = new JButton(" 检测 "); jbtOk.setFont(font1); jbtQx.setFont(font1); p3.add(jbtOk); p3.add(jbtQx); JPanel p9 = new JPanel(new FlowLayout()); p9.add(jLabel11); p9.add(jbtJc); JPanel p6 = new JPanel(new GridLayout(0, 1, 5, 5)); p6.add(p5); p6.add(p1); p6.add(p2); p6.add(p9); p6.add(p3); p6.add(p4); p6.add(p7); p6.add(p8); JLabel jLabel6 = new JLabel(" 请按确定后输入每位学生的该门课程成绩!!"); jLabel6.setFont(font1); container.add(jLabel8, BorderLayout.NORTH); container.add(p6, BorderLayout.CENTER); container.add(jLabel6, BorderLayout.SOUTH); frame1.setSize(600, 400); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int screenHeight = screenSize.height; int screenWidth = screenSize.width; int x = (screenWidth - frame1.getWidth()) / 2; int y = (screenHeight - frame1.getHeight()) / 2; frame1.setLocation(x, y); frame1.setVisible(true); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jbtJc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String strjtf1 = jtf1.getText(); String strjtf2 = jtf2.getText(); String strjtf3 = jtf3.getText(); Class class1 = new Class(strjtf1,strjtf3,strjtf2); String str = class1.toString(); File file = new File(str); if(file.exists()){ JOptionPane.showMessageDialog(null, "输入正确!", "信息", JOptionPane.INFORMATION_MESSAGE); scoreReport sRet = read(str); Course[] courseList = sRet.getCourseName(); Student[] student = sRet.getStudentList(); Class class2 = sRet.getClassName(); String strCourse; for(int i = 0; i < courseList.length; i++){ strCourse = courseList[i].getName(); jckecheng.addItem(strCourse); } } else JOptionPane.showMessageDialog(null, "输入班级不存在,请从新输入!!", "信息", JOptionPane.INFORMATION_MESSAGE); } }); jbtOk.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String strjtf1 = jtf1.getText(); String strjtf2 = jtf2.getText(); String strjtf3 = jtf3.getText(); String strjtf4 = jtf4.getText(); Class class1 = new Class(strjtf1,strjtf3,strjtf2); String str = class1.toString(); scoreReport scRet = read(str); Course[] course1 = scRet.getCourseName(); Student[] studentList = scRet.getStudentList(); double[][] score1 = scRet.getScore(); String kc = (String) jckecheng.getSelectedItem().toString(); int j = 0; for (int i = 0; i < course1.length; i++) { if (course1[i].getName().equals(kc)) { j = i; break; } } if(score1[j][0] == 0){ for (int i = 0; i < studentList.length; i++) { String strScore = JOptionPane.showInputDialog(null, "学号" + studentList[i].getNumber(), "成绩输入", JOptionPane.QUESTION_MESSAGE); double score = Integer.parseInt(strScore); course1[j].setTestDate(strjtf4); studentList[i].setCourseList(course1); score1[j][i] = score; } JOptionPane.showMessageDialog(null, "信息存储成功!", "信息", JOptionPane.INFORMATION_MESSAGE); scRet.setScore(score1); scRet.setStudentList(studentList); write(scRet,str); writeKcscore(studentList,kc,score1,j); frame1.dispose(); } else JOptionPane.showMessageDialog(null, "该课程分数已输入,请输入其他课程!", "信息", JOptionPane.INFORMATION_MESSAGE); } catch (FileNotFoundException ex) { Logger.getLogger(ButtonListener2.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ButtonListener2.class.getName()).log(Level.SEVERE, null, ex); } } }); jbtQx.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frame1.dispose(); } }); } public scoreReport read(String str) { ObjectInputStream input = null; scoreReport scRet = null; try { input = new ObjectInputStream(new BufferedInputStream(new FileInputStream(str))); scRet = (scoreReport) (input.readObject()); input.close(); } catch (IOException ex) { Logger.getLogger(ButtonListener2.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(ButtonListener2.class.getName()).log(Level.SEVERE, null, ex); } finally { try { input.close(); } catch (IOException ex) { Logger.getLogger(ButtonListener2.class.getName()).log(Level.SEVERE, null, ex); } } return scRet; } public void write(Object object,String str) throws IOException { ObjectOutputStream output = null; try { output = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(str))); output.writeObject(object); } catch (FileNotFoundException ex) { Logger.getLogger(ButtonListener1.class.getName()).log(Level.SEVERE, null, ex); } finally { try { output.close(); } catch (IOException ex) { Logger.getLogger(ButtonListener1.class.getName()).log(Level.SEVERE, null, ex); } } } public void writeKcscore(Student[] studentList,String str,double[][] score,int j) throws IOException { DataOutputStream output = null; output = new DataOutputStream(new FileOutputStream(str,true)); Course[] course = studentList[0].getCourseList(); for(int i = 0; i < studentList.length ; i++){ output.writeUTF(studentList[i].getNumber()); output.writeUTF(studentList[i].getName()); output.writeDouble(score[j][i]); output.writeUTF(course[j].getTestDate()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -