馮斌:JavaFx實例(三)“MultipleStageDemo”

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class MultipleStageDemo extends Application {
  @Override // Override the start method in the Application class
  public voidstart(Stage primaryStage) {
    // Create a scene and place a button in the scene
    Scene scene = new Scene(new Button("OK"),200,250);
    primaryStage.setTitle("MyJavaFX");// Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show();// Display the stage
    Stage stage = new Stage();// Create a new stage
    stage.setTitle("Second Stage");// Set the stage title
    // Set a scene with a button in the stage
    stage.setScene(new Scene(new Button("New Stage"),100,100));       
    stage.show();// Display the stage
  }
}

說明:

1、本實例演示同時創建兩個窗口,運行結果如下:

wKiom1OFUMyy0qNLAABnrzA9ZWs054.jpg

2、在默認的情況下,用戶可自用更改窗口大小,如果想固定窗口大小,可以使用stage.setResizable(false)方法


3、本實例省略了main方法public static void main(String[] args) { Application.launch(args); }

   用命令行的執行上述代碼可以不需要main方法。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章