⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 一个简单的学生信息查询系统
💻 JAVA
字号:
//MainFrame.java 

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 

public class MainFrame extends JFrame implements ActionListener { 
    JLabel label[]; 

    JLabel label_admin, label_user; 

    JTextField textfield[]; 

    Container con; 

    JButton b_all, b_one, b_pw, b_save, b_exit, b_about, b_del, b_user; 

    String username; 

    // 产生主界面 
    public MainFrame(String username, int flag) { 
        super("欢迎使用学生信息查询系统--" + username); 
        this.username = username; 
        con = getContentPane(); 
        con.setLayout(null); 
        setSize(520, 350); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

        label = new JLabel[10];
        textfield = new JTextField[10]; 
        for (int i = 0; i < 10; i++) { 
            label[i] = new JLabel(); 
            textfield[i] = new JTextField(); 
        } 
        label[0].setText("学号"); 
        label[1].setText("姓名"); 
        label[2].setText("性别"); 
        label[3].setText("出生日期"); 
        label[4].setText("所属班级"); 
        label[5].setText("家庭住址"); 
        label[6].setText("手机"); 
        label[7].setText("入学时间"); 
        label[8].setText("毕业时间"); 
        label[9].setText("学位"); 

        for (int i = 0; i < 10; i++) { 
            con.add(label[i]); 
            con.add(textfield[i]); 
        } 

        for (int i = 0; i < 10; i++) { 
            label[i].setBounds(10, i * 30 + 10, 70, 20); 
            label[i].setFont(new Font("", Font.PLAIN, 14)); 
            textfield[i].setBounds(80, i * 30 + 10, 150, 20); 
            textfield[i].setFont(new Font("", Font.PLAIN, 14)); 
        } 

        b_all = new JButton(); 
        b_all.setText("查询所有"); 
        b_one = new JButton(); 
        b_one.setText("单个查询"); 
        b_exit = new JButton(); 
        b_exit.setText("退出"); 
        b_pw = new JButton(); 
        b_pw.setText("修改密码"); 
        b_about = new JButton(); 
        b_about.setText("关于"); 

        con.add(b_about); 
        con.add(b_all); 
        con.add(b_one); 
        con.add(b_exit); 
        con.add(b_pw); 
        b_all.setBounds(260, 70, 100, 20); 
        b_one.setBounds(390, 70, 100, 20); 
        b_exit.setBounds(400, 280, 80, 20); 
        b_pw.setBounds(260, 100, 100, 20); 
        b_about.setBounds(300, 280, 80, 20); 

        b_all.addActionListener(this); 
        b_one.addActionListener(this); 
        b_exit.addActionListener(this); 
        b_pw.addActionListener(this); 
        b_about.addActionListener(this); 

        // 根据用户类型确定文本是否可以编辑 
        if (flag == 1) { 
            for (int i = 1; i < 10; i++) { 
                textfield[i].setEditable(false); 
            } 
        } 

        label_user = new JLabel("单个查询时,填写学号,点击单个查询即可"); 
        con.add(label_user); 
        label_user.setFont(new Font("", Font.PLAIN, 14)); 
        label_user.setBounds(240, 10, 310, 20); 

        if (flag == 0) { 
            label_admin = new JLabel("修改记录时,修改左侧内容,点击保存即可"); 
            con.add(label_admin); 
            label_admin.setFont(new Font("", Font.PLAIN, 14)); 
            label_admin.setBounds(240, 40, 310, 20); 
            b_save = new JButton(); 
            b_save.setText("保存修改"); 
            b_del = new JButton();
            b_del.setText("删除信息"); 
            b_user = new JButton(); 
            b_user.setText("用户管理"); 
            con.add(b_user); 
            con.add(b_del); 
            con.add(b_save); 
            b_del.setBounds(260, 130, 100, 20); 
            b_save.setBounds(390, 100, 100, 20); 
            b_user.setBounds(390, 130, 100, 20); 
            b_save.addActionListener(this); 
            b_del.addActionListener(this); 
            b_user.addActionListener(this); 
        } 

        // 将窗口置于屏幕中央 
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
        Dimension frameSize = this.getSize(); 
        if (frameSize.height > screenSize.height) { 
            frameSize.height = screenSize.height; 
        } 
        if (frameSize.width > screenSize.width) { 
            frameSize.width = screenSize.width; 
        } 
        setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 
        setResizable(false); 
        setVisible(true); 
    } 

