选择打开JavaFX窗口的监视器 [英] Choose which monitor does a JavaFX window open in

查看:203
本文介绍了选择打开JavaFX窗口的监视器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两台显示器。我在第二台显示器上打开了Eclipse,但是当我运行JavaFX代码时,JavaFX窗口总是在第一台显示器中打开,每次我必须将它拖到第二台显示器上才能使用它。

I have two monitors. I have Eclipse open on the second monitor but when I run my JavaFX code, the JavaFX window always opens up in the first monitor and every time I have to drag it to the second monitor to use it.

我必须这样做,因为当它在第一台显示器上打开时,场景中没有任何组件被加载。仅当我将其拖动到第二个监视器时才会加载它。但是,当我断开第二台显示器时,它会正确加载。

I have to do this because when it opens on the first monitor, none of the components inside the scene are loaded. It gets loaded only if I drag it to the second monitor. But when I disconnect the second monitor, it loads properly.

有人可以帮帮我吗?默认情况下,如何让窗口在第二台显示器上打开?

Can someone please help me out? How do I, by default, make the window to open on the second monitor?

注意:我的第一台显示器是Macbook Pro,第二台是用作iMac的iMac外部监视器。

NB: My first monitor is a Macbook Pro and the second is an iMac used as an external monitor.

回应所做的评论:

组件在screen1上未正确加载的问题发生在任何地方简单的javaFX代码。例如,为方便起见,我将@Sergey提供的代码作为答案。

The problem of components not loading properly on screen1 happens with any simple javaFX code. For example and for convenience, I am taking the code that @Sergey has given as the answer.

代码:

public class FXScreens extends Application {

    @Override
    public void start(Stage stage) {
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 200, 250);

        int index = 1;
        for (Screen screen : Screen.getScreens()) {
            Rectangle2D bounds = screen.getVisualBounds();

            Button btn = new Button("Move me to Screen " + index++);
            btn.setOnAction((e) -> {
                stage.setX(bounds.getMinX() + 100);
                stage.setY(bounds.getMinY() + 100);
            });
            root.getChildren().add(btn);
        }

        stage.setTitle("Screen Jumper");
        stage.setScene(scene);
        stage.show();
    }

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

当我使用eclipse或者用eclipse从screen2启动它时终端,这是屏幕1上显示的内容:

调整窗口大小不会向我显示组件,只是缩放它们。我也无法点击按钮。

When I launch this from screen2 either with eclipse or with a terminal, this is what appears on screen 1: Resizing the window does not show me the components but just zooms them. I am not able to click on the buttons as well.

当我将其拖到屏幕2时,它变成这样:

When I drag this to screen 2, it becomes like this:

当另外两台显示器仍然连接时,如果我将eclipse拖到screen1并从那里运行代码,它会正确加载:

With two other monitors still connected, if I drag eclipse to screen1 and run the code from there, it loads properly:

我还应该补充一点,一旦我将它拖到另一个屏幕并加载了组件,它们就可以在任何屏幕上正常工作。

I should also add that once I drag it to the other screen and components are loaded, they work well on any screen.

推荐答案

您可以迭代 Screen.getScreens()并将阶段移动到所需的阶段。请参阅下面的示例。

You can iterate Screen.getScreens() and move your stage to required one. See example below.

public class FXScreens extends Application {

    @Override
    public void start(Stage stage) {
        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 200, 250);

        int index = 1;
        for (Screen screen : Screen.getScreens()) {
            Rectangle2D bounds = screen.getVisualBounds();

            Button btn = new Button("Move me to Screen " + index++);
            btn.setOnAction((e) -> {
                stage.setX(bounds.getMinX() + 100);
                stage.setY(bounds.getMinY() + 100);
            });
            root.getChildren().add(btn);
        }

        stage.setTitle("Screen Jumper");
        stage.setScene(scene);
        stage.show();
    }

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

不幸的是我无法帮助处理未加载的组件主屏幕。请提供更多详细信息以解决这些问题(哪些组件,它们是如何创建的,代码示例等)

Unfortunately I can't help with components not loading on primary screen. Please provide more details to address that (which components, how are they created, code sample, etc)

这篇关于选择打开JavaFX窗口的监视器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