JavaFx:我如何用屏幕分辨率绑定舞台?

JavaFx:我如何用屏幕分辨率绑定舞台?,javafx,Javafx,我正在从事javaFx项目,我在笔记本电脑上创建的,一切都很好,但是当我更改屏幕时,现在我使用大屏幕,我发现我的应用程序受屏幕分辨率的影响,一些组件不适合屏幕。 这是我的代码: try { FXMLLoader fxm = new FXMLLoader(getClass().getResource("Home.fxml")); //Load th file XML To parent

我正在从事javaFx项目,我在笔记本电脑上创建的,一切都很好,但是当我更改屏幕时,现在我使用大屏幕,我发现我的应用程序受屏幕分辨率的影响,一些组件不适合屏幕。 这是我的代码:

 try {

                        FXMLLoader fxm = new FXMLLoader(getClass().getResource("Home.fxml"));

                        //Load th file XML To parent
                        Parent parent = (Parent) fxm.load();
                        //Create a new stage         

                        s.setTitle("PubWorks 1.0");
                        s.getIcons().add(new Image("/Images/Casque.png"));

                        Scene scene = new Scene(parent);
                        s.setScene(scene);
                        s.centerOnScreen();
                        ((Node) event.getSource()).getScene().getWindow().hide();
                        s.show();
                        s.setOnCloseRequest(new EventHandler<WindowEvent>() {
                            @Override
                            public void handle(WindowEvent event) {
                                new RecentMessageDao().Delete(new RecentMessage(0));
                                User user = new User(UserTxt.getText(), PasswordTxt.getText());
                                new UserDao().setOff(user);
                                System.exit(0);

                            }
                        });
                        MenuController controller = fxm.getController();
                        controller.getMenuController(s);

                    } catch (Exception exception) {
                        exception.printStackTrace();

                    }
试试看{
FXMLLoader fxm=新的FXMLLoader(getClass().getResource(“Home.fxml”);
//将文件XML加载到父级
Parent Parent=(Parent)fxm.load();
//开创新局面
s、 setTitle(“PubWorks 1.0”);
s、 getIcons().add(新图像(“/Images/Casque.png”);
场景=新场景(父场景);
s、 场景(场景);
s、 屏幕中心();
((节点)event.getSource()).getScene().getWindow().hide();
s、 show();
s、 setOnCloseRequest(新的EventHandler(){
@凌驾
公共无效句柄(WindowEvent事件){
new RecentMessageDao().Delete(new RecentMessage(0));
User User=新用户(UserTxt.getText(),PasswordTxt.getText());
新UserDao().setOff(用户);
系统出口(0);
}
});
MenuController controller=fxm.getController();
控制器。getMenuController;
}捕获(异常){
异常。printStackTrace();
}

这是我的父布局

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="710.0" prefWidth="1360.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="hmproject.MenuController"> ........</AnchorPane>
。。。。。。。。

这是您可以遵循的模式

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

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <VBox layoutX="168.0" layoutY="30.0" prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <AnchorPane prefHeight="52.0" prefWidth="600.0" style="-fx-background-color: pink;" />
            <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
               <children>
                  <AnchorPane prefHeight="309.0" prefWidth="113.0" style="-fx-background-color: lightgreen;" />
                  <AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: lightblue;" HBox.hgrow="ALWAYS" />
               </children>
            </HBox>
            <AnchorPane prefHeight="39.0" prefWidth="600.0" style="-fx-background-color: yellow;" />
         </children>
      </VBox>
   </children>
</AnchorPane>


这看起来像是布局问题。您需要发布布局代码(fxml)。您使用的是什么布局窗格?我使用了AnchorPane。使用
AnchorPane
时,UI元素的位置基本上不是硬编码的吗?位置被锚定到点上,因此如果锚定正确,它们应该相对于窗格展开和收缩,但保持在锚定点上。跳过这一点的最佳方法是什么,我的意思是跳过屏幕分辨率的改变?你是用scene builder做的,因为我没有找到一些属性吗?是的。您没有找到什么属性?这些属性是AnchorPane.bottomAnchor=“0.0”AnchorPane.leftAnchor=“0.0”AnchorPane.rightAnchor=“0.0”AnchorPane.topAnchor=“0.0”,如果您在代码中设置此属性,请执行以下操作:
AnchorPane.setBottomAnchor(您的节点,0.0)