JavaFX--第2天-視窗基本的類

來源:互聯網
上載者:User

標籤:except   lock   static   ssi   lambda   基本概念   ack   ini   proc   

 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)

     

 

JavaFX--第2天-視窗基本的類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.