java - 在JavaFX中同时进行场景和舞台交互

标签 java user-interface javafx

嗨,我正在尝试创建一个应用程序,但是如果我在舞台顶部打开一个场景,我无法与两个窗口交互,只能与顶部的窗口交互。经过研究,我猜测这是不可能完成的,我必须修改代码以将大 MainApp 分成较小的类,并使用 Platform.runLater 和一个主类中的线程在单独的线程上加载每个场景和阶段。我已经尝试过了,但没有成功。

public class MainApp extends Application {

private Stage primaryStage;
private BorderPane rootLayout;


@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("AddressApp");

    // Set the application icon.
    this.primaryStage.getIcons().add(new Image("file:resources/images/address_book_32.png"));

    initRootLayout();

    showPersonOverview();
}

/**
 * Initializes the root layout and tries to load the last opened
 * person file.
 */
public void initRootLayout() {
    try {
        // Load root layout from fxml file.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class
                .getResource("view/RootLayout.fxml"));
        rootLayout = (BorderPane) loader.load();

        // Show the scene containing the root layout.
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);

        // Give the controller access to the main app.
        RootLayoutController controller = loader.getController();
        controller.setMainApp(this);

        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Try to load last opened person file.
    File file = getPersonFilePath();
    if (file != null) {
        loadPersonDataFromFile(file);
    }
}

/**
 * Opens a dialog to show birthday statistics.
 */
public void showBirthdayStatistics() {
    try {
        // Load the fxml file and create a new stage for the popup.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/BirthdayStatistics.fxml"));
        AnchorPane page = (AnchorPane) loader.load();
        Stage dialogStage = new Stage();
        dialogStage.setTitle("Birthday Statistics");
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.initOwner(primaryStage);
        Scene scene = new Scene(page);
        dialogStage.setScene(scene);

        // Set the dialog icon.
        dialogStage.getIcons().add(new Image("file:resources/images/calendar.png"));

        // Set the persons into the controller.
        BirthdayStatisticsController controller = loader.getController();
        controller.setPersonData(personData);

        dialogStage.show();

    } catch (IOException e) {
        e.printStackTrace();
    }
}



/**
 * Returns the main stage.
 * @return
 */
public Stage getPrimaryStage() {
    return primaryStage;
}

public static void main(String[] args) {
    launch(args);
}
}

enter image description here

在上图中,我只能与生日静态场景交互,而不能与主舞台交互。

最佳答案

行:

dialogStage.initModality(Modality.WINDOW_MODAL);

使您的新窗口成为一个模态窗口,阻止其他窗口的任何事件。

您应该将其替换为:

dialogStage.initModality(Modality.NONE);

您可以阅读更多here

关于java - 在JavaFX中同时进行场景和舞台交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51693950/

相关文章:

java - 用于存储键值对的新数据结构

java - 将 ObservableSet 中值的存在与属性绑定(bind)

python - wxPython SplitterWindow 不会在面板内展开

java - 有没有类似于 Swing Builder 的用于 java-fx 的 GUI 构建器?

java - Javafx 上每个系列的带有复选框的 AreaCharts

java - Android应用使用Socket收发消息 :

java - 将java应用程序从bitbucket存储库部署到AWS : instance error

java - 如何在 Ubuntu 中将 Java 从 1.6.0 降级到 1.5.0?

java - 尝试在 GUI 中使用 Jpanel 进行绘制

user-interface - 自定义 UI 元素 CSS 填充问题