JAVA使用GUI写一个学生信息管理系统0.2版本--回车登陆+LOGO放置+第二个界面完善

0.2版本的学生信息管理系统隆重发布!!

1 登陆界面回车可直接登陆(弄了好久)!

2 新增加LOGO放置(炫酷)!

3 点击登陆出现第二个界面!

善意提示(WARNING),本作者是小白,写的不好,不要喷哦!


JAVA使用GUI写一个学生信息管理系统0.2版本--回车登陆+LOGO放置+第二个界面完善


如上图:直接上代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import frame.ImagePanel;


public class Login{
    public static void main (String []args){

        AFrame f = new AFrame("学生信息管理系统");

    }
}


class AFrame extends JFrame{
    JButton b1;
    JButton b2;
    JTextField t1;
    JTextField t2;

    public AFrame(String title){
        super(title);
        this.setLayout(null);
        Panel p = new Panel(); 
        b1 =new JButton("登陆");
        b2 =new JButton("取消");
    
        t1 =new JTextField(25);
        t1.setFont(new Font("宋体",Font.BOLD,25));

        t2 =new JPasswordField(25);
        t2.setFont(new Font("宋体",Font.BOLD,25));

        JLabel nameLabel = new JLabel("用户名");
        nameLabel.setFont(new Font("宋体",Font.BOLD,25));
        nameLabel.setHorizontalAlignment(JTextField.CENTER);
        JLabel pwdLabel = new JLabel("密码");
        pwdLabel.setFont(new Font("宋体",Font.BOLD,25));
        pwdLabel.setHorizontalAlignment(JTextField.CENTER);

        this.setBounds(300,300,1000,500);
        this.add(nameLabel);
        this.add(t1);
        this.add(pwdLabel);
        this.add(t2);
        this.add(b1);
        this.add(b2);

        nameLabel.setBounds(100,100,200,50);
        pwdLabel.setBounds(100,200,200,50);
        t1.setBounds(600,100,200,50);
        t2.setBounds(600,200,200,50);
        b1.setBounds(100,300,200,50);
        b2.setBounds(600,300,200,50);

        b1.addActionListener (new ResponseButton());

        b2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(null,"单击确定退出");
                System.exit(0);
            }
        });

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        getRootPane().setDefaultButton(b1);
        b1.addKeyListener (new KeyAdapter(){

			public void keyPressed(KeyEvent e){
                if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
                    AFrame2 f1 = new AFrame2("学生信息管理系统-123456");
                
				}else{
                    System.out.print(t1.getText());
                    JOptionPane.showMessageDialog(null, "账号或者密码错误", "Error.mxy",JOptionPane.ERROR_MESSAGE); 
            
                }
            }
        });

        ImagePanel ip = new ImagePanel(new ImageIcon("C:\\Users\\admin\\Desktop\\logo.png").getImage());
        this.add(ip);

    }

    public class ResponseButton  implements ActionListener{
        public void actionPerformed(ActionEvent e){
                String str ="123456";
                if(t1.getText().equals(str)&&t2.getText().equals(str) ){

                    AFrame2 f1 = new AFrame2("学生信息管理系统-123456");

                }else{
                    System.out.print(t1.getText());
                    JOptionPane.showMessageDialog(null, "账号或者密码错误", "Error.mxy",JOptionPane.ERROR_MESSAGE); 
            
                }
        }
    }

    class AFrame2 extends JFrame{
        JButton b3;
        JButton b4;
        JButton b5;
        JButton b6;
        public AFrame2(String title){
            super(title);
            b3 =new JButton("录入");
            b4 =new JButton("查询");
            b5 =new JButton("修改");
            b6 =new JButton("删除");
    
            this.add(b3);
            this.add(b4);
            this.add(b5);
            this.add(b6);
    
            b3.setBounds(100,80,160,100);
            b4.setBounds(100,290,160,100);
            b5.setBounds(300,80,160,100);
            b6.setBounds(300,290,160,100);
    
            this.setLayout(null);
            this.setBounds(300,300,600,500);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    }

}

以上是Login.java,以下则是放置logo的代码文件:

package frame;
 
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
 
import javax.swing.JPanel;
 
public class ImagePanel extends JPanel{
    private Image image;
    public ImagePanel(Image img){
        this.image = img;
        Dimension dime = new Dimension(image.getWidth(null),image.getHeight(null));
        this.setPreferredSize(dime);
        this.setMaximumSize(dime);
        this.setMinimumSize(dime);
        this.setSize(dime);
        this.setLayout(null);
    }
     
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(image, 0,0,null);
    }
}