    public void actionPerformed(ActionEvent e) { 
        if (e.getSource() == b_all) { 
            new ShowAllInfo(); 
        } else if (e.getSource() == b_one) { 
            String ID = textfield[0].getText(); 
            for (int i = 0; i < 10; i++) { 
                textfield[i].setText(""); 
            } 
            // System.out.println(ID); 
            String noID = (new JTextField()).getText(); 
            if (ID.equals(noID)) { 
                textfield[0].setText("请输入学号"); 
            } else { 
                StudentInfo SInfo = new StudentInfo(); 
                SInfo.SearchByID(ID); 
                textfield[0].setText(SInfo.ID); 
                textfield[1].setText(SInfo.Name); 
                textfield[2].setText(SInfo.Sex); 
                textfield[3].setText(SInfo.Birthdate.toString()); 
                textfield[4].setText(SInfo.Classname); 
                textfield[5].setText(SInfo.Homeaddr); 
                textfield[6].setText(SInfo.Mobile); 
                textfield[7].setText(SInfo.Intime.toString()); 
                textfield[8].setText(SInfo.Outtime.toString()); 
                textfield[9].setText(SInfo.Diploma); 
            } 
        } else if (e.getSource() == b_save) { 
            String ID, Name, Sex, Classname, Homeaddr, Mobile, Diploma; 
            String Birthdate, Intime, Outtime; 
            ID = textfield[0].getText(); 
            Name = textfield[1].getText(); 
            Sex = textfield[2].getText(); 
            Birthdate = textfield[3].getText(); 
            Classname = textfield[4].getText(); 
            Homeaddr = textfield[5].getText(); 
            Mobile = textfield[6].getText(); 
            Intime = textfield[7].getText(); 
            Outtime = textfield[8].getText(); 
            Diploma = textfield[9].getText(); 
            StudentInfo SInfo = new StudentInfo(); 
            if (SInfo.isExist(ID)) { 
                // 更新信息 
                // JOptionPane.showMessageDialog(null, "此学号已存在", "通知", 
                // JOptionPane.INFORMATION_MESSAGE); 
                if (SInfo.UpdateInfo(ID, Name, Sex, Birthdate, Classname, Homeaddr, Mobile, Intime, Outtime, Diploma)) { 
                    JOptionPane.showMessageDialog(null, "保存修改成功", "信息", JOptionPane.INFORMATION_MESSAGE); 
                } else { 
                    JOptionPane.showMessageDialog(null, "保存修改失败", "警告", JOptionPane.WARNING_MESSAGE); 
                } 
            } else { 
                // 创建新信息 
                // JOptionPane.showMessageDialog(null, "此学号不存在", "通知", 
                // JOptionPane.INFORMATION_MESSAGE); 
                if (SInfo.CreateInfo(ID, Name, Sex, Birthdate, Classname,Homeaddr, Mobile, Intime, Outtime, Diploma)) { 
                    JOptionPane.showMessageDialog(null, "创建信息成功", "信息", JOptionPane.INFORMATION_MESSAGE); 
                } else { 
                    JOptionPane.showMessageDialog(null, "创建信息失败", "警告", JOptionPane.WARNING_MESSAGE); 
                } 
            } 
            } else if (e.getSource() == b_pw) { 
                new ChangePassword(username); 
            } else if (e.getSource() == b_about) { 
                new About(); 
            } else if (e.getSource() == b_exit) { 
                System.exit(0); 
            } else if (e.getSource() == b_del) { 
                String ID; 
                ID = textfield[0].getText(); 
                StudentInfo SInfo = new StudentInfo(); 
                if (SInfo.isExist(ID)) { 
                    if (SInfo.DeleteInfo(ID)) { 
                        JOptionPane.showMessageDialog(null, "删除成功", "通知", JOptionPane.INFORMATION_MESSAGE); 
                    } else { 
                        JOptionPane.showMessageDialog(null, "删除失败", "警告", JOptionPane.WARNING_MESSAGE); 
                    } 
                } else { 
                    JOptionPane.showMessageDialog(null, "此学号不存在", "通知", JOptionPane.INFORMATION_MESSAGE); 
                } 
                for (int i = 0; i < 10; i++) { 
                    textfield[i].setText(""); 
                } 
            } else if (e.getSource() == b_user) { 
                new UserManager(); 
        } 
    } 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -