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

📄 login_1.java

📁 主要是对于JAVA的编程的基本语言 希望能够帮得上你。
💻 JAVA
字号:
package MultipleWindow;

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Login_1 extends Frame {

	/*
	 * 声明部分
	 */	
	Label lblUserName, lblPassword;
	TextField txtUserName, txtPassword;
	Button btnOK, btnCancel;
	Panel panTop, panMiddle, panBottom;
	String strUserName, strPassword, curUserName, curPassword;
	static Login_1 login;
	MainWindow main;
	File user;

	/*
	 * 构造方法
	 */
	public Login_1(String title) {
		super(title);
		initialize();
	}

	private void initialize() {
		/*
		 * 创建对象 
		 */
		lblUserName = new Label("输入用户名:");
		lblPassword = new Label("请输入密码:");
		txtUserName = new TextField("", 20);
		txtPassword = new TextField("", 20);
		btnOK = new Button("确定");
		btnCancel = new Button("取消");
		panTop = new Panel();
		panMiddle = new Panel();
		panBottom = new Panel();
		main = new MainWindow("成绩管理系统V1.0");

		/*
		 * 添加组件 
		 */
		panTop.add(lblUserName);
		panTop.add(txtUserName);
		panMiddle.add(lblPassword);
		panMiddle.add(txtPassword);
		panBottom.add(btnOK);
		panBottom.add(btnCancel);
		this.add(panTop);
		this.add(panMiddle);
		this.add(panBottom);

		/*
		 * 设置属性 
		 */
		this.setLayout(new GridLayout(3, 1)); // 设置窗口布局
		this.setSize(250, 200); // 设置窗口大小
		this.setLocationRelativeTo(null);// 让窗口居中
		this.setResizable(false); // 窗口不可调整大小
		this.pack(); // 使窗口恰好容纳组件
		this.setVisible(true); // 让窗口可见
		txtPassword.setEchoChar('*'); // 设置回显字符
		
		/*
		 * 从user.txt获取用户名和密码分别赋值给curUserName和curPassword		 * 
		 */
		
			try {
				BufferedReader br=new BufferedReader
				     (new FileReader("ScoreManagement/user.txt"));
				curUserName=br.readLine();
				curPassword=br.readLine();				
			} catch (FileNotFoundException e1) {				
				e1.printStackTrace();
			} catch (IOException e1) {				
				e1.printStackTrace();
			}		

		/*
		 * 注册监听器,编写事件处理代码
		 * 采用匿名内部类方式来实现
		*/
		
		//窗口关闭事件
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		//确定按钮单击事件
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				strUserName = txtUserName.getText().trim();
				strPassword = txtPassword.getText().trim();
				if (strUserName.equals(curUserName)) {
					if (strPassword.equals(curPassword)) {
						System.out.println("欢迎使用本系统!");
						main.setVisible(true);
						main.setAlwaysOnTop(true);
						login.dispose();
					} else {
						System.out.println("密码错误,请重新输入。");
						txtPassword.requestFocus();
						txtPassword.setText("");
					}
				} else {
					System.out.println("用户名不存在,请重新输入。");
					txtUserName.requestFocus();
					txtUserName.setSelectionStart(0);
					txtUserName.setSelectionEnd(txtUserName.getText().length());
				}
			}
		});

		//确定按钮按键事件
		btnOK.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == 10) {
					strUserName = txtUserName.getText().trim();
					strPassword = txtPassword.getText().trim();
					if (strUserName.equals("admin")) {
						if (strPassword.equals("12345")) {
							System.out.println("欢迎使用本系统!");
							main.setVisible(true);
							main.setAlwaysOnTop(true);
							login.dispose();
						} else {
							System.out.println("密码错误,请重新输入。");
							txtPassword.requestFocus();
							txtPassword.setText("");
						}
					} else {
						System.out.println("用户名不存在,请重新输入。");
						txtUserName.requestFocus();
						txtUserName.setSelectionStart(0);
						txtUserName.setSelectionEnd(txtUserName.getText()
								.length());
					}
				}
			}
		});

		//取消按钮单击事件
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		//用户名文本框按键事件
		txtUserName.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == 10) {
					txtPassword.requestFocus();
				}
			}
		});

		//密码文本框按键事件
		txtPassword.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == 10) {
					btnOK.requestFocus();
				}
			}
		});
	}

	//主方法
	public static void main(String[] args) {
		login = new Login_1("登录");
	}
}

⌨️ 快捷键说明

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