JavaFx创建简单的登录页面

MyLoginIn.java
程序入口

package fx.loginIn;

import static java.lang.System.err;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javafx.application.Application;

public class MyLoginIn {
	static ArrayList list = new ArrayList();  //存放用户数据的列表

	public static void main(String[] args) {
		LoadUser(); //从文件中载入用户数据
		Application.launch(QQLogin.class, args); //启动javafx Application程序
	}

	public static void LoadUser() {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(new File("user.txt")));
			String line = null;
			while ((line = reader.readLine()) != null) {
				String[] data = line.split("\\s+");
				list.add(new User(data[0], data[1]));

			}
			reader.close();
		} catch (IOException e) {
			err.println("用户文件数据 user.txt找不到");
			e.printStackTrace();
		}
	}

	public static boolean findUser(String account, String pwd) {
		return list.contains(new User(account, pwd));
	}

	public static User findAccount(String account) {
		for (User u : list)
			if (u.getAccount().equalsIgnoreCase(account))
				return u;
		return null;
	}

	public static User newUser() {
		User user = list.get(list.size() - 1);
		return new User(Integer.parseInt(user.getAccount()) + 1 + "", "");
	}

	public static void addNewUser(User user) {
		list.add(user);
		LoadUser();
		
	}
}

user.java
用户定义,属性包含账户和密码

package fx.loginIn;

