Java实验05(03)登录触发

一.界面

二.代码

/**
 * @Description
 * @auther lim
 * @create 2020-03-28 21:08
 */

package test08_Login;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

//快速引包,快捷键是ctrl+shift+O

public class Main {
    
    
	public static void main(String[] args) {
    
    
		LoginGUI loginGUI = new LoginGUI();
	}
}

class LoginGUI extends JFrame implements ActionListener {
    
    
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 定义组件
	JLabel jlabel1, jlabel2;
	// 用户名
	JTextField usernameText;
	// 密码
	JPasswordField passwordText;
	// 按钮
	JButton loginButton, cancelButton;

	JPanel jpanel1, jpanel2, jpanel3;

	public LoginGUI() {
    
    
		// 创建组件
		jpanel1 = new JPanel();
		jpanel2 = new JPanel();
		jpanel3 = new JPanel();
		jlabel1 = new JLabel("用户名");
		jlabel2 = new JLabel("密  码");

		// 控制宽度,美观
		usernameText = new JTextField(10);
		passwordText = new JPasswordField(10);

		loginButton = new JButton("登录");
		cancelButton = new JButton("取消");

		// 设置字体
		// 字号是18
		Font font = new Font("楷体", Font.BOLD, 18);
		jlabel1.setFont(font);
		jlabel2.setFont(font);
		// usernameText.setFont(font);
		// passwordText.setFont(font);
		loginButton.setFont(font);
		cancelButton.setFont(font);

		// 设置前景色
		Color color = new Color(0, 0, 255);
		jlabel1.setForeground(color);
		jlabel2.setForeground(color);
		// usernameText.setForeground(color);
		// passwordText.setForeground(color);
		loginButton.setForeground(color);
		cancelButton.setForeground(color);

		// 设置布局
		this.setLayout(new GridLayout(3, 1, 5, 5));
		jpanel1.add(jlabel1);
		jpanel1.add(usernameText);

		jpanel2.add(jlabel2);
		jpanel2.add(passwordText);

		jpanel3.add(loginButton);
		jpanel3.add(cancelButton);

		this.add(jpanel1);
		this.add(jpanel2);
		this.add(jpanel3);

		// 设置监听(绑定)
		loginButton.addActionListener(this);
		cancelButton.addActionListener(this);

		// 设置图标
		// 默认的路径在src前面!
		ImageIcon icon = new ImageIcon("./src/test08_Login/qq.png");
		this.setIconImage(icon.getImage());

		// 设置窗口
		this.setTitle("用户登录");
		// 窗口大小
		this.setSize(300, 180);

		// 设置窗体关闭时候,保证JVM要退出就是控制台
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 获取你的屏幕的宽和高
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		// 然后设置你编写的窗口的初始位置,也就是在中间,
		this.setLocation(width / 2 - 200, height / 2 - 150);

		// 显示
		this.setVisible(true);
	}

	// 实现抽象方法
	// 单击时候就调用这个
	@Override
	public void actionPerformed(ActionEvent e) {
    
    
		// 检查啥按钮被按下
		if (e.getSource() == loginButton) {
    
    
			if (checkPassword() == true) {
    
    
				// 显示弹出窗体
				// 后面的提示是标题栏
				JOptionPane.showMessageDialog(null, "用户名和密码正确,欢迎登录!", "提示", 1);

			} else {
    
    
				JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新登录!", "提示", 2);
			}
		} else {
    
    
			if (e.getActionCommand() == "取消") {
    
    
				clearText();
			}
		}

	}

	public boolean checkPassword() {
    
    
		String userName = "dxxy";
		String password = "admin";
		// trim()去掉前后多余的空格
		String inputUserName = usernameText.getText().trim();
		// String inputPassword = passwordText.getText().trim();
		// 密码框是错误的
		// 先转化再去空格!
		String inputPassword = String.valueOf(passwordText.getPassword()).trim();
		if (userName.equals(inputUserName) && password.equals(inputPassword)) {
    
    
			return true;
		} else {
    
    
			return false;
		}

	}

	// 取消时候清空文本框
	public void clearText() {
    
    
		usernameText.setText("");
		passwordText.setText("");
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_44179485/article/details/108400967
今日推荐