我有一个JavaFX应用程序,它有一个模式窗口,将主应用程序窗口设置为父应用程序窗口。当该弹出窗口出现时,我的 Ubuntu 任务切换器(alt-tab(似乎认为这是一个完全不同的窗口;它和主应用程序窗口都显示为选项。如何配置 JavaFX,以便此窗口不会在 alt-tab 中显示为单独的选项?
下面是一个最小示例:
public class PopupExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Stage window = new Stage();
window.initOwner(primaryStage);
window.initModality(Modality.APPLICATION_MODAL);
window.show();
}
}
嗯,这很简单,只需将StageStyle.UTILITY
设置为第二个Stage
的initStyle
即可。通过执行此操作,Alt+Tab
将显示一个窗口。
以下代码演示了如何处理此问题:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class PopupExample extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
VBox vBox = new VBox();
vBox.setAlignment(Pos.TOP_CENTER);
Button showPopUpbutton = new Button("Show Stage_TWO");
showPopUpbutton.setOnAction(event -> showPopup());
vBox.getChildren().add(showPopUpbutton);
Scene scene = new Scene(vBox, 300, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("Stage_One");
primaryStage.show();
}
private void showPopup()
{
Stage stage = new Stage();
VBox vBox = new VBox();
vBox.setAlignment(Pos.TOP_CENTER);
Label label = new Label("Stage_TWO");
Button closeStageButton = new Button("Close Stage_TWO");
closeStageButton.setOnAction(event -> stage.close());
vBox.getChildren().addAll(label, closeStageButton);
Scene scene = new Scene(vBox, 200, 200);
stage.setScene(scene);
stage.initStyle(StageStyle.UTILITY);
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Stage_TWO");
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}