最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - JavaFX, dialog size in actual gnome - Stack Overflow

programmeradmin1浏览0评论

i have a problem with the dialog size. After the program-window, i open another dialogs. The dialogs has a size. This works with Windows, Linux with KDE and older Gnome (Ubuntu 22.04) but not with the actual GNOME (Ubuntu 24.04 or Manjaro). When the comment out lines are active, it works better in actual GNOME but also not always. I have amount of things tryed, but nothing works. Any idea?

This is a sample code with the minimal things to show the problem:

public class Muster extends Application {

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

    @Override
    public void init() throws Exception {
    }

    @Override
    public void start(Stage primaryStage) {
        show(primaryStage, null, "Dialog 1", false, 500, 500);
        show(null, primaryStage, "Dialog 2", false, 400, 400);
        show(null, primaryStage, "Dialog 3", false, 400, 400);
    }

    private static void show(Stage primaryStage, Stage owner, String title, boolean wait, int pos, int size) {
        VBox vBox = new VBox();
        Label lbl = new Label("Das ist ein Dialog");
        Button btn = new Button("Dialog");
        btn.setOnAction(a -> show(null, owner, "Wieder einer", true, 600, 600));
        vBox.getChildren().add(lbl);
        vBox.getChildren().add(btn);
        vBox.setAlignment(Pos.CENTER);

        try {
            Stage stage = primaryStage == null ? new Stage() : primaryStage;
            Scene scene = new Scene(vBox, size, size);
            stage.setScene(scene);
            stage.setTitle(title);
            if (owner != null) {
                stage.initOwner(owner);
            }

            stage.setWidth(size);
            stage.setHeight(size);
            stage.setX(pos);
            stage.setY(pos);

//            stage.setOnShowing(e -> {
//                stage.setWidth(size);
//                stage.setHeight(size);
//                stage.setX(pos);
//                stage.setY(pos);
//            });
//            stage.setOnShown(e -> {
//                stage.setWidth(size);
//                stage.setHeight(size);
//                stage.setX(pos);
//                stage.setY(pos);
//            });

            stage.setOnCloseRequest(e -> {
                System.out.println("closing width: " + stage.getWidth());
                System.out.println("closing height: " + stage.getHeight());
                System.out.println("closing X: " + stage.getX());
                System.out.println("closing Y: " + stage.getY());
            });

            stage.requestFocus();
            stage.toFront();
            if (wait) {
                stage.showAndWait();

            } else {
                stage.show();
            }
        } catch (final Exception exc) {
            System.out.println(exc.getMessage());
        }
    }
}

i have a problem with the dialog size. After the program-window, i open another dialogs. The dialogs has a size. This works with Windows, Linux with KDE and older Gnome (Ubuntu 22.04) but not with the actual GNOME (Ubuntu 24.04 or Manjaro). When the comment out lines are active, it works better in actual GNOME but also not always. I have amount of things tryed, but nothing works. Any idea?

This is a sample code with the minimal things to show the problem:

public class Muster extends Application {

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

    @Override
    public void init() throws Exception {
    }

    @Override
    public void start(Stage primaryStage) {
        show(primaryStage, null, "Dialog 1", false, 500, 500);
        show(null, primaryStage, "Dialog 2", false, 400, 400);
        show(null, primaryStage, "Dialog 3", false, 400, 400);
    }

    private static void show(Stage primaryStage, Stage owner, String title, boolean wait, int pos, int size) {
        VBox vBox = new VBox();
        Label lbl = new Label("Das ist ein Dialog");
        Button btn = new Button("Dialog");
        btn.setOnAction(a -> show(null, owner, "Wieder einer", true, 600, 600));
        vBox.getChildren().add(lbl);
        vBox.getChildren().add(btn);
        vBox.setAlignment(Pos.CENTER);

        try {
            Stage stage = primaryStage == null ? new Stage() : primaryStage;
            Scene scene = new Scene(vBox, size, size);
            stage.setScene(scene);
            stage.setTitle(title);
            if (owner != null) {
                stage.initOwner(owner);
            }

            stage.setWidth(size);
            stage.setHeight(size);
            stage.setX(pos);
            stage.setY(pos);

//            stage.setOnShowing(e -> {
//                stage.setWidth(size);
//                stage.setHeight(size);
//                stage.setX(pos);
//                stage.setY(pos);
//            });
//            stage.setOnShown(e -> {
//                stage.setWidth(size);
//                stage.setHeight(size);
//                stage.setX(pos);
//                stage.setY(pos);
//            });

            stage.setOnCloseRequest(e -> {
                System.out.println("closing width: " + stage.getWidth());
                System.out.println("closing height: " + stage.getHeight());
                System.out.println("closing X: " + stage.getX());
                System.out.println("closing Y: " + stage.getY());
            });

            stage.requestFocus();
            stage.toFront();
            if (wait) {
                stage.showAndWait();

            } else {
                stage.show();
            }
        } catch (final Exception exc) {
            System.out.println(exc.getMessage());
        }
    }
}

Share Improve this question asked Jan 19 at 13:56 ThomasThomas 1011 silver badge4 bronze badges 2
  • Hello Tomás, to make sure they look the same on all systems, you must set Insets and specify the font and its size. – Marce Puente Commented Jan 19 at 14:40
  • 1 Usually, there is no reason to set the window size. Every control already has a preferred size, and every layout and Stage respects those preferred sizes. If you really need an exact size (and you probably don’t), you should pass the width and height to the Scene constructor, not the Stage constructor. – VGR Commented Jan 19 at 19:44
Add a comment  | 

1 Answer 1

Reset to default 0

without size it also works only in KDE and older GNOME (22.04) and not in actual GNOME (24.04)

public class Muster extends Application {

    private static Stage primary = null;

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

    @Override
    public void init() throws Exception {
    }

    @Override
    public void start(Stage primaryStage) {
        primary = primaryStage;
        show(true, "Dialog 1", false);
    }

    private static void show(boolean first, String title, boolean wait) {
        VBox vBox = new VBox(10);
        vBox.setPadding(new Insets(25));
        vBox.setAlignment(Pos.CENTER);

        Button btnWait = new Button("Dialog wait");
        btnWait.setOnAction(a -> show(false, "Wieder einer", true));
        Button btn = new Button("Dialog");
        btn.setOnAction(a -> show(false, "Wieder einer", false));

        vBox.getChildren().add(new Label("Das ist ein Dialog"));
        vBox.getChildren().addAll(btnWait, btn);

        for (int i = 0; i < 10; ++i) {
            HBox hBox = new HBox(10);
            for (int ii = 0; ii < 10; ++ii) {
                Text text = new Text("Nummer: " + ii);
                Font font = Font.font("Verdana", FontWeight.BOLD, 16);
                text.setFont(font);
                hBox.getChildren().add(text);
            }
            vBox.getChildren().add(hBox);
        }

        try {
            Stage stage = first ? primary : new Stage();

            Scene scene = new Scene(vBox);
            stage.setScene(scene);
            stage.setTitle(title);
            if (!first) {
                stage.initOwner(primary);
            }

            stage.requestFocus();
            stage.toFront();
            if (wait) {
                stage.showAndWait();

            } else {
                stage.show();
            }
        } catch (final Exception exc) {
            System.out.println(exc.getMessage());
        }
    }
}

发布评论

评论列表(0)

  1. 暂无评论