具有模态的舞台隐藏最大化按钮



当我设置舞台的模态时,它会隐藏最大化按钮

Stage stage = new Stage();
stage.setScene(new Scene((Parent) controller.getViewNode()));
stage.initStyle(StageStyle.DECORATED);
stage.setResizable(true);
stage.setIconified(false);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(window);

所以我希望舞台是一个模态,但也要显示最大化按钮,什么不会发生。顺便说一下,我正在使用Ubuntu。我进行了搜索,但我只找到了如何删除最大化按钮

同样的事情也发生在我身上。如果我尝试最大化使用

stage.setMaximized(true);

它不起作用,也不会显示最大化按钮。 我正在研究它,但根本没有答案。 这是我在这里发现的第一个类似问题。我正在使用:

  • 操作系统: GNU/Linux
  • 发行版:曼扎罗
  • Linux 核心版:5.3.6-1
  • 德:侏儒。
  • Java版本:OpenJDK 12.0.1
  • JavaFX 版本:OpenJFX 12.0.1(胶子构建(。

更新: 官方文件指出:

请注意,显示模式阶段不一定会阻止调用方。

因此,我决定使用事件处理程序来解决此问题。我创建了一个实用程序类来处理这个问题。

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( 时,所有者(主阶段(将被禁用,因此尽管用户可以激活主舞台窗口,但用户无法与其节点交互。

最新更新