2014年11月4日 星期二

如何在JavaFX中開啟新視窗?

JavaFX應用程式需繼承javafx.application.Application抽象類別,Application抽象類別類似於Java Swing的JApplet類別,以其「生命週期」執行JavaFX,分別為:
  • init():初始化JavaFX應用程式。
  • start():為JavaFX應用程式的進入點,在此為Java Applet。
  • launch():啟動獨立 (Standalone) 的JavaFX應用程式,在此為視窗程式。
  • stop():當JavaFX應用程式結束之前,將執行stop()方法。
其程式架構如下:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
...

// 繼承javafx.application.Application抽象類別
public class JavaFXHelloWorld extends Application {

  @Override
  public void start(Stage primaryStage) {
    ...
  }

  public static void main(String[] args) {
    launch(args);
  }
}
其中javafx.stage.Stage類別為JavaFX最上層的容器 (Container),類似於Java Swing的JRootPane類別,用以置放SceneSubScene物件。javafx.scene.Scene類別則是JavaFX Scene Graph,可藉以繪圖或配置各類GUI物件。

因此欲在原視窗中產生另一個新視窗,只要使用Stage類別則可,以下範例示範當點選原視窗中的按鈕時,則產生另一個新視窗:
public class StageDemo extends Application {

  @Override
  public void start(Stage primaryStage) {
    
    // Normal Button
    Button button = new Button();
    button.setText("New Window");
    button.setPrefSize(150, 20);
    
    button.setOnAction(event -> {
      newWindow();
    });
    
    BorderPane borderpane = new BorderPane();
    borderpane.setCenter(button);
    
    // Set the Layout Pane of Scene
    Scene scene = new Scene(borderpane);    
    // Set the title of Stage
    primaryStage.setTitle("Stage Demo");
    // Set the width of Stage
    primaryStage.setWidth(250);
    // Set the height of Stage
    primaryStage.setHeight(150);
    primaryStage.setScene(scene);
    primaryStage.setResizable(false);
    
    // Show Stage
    primaryStage.show();
  }
  
  private void newWindow(){
    Label label1 = new Label("Name:");
    Label label2 = new Label("Birthday:");
    Label label3 = new Label("Tel.:");
    
    TextField textfield1 = new TextField();
    textfield1.setText("Athena");
    textfield1.setPrefColumnCount(15);
    textfield1.selectAll();
    textfield1.setTooltip(new Tooltip("Name"));
    
    TextField textfield2 = new TextField();
    textfield2.setPrefColumnCount(15);
    textfield2.setAlignment(Pos.CENTER_RIGHT);
    textfield2.setCursor(Cursor.CROSSHAIR);
    textfield2.setTooltip(new Tooltip("Birthday"));

    TextField textfield3 = new TextField();
    textfield3.setText("1234567890");
    textfield3.setPrefColumnCount(15);
    textfield3.setAlignment(Pos.CENTER);
    textfield3.setCursor(Cursor.HAND);
    textfield3.selectAll();
    textfield3.setTooltip(new Tooltip("Phone Number"));
           
    GridPane gridpane = new GridPane();
    gridpane.setAlignment(Pos.CENTER);
    gridpane.setHgap(10);
    gridpane.setVgap(10);
    gridpane.setPadding(new Insets(5, 5, 5, 5));

    gridpane.add(label1, 0, 0);
    gridpane.add(label2, 0, 1);
    gridpane.add(label3, 0, 2);

    gridpane.add(textfield1, 1, 0);
    gridpane.add(textfield2, 1, 1);
    gridpane.add(textfield3, 1, 2);

    // Set the Layout Pane of Scene
    Scene scene = new Scene(gridpane);
    
    Stage stage = new Stage();

    // Set the title of Stage
    stage.setTitle("New Window");
    // Set the width of Stage
    stage.setWidth(300);
    // Set the height of Stage
    stage.setHeight(150);
    stage.setScene(scene);
    stage.setResizable(false);

    // Show Stage
    stage.show();
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    launch(args);
  }
}
【執行結果】

當點選原視窗中的按鈕時,則產生另一個新視窗:
【參考資料】

[1] Java Official Web Site:http://www.oracle.com/technetwork/java/index.html
[2] JavaFX:http://www.oracle.com/technetwork/java/javafx
[3] JavaFX 8.0 API Specification.
[4] Java Platform, Standard Edition 8 API Specification.

© Chia-Hui Huang

沒有留言:

張貼留言