1 回答

TA貢獻1816條經驗 獲得超4個贊
我也遇到同樣的情況。如果我嘗試最大化使用
stage.setMaximized(true);
它不起作用,最大化按鈕也不顯示。我正在研究這個問題,但根本沒有答案。這是我在這里發現的第一個類似問題。我在用著:
操作系統:GNU/Linux
發行版:Manjaro
Linux 核心:5.3.6-1
來自:侏儒。
Java版本:OpenJDK 12.0.1
JavaFX 版本:OpenJFX 12.0.1(膠子構建)。
更新:官方文檔指出:
請注意,顯示模態階段并不一定會阻止調用者。
因此,我決定使用 EventHandler 來解決該問題。我創建了一個實用程序類來處理這個問題。
import javafx.event.Event;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
/**
* This is a utility class to create a Widow Event handler
* in such way that when a child window shows, a parent (owner)
* stage get locked; and when the child window hides, the parent
* stage get unlocked.
*
* @author David Vidal
* @version 1.0
* @since 1.0
*/
public final class WindowsModality {
/*=================*
* Private fields. *
*=================*/
/**
* The parent stage.
*/
private final Stage owner;
/*===============*
* Constructors. *
*===============*/
/**
* Initialize an instance with given parameters.
*
* @param owner parent stage.
* @param child child stage.
*/
public WindowsModality(Stage owner, Stage child) {
this.owner = owner;
child.addEventHandler(WindowEvent.WINDOW_HIDDEN, this::childHidden);
child.addEventHandler(WindowEvent.WINDOW_SHOWN, this::childShown);
}
/*==================*
* Implementations. *
*==================*/
/**
* Implementation of functional interface EventHandler,
* used to know when the child window is closed/hidden.
*
* @param event from {@link javafx.event.EventHandler#handle(Event)}
*/
private void childHidden(WindowEvent event) {
if (!event.isConsumed()) {
owner.getScene().getRoot().setDisable(false);
}
}
/**
* Implementation of functional interface EventHandler,
* used to know when the child window is shown.
*
* @param event from {@link javafx.event.EventHandler#handle(Event)}
*/
private void childShown(WindowEvent event) {
if (!event.isConsumed()) {
owner.getScene().getRoot().setDisable(true);
}
}
}
然后,我剛剛添加了以下代碼:
public void showAnotherStage(){
//(Create and setup the new stage and scene)
new WindowModality(primaryStage, newStage);
newStage.show();
//Do something else.
}
這是我的解決方案,經過測試并且工作正常。當顯示子窗口(newStage)時,所有者(primaryStage)將被禁用,因此盡管用戶可以激活primaryStage窗口,但用戶無法與其節點交互。
添加回答
舉報