21.JavaFx 阶段Demo

package cn.tfinfo.tools.client.demo;

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
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.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *  demo 初阶版
 */
public class Demo23 extends Application {

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

        Label usernameLabel = new Label("账号:");
        usernameLabel.setFont(new Font(14));
        TextField username = new TextField();
        username.setUserData("root");

        Label passwordLabel = new Label("密码:");
        passwordLabel.setFont(new Font(14));
        PasswordField password = new PasswordField();
        password.setUserData("root");

        Button clearButton = new Button("清除");
        Button loginButton = new Button("登录");

        GridPane gridPane = new GridPane();
        gridPane.setStyle("-fx-background-color: #FFF5EE");
        gridPane.add(usernameLabel, 0, 0);
        gridPane.add(username, 1, 0);
        gridPane.add(passwordLabel, 0, 1);
        gridPane.add(password, 1, 1);
        gridPane.add(clearButton, 0, 2);
        gridPane.add(loginButton, 1, 2);

        gridPane.setHgap(5);
        gridPane.setVgap(15);
        gridPane.setAlignment(Pos.CENTER);
        GridPane.setMargin(loginButton, new Insets(0, 0, 0, 120));


        Scene scene = new Scene(gridPane);
        primaryStage.getIcons().add(new Image("icon/tf_logo.png"));
        primaryStage.setScene(scene);
        primaryStage.setTitle("Tools Platform");
        primaryStage.setWidth(512);
        primaryStage.setHeight(512);
        primaryStage.setResizable(false);
        primaryStage.show();

        clearButton.setOnAction(event -> {
            username.clear();
            password.clear();
        });

        loginButton.setOnAction(event -> {
            if (null != username.getText() && username.getText().equals(username.getUserData())
                    && null != password.getText() && password.getText().equals(password.getUserData())) {
                System.out.println("login success");
                primaryStage.close();
                new MainWindow(username.getText(), password.getText());
            } else {
                System.out.println("login error");

                usernameLabel.setTextFill(Color.RED);
                passwordLabel.setTextFill(Color.RED);

                primaryStage.setTitle("login error");
                // 闪烁
                FadeTransition fadeTransition = new FadeTransition();
                fadeTransition.setDuration(Duration.millis(300));
                fadeTransition.setNode(gridPane);
                fadeTransition.setFromValue(0);
                fadeTransition.setToValue(1);
                fadeTransition.play();
            }
        });
    }

}

class MainWindow {
    private final Stage stage = new Stage();

    private String username;

    private String password;

    public MainWindow(String username, String password) {
        this.username = username;
        this.password = password;

        BorderPane borderPane = new BorderPane();
        borderPane.setStyle("-fx-background-color: #FFF5EE");
        Scene scene = new Scene(borderPane);
        stage.setScene(scene);
        stage.setTitle("Tools Platform");
        stage.getIcons().add(new Image("icon/logo.png"));
        stage.setWidth(1024);
        stage.setHeight(600);
        stage.setResizable(false);
        stage.show();
    }

}

运行效果预览

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Exception in Application constructor Exception in thread "main" java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071) Caused by: java.lang.RuntimeException: Unable to construct Application instance: class com.example.demo.Main at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:891) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access class com.example.demo.Main (in module com.example.javafxtest) because module com.example.javafxtest does not export com.example.demo to module javafx.graphics at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392) at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:489) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:803) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456) at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184) ... 1 more
05-24

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值