javafx登录界面

package a;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Main extends Application {

	public static boolean login(String username, String password) {
		boolean b = false;
		try {
			Class.forName("org.sqlite.JDBC");
			try (Connection conn = DriverManager.getConnection("jdbc:sqlite:mydata.sqlite3")) {
				try (PreparedStatement ps = conn
						.prepareStatement("select count(*) from t_user where username = ? and password = ?")) {
					ps.setString(1, username);
					ps.setString(2, password);
					try (ResultSet rs = ps.executeQuery()) {
						while (rs.next()) {
							int i = rs.getInt(1);
							if (i > 0) {
								b = true;
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}

	public void start(Stage stage) {

		Label lbl_userlogin = new Label("用户登录");
		Label lbl_username = new Label("用户名:");
		Label lbl_password = new Label("密码:");
		TextField txt_username = new TextField();
		PasswordField pwf_password = new PasswordField();
		Button btn_login = new Button("登录");
		Button btn_cancel = new Button("取消");

		Font f = new Font(20);
		lbl_userlogin.setFont(f);
		lbl_username.setFont(f);
		lbl_password.setFont(f);
		txt_username.setFont(f);
		pwf_password.setFont(f);
		btn_login.setFont(f);
		btn_cancel.setFont(f);

		btn_login.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent ae) {
				String username = txt_username.getText();
				String password = pwf_password.getText();

				if (login(username, password)) {
					System.out.println("登录成功");
				} else {
					Alert alert = new Alert(AlertType.CONFIRMATION);
					alert.setTitle("登录失败");
					alert.setHeaderText("用户名或者密码错误");
					alert.setContentText("请输入正确的用户名和密码");
					alert.showAndWait();
				}
			}
		});

		btn_cancel.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent ae) {
				stage.close();
			}
		});

		GridPane gridPane = new GridPane();
		Scene scene = new Scene(gridPane);

		gridPane.addRow(0, new Label(), lbl_userlogin);
		gridPane.addRow(1, lbl_username, txt_username);
		gridPane.addRow(2, lbl_password, pwf_password);
		gridPane.addRow(3, btn_login, btn_cancel);

		stage.setScene(scene);
		stage.show();
	}

	public static void main(String[] args) {
		Application.launch(args);

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值