第二个屏幕上的javafx全屏

对于我的生活,我似乎无法得到这方面的帮助。我有一个 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,所以它应该是第二个显示器。程序循环遍历所有屏幕并尝试移动程序,然后将全屏显示。我知道坐标是一样的,但没有骰子,它仍然在主屏幕上全屏显示。请帮忙。


慕村9548890
浏览 170回答 1
1回答

回首忆惘然

我不知道我是否真正理解您的问题,但是如果您有两个屏幕,为什么要循环显示屏幕?为什么不使用与屏幕相关的信息在位置二/索引之一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;/**&nbsp;*&nbsp;* @author blj0011&nbsp;*/public class JavaFXApplication257 extends Application{&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage primaryStage)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ObservableList<Screen> screens = Screen.getScreens();//Get list of Screens&nbsp; &nbsp; &nbsp; &nbsp; Button btn = new Button();&nbsp; &nbsp; &nbsp; &nbsp; btn.setText("Full Screen - Screen 1");&nbsp; &nbsp; &nbsp; &nbsp; btn.setOnAction((ActionEvent event) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle2D bounds = screens.get(0).getVisualBounds();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setX(bounds.getMinX());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setY(bounds.getMinY());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setFullScreen(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //primaryStage.setWidth(bounds.getWidth());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //primaryStage.setHeight(bounds.getHeight());&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; Button btn2 = new Button();&nbsp; &nbsp; &nbsp; &nbsp; btn2.setText("Full Screen - Screen 2");&nbsp; &nbsp; &nbsp; &nbsp; btn2.setOnAction((ActionEvent event) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (screens.size() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle2D bounds = screens.get(1).getVisualBounds();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setX(bounds.getMinX());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setY(bounds.getMinY());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setFullScreen(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //primaryStage.setWidth(bounds.getWidth());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //primaryStage.setHeight(bounds.getHeight());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; StackPane root = new StackPane();&nbsp; &nbsp; &nbsp; &nbsp; root.getChildren().add(new VBox(btn, btn2));&nbsp; &nbsp; &nbsp; &nbsp; Scene scene = new Scene(root, 300, 250);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setTitle("Hello World!");&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setScene(scene);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.show();&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param args the command line arguments&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; launch(args);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java