【问题标题】:Empty pixels in rendered window using JavaFX使用 JavaFX 渲染窗口中的空像素
【发布时间】:2015-08-29 22:20:48
【问题描述】:

我正在使用 JavaFX 编写一个窗口。 如果我用 JavaFX Scene Builder 2.0 预览它,它似乎工作正常。

当我用我的应用程序加载它时,我使用以下代码:

Parent root = FXMLLoader.load(getClass().getResource("/Client/FXMLForms/MainForm.fxml"));
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.setTitle("Il Covo");
stage.setResizable(false);
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
     public void handle(WindowEvent we) {
         MainClient.close();
         System.exit(0);
     }
});
stage.show();

它出现了,但我遇到了这个问题:

JavaFX Bug Picture

实际上,它让我在窗口底部和右侧留出 10 像素。 现在,我通过在“stage.show()”之前添加这个找到了一个“修复”:

stage.setWidth(1280.0);
stage.setHeight(720.0);

但我想了解为什么它会给我这个问题。

窗口的FXML文件

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <MenuBar prefHeight="25.0" prefWidth="1280.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
      <TabPane layoutY="25.0" prefHeight="695.0" prefWidth="1280.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab closable="false" text="Homepage">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="260.0" prefWidth="1280.0">
                     <children>
                        <TreeView prefHeight="666.0" prefWidth="285.0" />
                        <Pane layoutX="285.0" prefHeight="667.0" prefWidth="996.0" />
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
          <Tab closable="false" text="Orders">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
            <Tab closable="false" text="Reviews">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
              </content>
            </Tab>
            <Tab closable="false" text="Cart">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
              </content>
            </Tab>
        </tabs>
      </TabPane>
   </children>
</Pane>

【问题讨论】:

    标签: java javafx java-8 fxml


    【解决方案1】:

    我之前也遇到过这个问题。我认为这是JavaFX的一个错误。代码行的顺序对于解决这个问题变得非常重要。以下是我在一个项目中使用的代码,没有遇到这个问题:

    panel = new MyPanel(); //a BorderPane, its size is set using "setPrefSize" method
    
    root = new Group();
    root.getChildren().add(panel);
    
    scene = new Scene(root);
    
    stage.setResizable(false);
    stage.setScene(scene);
    stage.show();
    stage.sizeToScene();
    stage.centerOnScreen();
    

    【讨论】:

    • “stage.sizeToScene();”这一行做了完美的工作。很高兴找到 JavaFX 给出这个错误的原因。这可能更动态,我的解决方案是强制窗口的宽度 - 高度,不是很好。非常感谢您的解决方案,我不会给您确认答案,看看其他人是否知道更多。
    • 好的,但是几个月前我在互联网上经过长时间的搜索后找到了该解决方案。所以,我不认为还有更多。
    猜你喜欢
    • 2018-05-28
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多