1 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
我也遇到同樣的情況。如果我嘗試最大化使用
stage.setMaximized(true);
它不起作用,最大化按鈕也不顯示。我正在研究這個(gè)問題,但根本沒有答案。這是我在這里發(fā)現(xiàn)的第一個(gè)類似問題。我在用著:
操作系統(tǒng):GNU/Linux
發(fā)行版:Manjaro
Linux 核心:5.3.6-1
來自:侏儒。
Java版本:OpenJDK 12.0.1
JavaFX 版本:OpenJFX 12.0.1(膠子構(gòu)建)。
更新:官方文檔指出:
請(qǐng)注意,顯示模態(tài)階段并不一定會(huì)阻止調(diào)用者。
因此,我決定使用 EventHandler 來解決該問題。我創(chuàng)建了一個(gè)實(shí)用程序類來處理這個(gè)問題。
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.
}
這是我的解決方案,經(jīng)過測(cè)試并且工作正常。當(dāng)顯示子窗口(newStage)時(shí),所有者(primaryStage)將被禁用,因此盡管用戶可以激活primaryStage窗口,但用戶無法與其節(jié)點(diǎn)交互。
添加回答
舉報(bào)