写了一个JavaFx版的登陆界面

想用JavaFx扩展原有的B/S结构的Web应用程序,JavaFx目前处于1.2版,很多东西都还处于快速的发展中,要做程序,只能是自己边学边干,这里是自己做的一个登陆界面,界面 如下:


 

 

 

代码如下:

 

 package page;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.paint.Color;
import javafx.scene.layout.VBox;
import javafx.scene.control.TextBox;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.Group;
import javafx.stage.Alert;
import javafx.scene.text.Text;

/**
 * 开始于 2010-01-28
 * javaFx版的威博文件管理系统
 * 登陆页面
 * 最后修改于 2010-02-08
 * @author 万继斌
 * @version 1.2
 */
//定义舞台场景的宽度
def sceneWidth = 640;
//定义舞台场景的高度
def sceneHeight = 480;
//定义用户名组合对象
def userName = Group {
            content: [
                HBox {
                    spacing: 5
                    content: [
                        Label {
                            text: "用户:"
                        }
                        TextBox {
                            text: ""
                            columns: 15
                            selectOnFocus: true
                        }
                    ]
                }
            ]
        }
//定义用户名组合对象
def userPassword = Group {
            content: [
                HBox {
                    spacing: 5
                    content: [
                        Label {
                            text: "密码:"
                        }
                        TextBox {
                            text: ""
                            columns: 15
                            selectOnFocus: true
                        }
                    ]
                }
            ]
        }
//提示信息
def message = Text {
            content: "处理提示信息"
        }
//定义提交按钮组合对象
def submit = Group {
            content: [
                HBox {
                    content: [
                        Button {
                            text: "登陆系统"
                            action: function () {
                                if (Alert.question("提交处理", "提交处理吗?")) {
                                    message.content = "是,提交";
                                    } else {
                                    message.content = "否,放弃";
                                    }
                                }
                        }
                    ]
                }
            ]
        }
//定义登陆矩形背景
def bgRectangle = Rectangle {
            x: sceneWidth / 2 - 120;
            y: sceneHeight / 2 - 120;
            width: 240
            height: 180
            arcWidth: 20
            arcHeight: 20
            opacity: 0.5
            stroke: Color.YELLOWGREEN
            strokeWidth: 2
            fill: Color.WHITESMOKE
        }
//定义垂直布局
def vbox = VBox {
            layoutX: bgRectangle.layoutBounds.minX + 20
            layoutY: bgRectangle.layoutBounds.minY + 20
            spacing: 20
            content: [
                userName,
                userPassword,
                submit,
                message
            ]
        }

