繁体   English   中英

块所有者窗口 Java FX

[英]Block owner window Java FX

我想在 JavaFX 中阻止弹出窗口的所有者窗口。

我像这样初始化我的弹出窗口:

popUp = new Popup();
popUp.getContent().add(content);
popUp.show(pane.getScene().getWindow());

有了这个,我仍然可以在第一个窗口(窗格窗口)中工作。 我想禁用此操作,并且我希望用户只在弹出窗口中工作。

这个怎么做 ?

谢谢。

使用Stage而不是Popup

在显示舞台之前,根据需要调用stage.initModality作为APPLICATION_MODALWINDOW_MODAL 还要调用stage.initOwner到新阶段的父窗口,以便在WINDOW_MODAL情况下适当地阻止它。

Stage stage = new Stage();
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(pane.getScene().getWindow());
stage.setScene(new Scene(content));
stage.show();

谢谢,最佳解决方案:带有 FXML 加载文件的示例:

@Override
    public void start(Stage primaryStage) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("DialogView.fxml"));
        primaryStage.initModality(Modality.APPLICATION_MODAL); // 1 Add one
        Scene scene = new Scene(root);        
        primaryStage.setScene(scene);
        primaryStage.initOwner(primaryStage.getScene().getWindow());// 2 Add two
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);

    }

我正在使用 JavaFX11。 无论这个这个作品有小的变化。 这是我的工作示例。 我正在使用rgielen 的 javafx-weaver

主要的:

package com.featuriz.controller;

import javafx.fxml.FXML;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import net.rgielen.fxweaver.core.FxControllerAndView;
import net.rgielen.fxweaver.core.FxmlView;
import org.springframework.stereotype.Component;

@Component
@FxmlView("/com/featuriz/ui/Login.fxml")
public class LoginController {
    
    private final FxControllerAndView<InfoDialogController, AnchorPane> infoDialog;

    @FXML
    private Text featuriz;

    public LoginController(FxControllerAndView<InfoDialogController, AnchorPane> infoDialog) {
        this.infoDialog = infoDialog;
    }

    @FXML
    public void initialize() {
        featuriz.setOnMouseClicked(
                actionEvent -> this.infoDialog.getController().show()
        );
    }
}

对话:

package com.featuriz.controller;

import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import net.rgielen.fxweaver.core.FxmlView;
import org.springframework.stereotype.Component;

@Component
@FxmlView("/com/featuriz/ui/InfoDialog.fxml")
public class InfoDialogController {

    private Stage stage;

    @FXML
    private Label lbl_info;

    @FXML
    private Button closeButton;

    @FXML
    private AnchorPane dialog_info;

    @FXML
    public void initialize() {
        Scene scene = new Scene(dialog_info);
        scene.setFill(Color.TRANSPARENT);
        this.stage = new Stage();
        this.stage.initModality(Modality.APPLICATION_MODAL);
        this.stage.initStyle(StageStyle.TRANSPARENT);
        this.stage.setScene(scene);
        this.stage.setAlwaysOnTop(true);

        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        lbl_info.setText("JavaFX " + javafxVersion + "\nRunning on Java " + javaVersion + ".");

        closeButton.setOnAction(
                actionEvent -> this.stage.close()
        );
    }

    public void show() {
        this.stage.show();
    }

}

你只需要从 javafx.stage.Modality 添加 Modality 资源;

然后添加行 stage.initModality(Modality.APPLICATION_MODAL);

这里有一个例子

 Stage stage = new Stage(); Parent root = FXMLLoader.load(getClass().getResource( -- pathInterface --)); root.getStylesheets().add(getClass().getResource( -- pathCss --).toExternalForm()); stage.initStyle(StageStyle.DECORATED.UNDECORATED); stage.initModality(Modality.APPLICATION_MODAL);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM