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

时间:2017-09-27 17:21:43

标签: javafx

我正在研究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();

                    }

enter image description here

这是我的父布局

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

1 个答案:

答案 0 :(得分:1)

这是您可以遵循的模式

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

enter image description here enter image description here