gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:04:15 25 4
gpt4 key购买 nike

嗨,我正在尝试创建一个应用程序,但是如果我在舞台顶部打开一个场景,我无法与两个窗口交互,只能与顶部的窗口交互。经过研究,我猜测这是不可能完成的,我必须修改代码以将大 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com