我正在使用 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();
看起来,但我遇到了这个问题:
实际上,它在窗口底部和右侧留下了 10px 的空间。 现在,我找到了一个“修复”,通过在“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>
最佳答案
我以前也遇到过这个问题。我认为这是 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();
关于java - 使用 JavaFX 渲染窗口中的空像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32291232/