//舞台
Stage {
    title: "威博文件管理系统"
    scene: Scene {
        //初始场景,按照640*480的屏幕大小设计
        width: sceneWidth;
        height: sceneHeight
        //场景填色,使用线性渐变
        fill: LinearGradient {
            startX: 1; endX: 1 //X坐标方向不渐变
            startY: 0; endY: 1 //Y坐标方向渐变
            stops: [
                Stop {
                    offset: 0
                    color: Color.LIGHTGREEN
                },
                Stop {
                    offset: 1
                    color: Color.LIGHTCYAN
                }
            ]
        }//fill: LinearGradient
        //场景内容
        content: [
            bgRectangle,
            vbox
        ]//场景内容组
    }//场景
}
 

 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的JavaFX登陆界面连接数据库的示例: 首先,我们需要创建一个包含用户名和密码字段的数据库表。假设我们已经创建了一个名为“users”的表,并且包含“username”和“password”两个字段。 接下来,我们需要创建一个JavaFX界面。在这个例子中,我们将创建一个包含两个文本框和两个按钮的界面,用于用户输入用户名和密码,以及进行登录和注册操作。 ```java import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; 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.scene.text.FontWeight; import javafx.stage.Stage; public class LoginApp extends Application { @Override public void start(Stage primaryStage) throws Exception { // 创建一个网格布局 GridPane grid = new GridPane(); grid.setAlignment(Pos.CENTER); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(25, 25, 25, 25)); // 添加界面元素 Label sceneTitle = new Label("Welcome"); sceneTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20)); grid.add(sceneTitle, 0, 0, 2, 1); Label userName = new Label("User Name:"); grid.add(userName, 0, 1); TextField userTextField = new TextField(); grid.add(userTextField, 1, 1); Label pw = new Label("Password:"); grid.add(pw, 0, 2); PasswordField pwBox = new PasswordField(); grid.add(pwBox, 1, 2); Button loginBtn = new Button("Login"); Button registerBtn = new Button("Register"); HBox hbBtn = new HBox(10); hbBtn.setAlignment(Pos.BOTTOM_RIGHT); hbBtn.getChildren().addAll(loginBtn, registerBtn); grid.add(hbBtn, 1, 4); // 创建一个场景并设置到主舞台 Scene scene = new Scene(grid, 300, 275); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 接下来,我们需要添加登录和注册的功能。我们将使用JavaFX的事件处理程序来实现这些功能。对于登录操作,我们将检查用户输入的用户名和密码是否匹配数据库中的记录。对于注册操作,我们将在数据库中创建一个新的用户记录。 ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; 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.layout.HBox; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.stage.Stage; public class LoginApp extends Application { @Override public void start(Stage primaryStage) throws Exception { // 创建一个网格布局 GridPane grid = new GridPane(); grid.setAlignment(Pos.CENTER); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(25, 25, 25, 25)); // 添加界面元素 Label sceneTitle = new Label("Welcome"); sceneTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20)); grid.add(sceneTitle, 0, 0, 2, 1); Label userName = new Label("User Name:"); grid.add(userName, 0, 1); TextField userTextField = new TextField(); grid.add(userTextField, 1, 1); Label pw = new Label("Password:"); grid.add(pw, 0, 2); PasswordField pwBox = new PasswordField(); grid.add(pwBox, 1, 2); Button loginBtn = new Button("Login"); Button registerBtn = new Button("Register"); HBox hbBtn = new HBox(10); hbBtn.setAlignment(Pos.BOTTOM_RIGHT); hbBtn.getChildren().addAll(loginBtn, registerBtn); grid.add(hbBtn, 1, 4); // 添加事件处理程序 loginBtn.setOnAction(event -> { String username = userTextField.getText(); String password = pwBox.getText(); if (verifyLogin(username, password)) { // 登录成功,跳转到主界面 // TODO: 实现跳转逻辑 } else { // 登录失败,显示错误信息 sceneTitle.setText("Login Failed!"); } }); registerBtn.setOnAction(event -> { String username = userTextField.getText(); String password = pwBox.getText(); if (registerUser(username, password)) { // 注册成功,显示成功信息 sceneTitle.setText("Registration Successful!"); } else { // 注册失败,显示错误信息 sceneTitle.setText("Registration Failed!"); } }); // 创建一个场景并设置到主舞台 Scene scene = new Scene(grid, 300, 275); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } // 验证登录 private boolean verifyLogin(String username, String password) { try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false", "root", "password"); PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?")) { stmt.setString(1, username); stmt.setString(2, password); ResultSet rs = stmt.executeQuery(); return rs.next(); } catch (SQLException e) { e.printStackTrace(); return false; } } // 注册用户 private boolean registerUser(String username, String password) { try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false", "root", "password"); PreparedStatement stmt = conn.prepareStatement("INSERT INTO users (username, password) VALUES (?, ?)")) { stmt.setString(1, username); stmt.setString(2, password); stmt.executeUpdate(); return true; } catch (SQLException e) { e.printStackTrace(); return false; } } } ``` 注意,在这个例子中,我们使用了Java的JDBC API来连接数据库。你需要根据你的数据库类型和配置来修改这个代码中的连接字符串、用户名和密码。同时,你也需要将MySQL驱动程序添加到你的项目中,以便在运行时加载它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值