使用JavaFX在渲染窗口中清空像素
我正在使用JavaFX编写一个窗口。 如果我使用JavaFX Scene Builder 2.0预览它,它似乎可以正常工作 当我将其与应用程序一起加载时,我使用以下代码:使用JavaFX在渲染窗口中清空像素,java,javafx,java-8,fxml,Java,Javafx,Java 8,Fxml,我正在使用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
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();
但我想知道为什么它会给我这个问题
窗口的FXML文件
我以前也遇到过这个问题。我认为这是JavaFX的一个bug。代码行的顺序对于解决这个问题变得非常重要。以下是我在一个项目中使用的代码,但没有遇到该问题:
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给出这个bug的原因就好了。这可能更具动态性,我的解决方案是强制窗口的宽度-高度,但不是很好。非常感谢您的解决方案,我不会给您确认答案,以查看其他人是否了解更多。好的,但几个月前我在互联网上进行了长时间搜索后找到了该解决方案。所以,我认为没有比这更多的了。
<?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>
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();