JavaFX 2.0子窗口

时间:2012-04-03 16:52:57

标签: user-interface javafx-2

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

寻求帮助。

3 个答案:

答案 0 :(得分:17)

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

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

答案 1 :(得分: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();
  }
}

答案 2 :(得分:0)

在按钮单击操作中,您可以创建新的satge,然后创建要显示的其他类的对象。之后使用创建的对象调用start方法。

      Stage stage= new Stage();
      NewClass nc= new NewClass();
      nc.start(stage);
希望这会起作用!!!