使用 JavaFX 渲染 window 中的空像素

Empty pixels in rendered window using JavaFX

我正在使用 JavaFX 编写 window。 如果我用 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

实际上,它在 window 的底部和右侧留下了 10px 的空间。 现在,我通过在 "stage.show()":

之前添加这个找到了 "fix"
stage.setWidth(1280.0);
stage.setHeight(720.0);

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

window

的 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>

我之前也遇到过这个问题。我认为这是 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();