📄 buttonlistener3.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.util.logging.Level;import java.util.logging.Logger;import javax.swing.*;/** * * @author HSM */class ButtonListener3 implements ActionListener { public void actionPerformed(ActionEvent e) { final JFrame frame1 = new JFrame("Sorce_Search"); Container container = frame1.getContentPane(); 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); JLabel jLabel1 = new JLabel("成绩查询"); JLabel jLabel2 = new JLabel("学号:"); JLabel jLabel3 = new JLabel("课程:"); JLabel jLabel4 = new JLabel("查询结果:"); JLabel jLabel5 = new JLabel(" "); JLabel jLabel6 = new JLabel(" "); JLabel jLabel7 = new JLabel(" "); JLabel jLabel8 = new JLabel(" "); final JTextField jtf1 = new JTextField(9); final JTextField jtf2 = new JTextField(9); JTextField jtf3 = new JTextField(10); JTextField jtf4 = new JTextField(10); JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel p2 = new JPanel(new FlowLayout()); JPanel p3 = new JPanel(new FlowLayout()); JPanel p4 = new JPanel(new FlowLayout()); JPanel p5 = new JPanel(new FlowLayout(FlowLayout.CENTER,10,5)); JPanel p6 = new JPanel(new FlowLayout()); JPanel p7 = new JPanel(new BorderLayout()); JPanel p8 = new JPanel(new BorderLayout()); JButton jbtCx = new JButton(" 查询 "); JButton jbtQx = new JButton(" 取消 "); final JTextArea jtArea = new JTextArea(12,7);// jtArea.setBounds(new Rectangle(25, 64, 98, 45)); p1.add(jLabel1); p2.add(jLabel2); p2.add(jtf1); p3.add(jLabel3); p3.add(jtf2); p4.add(jbtCx); p4.add(jbtQx); p5.add(p2); p5.add(p3); p5.add(p4); p7.add(jLabel4,BorderLayout.NORTH); p7.add(jtArea,BorderLayout.CENTER); p8.add(p1,BorderLayout.NORTH); p8.add(p5,BorderLayout.CENTER); p8.add(p7,BorderLayout.SOUTH); container.add(jLabel5, BorderLayout.NORTH); container.add(jLabel6, BorderLayout.SOUTH); container.add(jLabel7, BorderLayout.WEST); container.add(jLabel8, BorderLayout.EAST); container.add(p8, BorderLayout.CENTER); jbtQx.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frame1.dispose(); } }); jbtCx.addActionListener(new ActionListener() { @SuppressWarnings("empty-statement") public void actionPerformed(ActionEvent e) { String str = jtf2.getText(); String strNumber = jtf1.getText(); File file = new File(str); int flag = 0; if(file.exists()){ DataInputStream input = null; try { input = new DataInputStream(new FileInputStream(str)); String strXh = null; String strName = null; String strDate = null; double score = 0; while (input.available() != 0) { strXh = input.readUTF(); strName = input.readUTF(); score = input.readDouble(); strDate = input.readUTF(); if (strXh.equals(strNumber)) { flag = 1; break; } } if (flag == 1) { String stResult = " 学号:" + strXh + " 姓名:" + strName + " 课程:" + str + " 成绩:" + score + " 考试时间:" + strDate; jtArea.setText(stResult); } else JOptionPane.showMessageDialog(null, "该同学本门课程未录入成绩!", "信息", JOptionPane.INFORMATION_MESSAGE); } catch (IOException ex) { Logger.getLogger(ButtonListener3.class.getName()).log(Level.SEVERE, null, ex); } finally { try { input.close(); } catch (IOException ex) { Logger.getLogger(ButtonListener3.class.getName()).log(Level.SEVERE, null, ex); } } } else JOptionPane.showMessageDialog(null, "该同学本门课程未录入成绩!", "信息", JOptionPane.INFORMATION_MESSAGE); } }); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -