JavaFX--第2天-窗口基本的类

 1 内部匿名类和Lambda表达式

 2 Switching Scene

 3 信息提示框 (Alert Boxes)

 前情回顾:

前面的学习内容:关于JavaFX的基本概念,以及窗口所使用的类的一个介绍

                              

             学习了如何运用事件对一个按钮做出最简单的回应—click me 点击。

 1 内部匿名类和Lambda表达式

在之前的例子上对 

button.setOnAction(this);

进行更改

button.setOnAction(new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent event){
        System.out.println("I am an annonymous inner class");
    }
});    

此时点击按钮调用的时间就是我们后来修改的,不用去检验每个按钮的名字,直接在生成对象之后对象的方法上调用内部类,使得事件发生。"Click me"。

但是后来会出现一个问题。按照上一次的想法我们有很多个按钮的时候会写出if条件结构,然后还要去对应代码中的对象,但是都使用内部匿名类也不方便。

甲骨文公司在Java 8中开始加入了Lambda表达式,此时将这个语句改成如下:

button.setOnAction(e-> System.out.println("heyyyyy, I am Lambda"));    

此时控制台对我们点击了按钮进行回应:heyyyyy, I am Lambda,Java自动帮我们处理这个事件。同时也可以改成

button.setOnAction(e->{
    System.out.println("heyyyyy, I am Lambda1");
    System.out.println("heyyyyy, I am Lambda2");
    System.out.println("heyyyyy, I am Lambda3");
    });    

 2 Switching Scene

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

public class Main extends Application{
    
    Stage window;
    Scene scene1,scene2;
    
    public static void main(String[] args) {
        launch(args);    
    }
    @Override
    public void start(Stage primaryStage) throws Exception{
        window = primaryStage;
        Label label1 = new Label("This is Scene1");
        Button button1 = new Button("Go to Scene2");
        button1.setOnAction(e -> window.setScene(scene2));
        //Layout 1 - children are laid out in vertical column
        VBox layout1 = new VBox(20);
        layout1.getChildren().addAll(label1,button1);
        scene1 = new Scene(layout1,200,200); //200x200 pixel
        
        //Button2 
        Button button2 = new Button("Go back to Scene1");
        button2.setOnAction(e -> window.setScene(scene1));    
        //layout2
        StackPane layout2 = new StackPane();
        layout2.getChildren().addAll(button2);
        scene2 = new Scene(layout2, 200, 200);    
        window.setScene(scene1);
        window.setTitle("This is a title");
        window.show();
    }
}

研究Scene1和Scene2 的两种不同的情况,Scene的切换通过点击Button来实现。这个例子看起来有点像我们平时使用的软件,比如说我们要关闭一个word文档的时候会发现此时,系统弹出一个窗口,问是否保存。有时候系统出错,也会弹出一个窗口来提示错误。下面将介绍具体的例子。

 3 信息提示框 (Alert Boxes)

点击按键之后弹出对话框

此时就很像我们实现AlertBox,如果不解决新弹出窗口,比如关闭,那么旧的窗口就不能操作。

public class AlertBox {
        
    public static void display(String title, String message){
        Stage window = new Stage();    // make a new Stage for our Scene
            window.initModality(Modality.APPLICATION_MODAL);  //initiate the Mod by the using the Java Library
        window.setTitle(title);    //Set the title of the new window
        window.setMinWidth(250);    
        Label label1 = new Label();  //make label to write some message
        label1.setText(message);        
        Button closeButton = new Button("Close the window");
        closeButton.setOnAction(e ->window.close());
        
        VBox layout = new VBox(10);  // make the Alert box layout
        layout.getChildren().addAll(label1, closeButton); //Add the Button and label to the window
        layout.setAlignment(Pos.CENTER);  
        
        Scene scene = new Scene (layout);
        window.setScene(scene);
        window.show();
        window.showAndWait();
    }
        
}
  • showAndWait 官方解释

    public void showAndWait()
    Shows this stage and waits for it to be hidden (closed) before returning to the caller. This method temporarily blocks processing of the current event, and starts a nested event loop to handle other events. This method must be called on the FX Application thread.

    A Stage is hidden (closed) by one of the following means:

    • the application calls the Window.hide() or close() method on this stage
    • this stage has a non-null owner window, and its owner is closed
    • the user closes the window via the window system (for example, by pressing the close button in the window decoration)
原文地址:https://www.cnblogs.com/rynerlute/p/6387947.html