class User {
	private String account;
	private String pwd;
	public User(String account, String pwd) {
		super();
		this.account = account;
		this.pwd = pwd;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	@Override
	public boolean equals(Object obj) { //覆盖equals方法,为了实现账户和密码相同的User是同一个对象,如果不覆盖,比较的时候会按照哈希值
		
		if (obj instanceof User) {
			if (this.getAccount().equals(((User) obj).getAccount())
					&& this.getPwd().equals(((User) obj).getPwd())) {
				return true;
			}
			else {
				return false;
			}
		}
		return false;
	}
	
}

QQLogin.java
简单的QQ登录

package fx.loginIn;

import java.io.File;
import java.io.FileInputStream;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class QQLogin extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {

		try { //设置登录图标
			primaryStage.getIcons().add(new Image(new FileInputStream(new File("icon.png"))));
		} catch (Exception e) {
			System.err.println("要设置的图标 icon.png找不到");
			e.printStackTrace();
		}
		primaryStage.setTitle("QQ登录");
		GridPane grid = new GridPane();
		grid.setAlignment(Pos.CENTER);
		grid.setHgap(10);
		grid.setVgap(10);
		grid.setPadding(new Insets(25, 25, 25, 25));// 设置填充, Insets实例设置矩形区域的四边的一组内偏移量

		Text title = new Text("登录");
		title.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
		Label Account = new Label("账户:");
		TextField userAccount = new TextField();
		Label pwd = new Label("密码:");
		PasswordField userPwd = new PasswordField();
		Label no = new Label("没有账号?->");
		no.setTextFill(Color.BLUE);
		Button confirm = new Button("确定");
		Button register = new Button("注册");
		Button forget = new Button("忘记密码");

		confirm.setOnAction(new EventHandler() {

			@Override
			public void handle(ActionEvent arg0) {

				if (MyLoginIn.findUser(userAccount.getText(), userPwd.getText())) {
					Alert alert = new Alert(AlertType.INFORMATION);
					alert.setTitle("成功提示");
					alert.setHeaderText(null);
					alert.setContentText("登录成功");
					alert.showAndWait();
				} else {
					Alert alert = new Alert(AlertType.ERROR);
					alert.setTitle("错误提示");
					alert.setHeaderText(null);
					alert.setContentText("用户名或密码错误,请重新输入");
					alert.showAndWait();
					userAccount.clear();
					userPwd.clear();
				}
			}

		});

		register.setOnAction(new EventHandler() {

			@Override
			public void handle(ActionEvent event) {
				Stage dialog = new Stage();
				dialog.initModality(Modality.APPLICATION_MODAL);
				dialog.initOwner(primaryStage);
				User user = MyLoginIn.newUser();
				Alert alert = new Alert(AlertType.INFORMATION);
				alert.setTitle("成功提示");
				alert.setHeaderText(null);
				alert.setContentText("您的账户为: " + user.getAccount());
				alert.showAndWait();
				dialog.setTitle("设置密码");
				PasswordField newPwd = new PasswordField();
				Label label = new Label("密码");
				Button confirm = new Button("确认");
				confirm.setOnAction(new EventHandler() {
					@Override
					public void handle(ActionEvent event) {
						
						alert.setTitle("成功提示");
						alert.setHeaderText(null);
						alert.setContentText("注册成功,请返回登录");
						user.setPwd(newPwd.getText());
						MyLoginIn.addNewUser(user);
						alert.showAndWait();
						dialog.close();
						userAccount.setText(user.getAccount());
						userPwd.clear();
					}
			});
				GridPane grid = new GridPane();
				grid.setAlignment(Pos.CENTER);
				grid.setHgap(10);
				grid.setVgap(10);
				grid.setPadding(new Insets(25, 25, 25, 25));// 设置填充, Insets实例设置矩形区域的四边的一组内偏移量
				grid.add(label, 0, 1);
				grid.add(newPwd, 1, 1);
				grid.add(confirm, 1, 2);
				dialog.setScene(new Scene(grid, 300, 100));
				dialog.show();
		}	
	});

		forget.setOnAction(new EventHandler() {

			@Override
			public void handle(ActionEvent event) {

				User user = MyLoginIn.findAccount(userAccount.getText());
				if (user != null) {
					Stage dialog = new Stage();
					dialog.setTitle("重置密码");
					dialog.initModality(Modality.APPLICATION_MODAL);
					dialog.initOwner(primaryStage);
					PasswordField newPwd = new PasswordField();
					Label label = new Label("新密码");
					Button confirm = new Button("确认");
					confirm.setOnAction(new EventHandler() {

						@Override
						public void handle(ActionEvent event) {
							user.setPwd(newPwd.getText());
							Alert alert = new Alert(AlertType.INFORMATION);
							alert.setTitle("成功提示");
							alert.setHeaderText(null);
							alert.setContentText("修改密码成功,请返回登录");
							alert.showAndWait();
							dialog.close();
							userPwd.clear();
						}
					});
					GridPane grid = new GridPane();
					grid.setAlignment(Pos.CENTER);
					grid.setHgap(10);
					grid.setVgap(10);
					grid.setPadding(new Insets(25, 25, 25, 25));// 设置填充, Insets实例设置矩形区域的四边的一组内偏移量
					grid.add(label, 0, 1);
					grid.add(newPwd, 1, 1);
					grid.add(confirm, 1, 2);
					dialog.setScene(new Scene(grid, 300, 100));
					dialog.show();
				} else {
					Alert alert = new Alert(AlertType.ERROR);
					alert.setTitle("错误提示");
					alert.setHeaderText(null);
					alert.setContentText("您输入的账户不存在,请重新输入");
					alert.showAndWait();
					userAccount.clear();
					userPwd.clear();
				}
			}
		});
		grid.add(title, 0, 0, 2, 1);
		grid.add(Account, 0, 1);
		grid.add(userAccount, 1, 1);
		grid.add(pwd, 0, 2);
		grid.add(userPwd, 1, 2);

		HBox panel = new HBox(40);
		panel.setAlignment(Pos.BOTTOM_RIGHT);
		panel.getChildren().add(confirm);

		HBox panel2 = new HBox(40);
		panel2.setAlignment(Pos.BOTTOM_RIGHT);
		panel2.getChildren().add(no);
		panel2.getChildren().add(register);
		grid.add(panel, 1, 4);
		grid.add(panel2, 1, 5);

		HBox panel3 = new HBox(20);
		panel3.setAlignment(Pos.BOTTOM_RIGHT);
		panel3.getChildren().add(forget);
		grid.add(panel3, 1, 6);
		primaryStage.setScene(new Scene(grid, 350, 300));

		primaryStage.show();
	}
}

效果图
JavaFx创建简单的登录页面_第1张图片
用户数据文件
JavaFx创建简单的登录页面_第2张图片

你可能感兴趣的:(JavaFx创建简单的登录页面)