[注] javafx、ログインインターフェイスのデモ、ハイパーリンクジャンプ

ログインする

2つのカテゴリ:

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

public class Main_24 extends Application {
    
    
    public static void main(String[] args) {
    
    
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
    
    
        //声明组件
        Label l_name = new Label("账号");
        Label l_passwd = new Label("密码");
        TextField t_name = new TextField();
        PasswordField p_passwd = new PasswordField();
        Button b_login = new Button("登录");
        Button b_clear = new Button("清除");

        //对组件进行设置
        l_name.setFont(Font.font(20.0));
        l_passwd.setFont(Font.font(20.0));
        t_name.setUserData("12345");//类似于map,自带的属性
        p_passwd.setUserData("12345");
//        t_name.getUserData();
//        p_passwd.getUserData();

        b_login.setFont(Font.font(18.0));
        b_clear.setFont(Font.font(18.0));

        //文本框里的灰色提示
        t_name.setPromptText("请输入账号");
        p_passwd.setPromptText("请输入密码");

        GridPane gr = new GridPane();
        //设置背景
        gr.setStyle("-fx-background-color:linear-gradient(to bottom right, #C1FFC1, #FF8C00, yellow)");
        gr.setAlignment(Pos.CENTER);
        gr.setHgap(30);//水平间距
        gr.setVgap(15);//垂直间距
        gr.add(l_name, 0,0);
        gr.add(t_name,1,0);
        gr.add(l_passwd,0,1);
        gr.add(p_passwd,1,1);
        gr.add(b_clear,0,2);
        gr.add(b_login,1,2);
        GridPane.setHalignment(b_login, HPos.RIGHT);

        Scene scene = new Scene(gr);
        primaryStage.setScene(scene);
        primaryStage.setTitle("登录");
        primaryStage.setWidth(500);
        primaryStage.setHeight(300);
        primaryStage.setResizable(false);
        primaryStage.show();

        //动作设置
        //清除
        b_clear.setOnAction(new EventHandler<ActionEvent>() {
    
    
            @Override
            public void handle(ActionEvent event) {
    
    
                t_name.clear();
                p_passwd.clear();
            }
        });

        //登录
        b_login.setOnAction(new EventHandler<ActionEvent>() {
    
    
            @Override
            public void handle(ActionEvent event) {
    
    
                //用户没有输入任何值
                if(t_name.getText().isEmpty() || p_passwd.getText().isEmpty()){
    
    
                    System.out.println("登陆失败");
                    return;
                }
                String name = (String)t_name.getUserData();
                String passwd = (String)p_passwd.getUserData();

                if(name.equals(t_name.getText()) && passwd.equals(p_passwd.getText())){
    
    
                    System.out.println("登陆成功");
                    new MyWindow(name);

                    primaryStage.close();
                }
                else{
    
    
                    System.out.println("登陆失败");

                    FadeTransition fade = new FadeTransition();
                    fade.setDuration(Duration.seconds(0.1));
                    fade.setNode(gr);
                    fade.setFromValue(0);
                    fade.setToValue(1);
                    fade.play();

                    primaryStage.setTitle("登录-账号或密码错误!!");
                }
            }
        });
    }
}
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;


public class MyWindow {
    
    
    private final Stage stage = new Stage();
    public MyWindow(String id){
    
    
        //组件
        Text text = new Text("id = " + id);

        //布局
        BorderPane bor = new BorderPane();
        bor.setStyle("-fx-background-color:#FFF0F5");
        bor.setCenter(text);

        //传值
        Scene scene = new Scene(bor);
        stage.setScene(scene);
        stage.setTitle("登陆成功");//标题
        stage.setHeight(500);//高
        stage.setWidth(500);//宽
        stage.show();
    }
}

効果:

ここに画像の説明を挿入します

ハイパーリンクジャンプ:

import javafx.application.Application;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main_25 extends Application {
    
    
    public static void main(String[] args) {
    
    
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
    
    
        //布局
        VBox vBox = new VBox();

        //超链接效果
        Hyperlink link = new Hyperlink("www.baidu.com",new Button("跳转"));
        //设置点击事件
        link.setOnAction(new EventHandler<ActionEvent>() {
    
    
            @Override
            public void handle(ActionEvent event) {
    
    
                //用默认浏览器打开超链接
                HostServices host = Main_25.this.getHostServices();
                host.showDocument(link.getText());
            }
        });

        vBox.getChildren().add(link);

        Scene scene = new Scene(vBox);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX");
        primaryStage.setWidth(600);
        primaryStage.setHeight(300);
        primaryStage.show();
    }
}

効果:
ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/qq_43750882/article/details/110140674