JavaFX 2.0子窗口

12

如何在JavaFX 2.0中显示新窗口?例如,在按钮点击操作后。 我希望两个窗口(主窗口和新窗口)彼此通信。

谢谢帮助。


2
我认为在Ensemble中有一个例子。 - assylias
3个回答

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

在Java中,两个窗口之间的通信与任何两个对象之间的通信类似。


7
谢谢!稍作修改后就可以运行了;-) Stage stage = new Stage(); stage.setScene(new Scene(new Group(new Text(10,10, "我的第二个窗口")))); stage.show(); - fxuser

13

通过调用new Stage()创建新窗口,然后通过stage.show()显示它们。

下面是一个示例,创建了一个新的Stage,并带有一个复选框控件,该控件可以修改在另一个Stage中显示的标签的文本内容。

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();
  }
}

如果我希望在用户关闭其中一个窗口时同时关闭并终止应用程序,该怎么办? - SasQ
@SasQ,请将新的问题作为新问题提出。 - jewelsea
这个问题是对原问题的追问,所以我在这里提出了它。如果你要这么热心地帮助我,那么我也不需要你的帮助了。我已经自己解决了。 - SasQ
我试图提供帮助,很遗憾你看到了不同的意思。 - jewelsea

0
在按钮点击事件中,您可以创建一个新的舞台,然后创建要显示的其他类的对象。之后,使用创建的对象调用start方法。
      Stage stage= new Stage();
      NewClass nc= new NewClass();
      nc.start(stage);

希望这个能够运行!!!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接