我有一个按钮,点击后会弹出新的窗口,但主窗口仍然可以被点击,如何禁用主窗口?
这是我的舞台和场景代码:
```html
这是我的舞台和场景代码:
```html
stage和scene的代码如下:
```private static final Stage stage = new Stage();
private static final Stage newstage = new Stage();
/**
*
*/
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MainMenu.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().addAll(this.getClass().getResource("background.css").toExternalForm());
stage.setScene(scene);
stage.show();
}
public void chgScene (String str) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource(str));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void addStage (String str) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource(str));
Scene scene = new Scene(root);
newstage.setScene(scene);
newstage.setResizable(false);
newstage.showAndWait();
}
public void clearStage () {
newstage.close();
}
如果有任何错误,非常抱歉,因为我刚开始学习Java并需要为我的项目创建GUI界面。感谢您的帮助。
showAndWait
方法之前,您需要调用initModality
方法。 - KachnaStage
实例上,你只能调用一次initModality
方法。由于你在初始化程序中实例化了它并重复使用相同的实例,所以你应该在只执行一次的地方调用initModality
方法,例如在start()
方法中。此外,你也不应该将其设置为static
(这完全没有意义)。 - James_Dstatic
的意思是字段属于类,而不属于类的实例。这与来自其他类的对象的可访问性无关,由public
和private
等控制。(在逻辑处理中使用try - catch
结构也是错误的做法)。 - James_D