2012-04-03 91 views
12

如何在JavaFX 2.0中顯示新窗口?例如在按鈕點擊操作之後。 我希望這兩個窗口(主窗口和新窗口)相互通信。JavaFX 2.0子窗口

Thx尋求幫助。

+2

我認爲在Ensemble中有一個例子。 – assylias 2012-04-03 17:01:09

回答

17
new Stage(new Scene(new Group(new Text(10,10, "my second window")))).show(); 

兩個窗口之間的通信類似於Java中的任何兩個對象。

+7

Thx。它適用於一個小的修改;-)'Stage stage = new Stage(); (新的組(新的文本(10,10,「我的第二個窗口」)))); stage.show();' – fxuser 2012-04-03 17:20:42

+0

right,my bad :) – 2012-04-03 17:26:45

13

通過調用new Stage()來創建新窗口,並通過stage.show()顯示它們。

以下是使用複選框控件創建新舞臺的示例,該複選框控件可修改顯示在不同舞臺中的標籤的文本。

import javafx.application.Application; 
import javafx.beans.value.*; 
import javafx.event.EventHandler; 
import javafx.scene.*; 
import javafx.scene.control.*; 
import javafx.scene.layout.StackPane; 
import javafx.stage.*; 

public class SecondStage extends Application { 
    public static void main(String[] args) { launch(args); } 
    @Override public void start(Stage primaryStage) { 
    // setup some dymamic data to display. 
    final String STANDARD_TEXT = "Every Good Boy Deserves Fruit"; 
    final String ALTERNATE_TEXT = "Good Boys Deserve Fruit Always";   
    final Label label = new Label(STANDARD_TEXT); 

    // configure the primary stage. 
    StackPane primaryLayout = new StackPane(); 
    primaryLayout.getChildren().add(label); 
    primaryLayout.setStyle("-fx-background-color: lightgreen; -fx-padding: 10;"); 
    primaryStage.setScene(new Scene(primaryLayout, 200, 100)); 
    primaryStage.setTitle("Primary Stage"); 

    // configure the secondary stage. 
    final Stage secondaryStage = new Stage(StageStyle.UTILITY); 
    CheckBox alternateTextCheck = new CheckBox("Show alternate text"); 
    alternateTextCheck.selectedProperty().addListener(new ChangeListener<Boolean>() { 
     @Override public void changed(ObservableValue<? extends Boolean> selected, Boolean oldValue, Boolean newValue) { 
     if (newValue) label.setText(ALTERNATE_TEXT); else label.setText(STANDARD_TEXT); 
     } 
    }); 
    StackPane secondaryLayout = new StackPane(); 
    secondaryLayout.getChildren().add(alternateTextCheck); 
    secondaryLayout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;"); 
    secondaryStage.setScene(new Scene(secondaryLayout, 200, 100)); 
    secondaryStage.setTitle("Secondary Stage"); 

    // specify stage locations. 
    secondaryStage.setX(400); secondaryStage.setY(200); 
    primaryStage.setX(400); primaryStage.setY(350); 

    // add a trigger to hide the secondary stage when the primary stage is hidden. 
    // this will cause all stages to be hidden (which will cause the app to terminate). 
    primaryStage.setOnHidden(new EventHandler<WindowEvent>() { 
     @Override public void handle(WindowEvent onClosing) { 
     secondaryStage.hide(); 
     } 
    }); 

    // show both stages. 
    primaryStage.show(); 
    secondaryStage.show(); 
    } 
} 
+0

如果我希望兩個窗口在用戶關閉其中一個時關閉並終止應用程序,該怎麼辦? – SasQ 2016-04-13 09:09:44

+0

@SasQ請將新問題作爲新問題提出。 – jewelsea 2016-04-13 16:35:37

+0

這個問題跟隨原始問題,因此我在這裏提出。如果你打算有所幫助,那麼我不需要你的幫助。我已經知道了自己。 – SasQ 2016-04-14 15:09:34

0

裏面的按鈕點擊動作,你可以創建一個新的舞臺,然後你要顯示的其他類的對象。之後使用創建的對象調用start方法。

 Stage stage= new Stage(); 
     NewClass nc= new NewClass(); 
     nc.start(stage); 

希望這會起作用!