2017-09-27 61 views
-1

我正在研究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> 
+2

這看起來像一個佈局問題。您需要發佈佈局代碼(fxml)。您使用的是什麼佈局窗格? –

+0

我使用了AnchorPane。 –

+0

With a AnchorPane不是UI元素的位置基本上是硬編碼的嗎? –

回答

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

+0

你是否與場景製作者合作,因爲我沒有找到一些屬性? –

+0

是的,我做到了。你沒有找到什麼屬性? – Sedrick

+0

這些是AnchorPane.bottomAnchor =「0.0」AnchorPane.leftAnchor =「0.0」AnchorPane.rightAnchor =「0.0」AnchorPane.topAnchor =「0.0」, –