JavaFX如何在第二个屏幕上全屏显示

7

无论如何,我似乎都得不到关于这个问题的帮助。我有一个JavaFX屏幕,我正在尝试在第二个监视器上全屏显示。我根据其他建议尝试了以下方法,但没有成功。我知道坐标是正确的,但它一直在我的主监视器上全屏显示。请帮忙。

if (mainSet.getBoolean("fullScr", false)) {
  int count = mainSet.getInt("MonSel", 0);
  if (count > 0) {
    int i = 0;
    for (Screen screen: Screen.getScreens()) {
      if (count == i) {
        Rectangle2D bounds = screen.getBounds();
        primaryStage.setX(bounds.getMinX());
        System.out.println(bounds.getMinX());
        System.out.println(bounds.getMinY());
        primaryStage.setY(bounds.getMinY());
      }
      i++;
    }
  }
  primaryStage.setFullScreen(true);
}

第一个 if 检查偏好设置以查看是否设置了全屏模式。第二个 if 查看是否选择了除第一个显示器外的其他显示器。它是 1,所以应该是第二个显示器。程序循环遍历所有屏幕并尝试移动程序,然后再全屏。我知道坐标是相同的,但不行,它仍然在主屏幕上全屏。请帮忙。


1
请提供一个 [mcve] 来演示问题。 - kleopatra
我通过在全屏代码声明后放置primarystage.show()来解决它,但是我喜欢其他人的答案更好,看起来更整洁 :) - darthgamer64
1个回答

5

我不确定我是否真正理解了你的问题,但如果你有两个屏幕,为什么要循环遍历这些屏幕呢?为什么不直接使用与第二个位置/索引处的屏幕相关联的信息,例如ObservableList?我会发布一个演示如何在第二个显示器上全屏显示的示例应用程序。

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication257 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        ObservableList<Screen> screens = Screen.getScreens();//Get list of Screens
        Button btn = new Button();
        btn.setText("Full Screen - Screen 1");
        btn.setOnAction((ActionEvent event) -> {
            Rectangle2D bounds = screens.get(0).getVisualBounds();
            primaryStage.setX(bounds.getMinX());
            primaryStage.setY(bounds.getMinY());
            primaryStage.setFullScreen(true);
            //primaryStage.setWidth(bounds.getWidth());
            //primaryStage.setHeight(bounds.getHeight());
        });

        Button btn2 = new Button();
        btn2.setText("Full Screen - Screen 2");
        btn2.setOnAction((ActionEvent event) -> {
            if (screens.size() > 0) {
                Rectangle2D bounds = screens.get(1).getVisualBounds();
                primaryStage.setX(bounds.getMinX());
                primaryStage.setY(bounds.getMinY());
                primaryStage.setFullScreen(true);
                //primaryStage.setWidth(bounds.getWidth());
                //primaryStage.setHeight(bounds.getHeight());
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(new VBox(btn, btn2));

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

1
我将此标记为已回答,因为它实际上极大地简化了我的代码。我不知道我可以这样做。谢谢你,我的朋友 :) - darthgamer64
1
有趣的是,当我在primarystage.show()之前编写了所有代码时,它能够正常工作。但如果我使用按钮执行全屏命令,它也能正常工作。这很奇怪。 - darthgamer64

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接