简体   繁体   English

SceneBuilder看起来与JavaFX程序不同吗?

[英]SceneBuilder looks different to JavaFX program?

When I edit on SceneBuilder, the program looks like this 当我在SceneBuilder上进行编辑时,程序如下所示 在此处输入图片说明

When I run the program, it looks like this: 当我运行程序时,它看起来像这样: 在此处输入图片说明

This is my first time trying to use FXML and I have no idea what is going wrong. 这是我第一次尝试使用FXML,但我不知道出了什么问题。 I tried to follow this question but don't see a solution.. 我尝试遵循问题,但未找到解决方案。

Here is my FXML code: 这是我的FXML代码:

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

<GridPane alignment="center" hgap="10" prefHeight="633.0" prefWidth="869.0" stylesheets="/sample/sample.css" vgap="10" xmlns="http://javafx.com/javafx/8.0.76-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <BorderPane prefHeight="662.0" prefWidth="869.0" stylesheets="@sample.css">
         <top>
            <ImageView fitHeight="173.0" fitWidth="409.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER">
               <image>
                  <Image url="@../../../../../Pictures/title.png" />
               </image>
            </ImageView>
         </top>
         <right>
            <VBox prefHeight="305.0" prefWidth="105.0" BorderPane.alignment="CENTER">
               <children>
                  <Button mnemonicParsing="false" text="Make a Graph" />
                  <Button mnemonicParsing="false" text="Save" />
                  <Button mnemonicParsing="false" text="Delete" />
               </children></VBox>
         </right>
         <center>
            <VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
               <children>
                  <Pane prefHeight="41.0" prefWidth="764.0">
                     <children>
                        <BorderPane prefHeight="0.0" prefWidth="764.0">
                           <left>
                              <ComboBox prefWidth="150.0" promptText="Sort or Filter" BorderPane.alignment="CENTER" />
                           </left>
                           <right>
                              <TextField text="Search" BorderPane.alignment="CENTER" />
                           </right>
                           <center>
                              <StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
                                 <children>
                                    <HBox disable="true" prefHeight="100.0" prefWidth="200.0">
                                       <children>
                                          <ComboBox prefWidth="150.0" />
                                          <CheckBox mnemonicParsing="false" prefHeight="33.0" prefWidth="120.0" text="Favourties" />
                                       </children>
                                    </HBox>
                                 </children>
                              </StackPane>
                           </center>
                        </BorderPane>
                     </children></Pane>
                  <ScrollPane prefHeight="507.0" prefWidth="764.0">
                     <content>
                        <GridPane prefHeight="90.0" prefWidth="762.0">
                          <columnConstraints>
                            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                              <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                              <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                          </columnConstraints>
                          <rowConstraints>
                            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                          </rowConstraints>
                           <children>
                              <Label prefHeight="21.0" prefWidth="61.0" text="Graph" />
                              <Label text="Description" GridPane.columnIndex="1" />
                              <Label text="Options" GridPane.columnIndex="2" />
                              <Label text="Favourites" GridPane.columnIndex="3" />
                           </children>
                        </GridPane>
                     </content>
                  </ScrollPane>
               </children>
            </VBox>
         </center></BorderPane>
   </children>
</GridPane>

Here is my Java code: 这是我的Java代码:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Sorry for the huge chunks of code. 对不起,海量的代码。 Just not sure where the problem is coming from. 只是不确定问题出在哪里。

You just seem to throw together some layouts that make the UI look the right way in SceneBuilder. 您似乎只是把一些布局放在一起,使UI在SceneBuilder中看起来正确。

This approach is bad. 这种方法是不好的。 This way you can be almost sure the layout will be messed up when it's resized. 这样,您几乎可以确定调整大小后布局将被弄乱。 If a Stage is resized this forces the content of the Scene to a appropriate size depending on the Stage size. 如果调整Stage的大小,这将根据Stage大小将Scene的内容强制为适当的大小。

You can observe the behaviour in SceneBuilder too, if you wrap the root node in a AnchorPane , set all anchors for the original root to 0 and resize the AnchorPane . 如果将根节点包装在AnchorPane ,将原始根的所有锚点都设置为0并调整AnchorPane大小,那么您也可以在SceneBuilder中观察到该行为。

You should learn, what the layouts do and then design the UI in SceneBuilder. 您应该了解布局的用途, 然后在SceneBuilder中设计UI。 In general it's best to keep the scene simple instead of nesting more layouts than necessary. 通常,最好保持场景简单,而不是嵌套不必要的更多布局。
In your case 3 Pane s seem to suffice: 在您的情况下,3 Pane似乎就足够了:

  1. A GridPane as root GridPane为根
  2. A VBox containing the Button s 包含ButtonVBox
  3. The GridPane inside the ScrollPane ScrollPane内部的GridPane

By using the GridPane 's layout parameters you can design a UI that has a much nicer resizing behavior: 通过使用GridPane的布局参数,您可以设计一个具有更好的调整大小行为的UI:

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.image.*?>

<GridPane alignment="center" hgap="10" prefHeight="633.0" prefWidth="869.0" stylesheets="/sample/sample.css" vgap="10" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <ImageView fitHeight="173.0" fitWidth="409.0" pickOnBounds="true" preserveRatio="true" GridPane.halignment="CENTER" GridPane.columnSpan="5" >
            <image>
                <Image url="@../../../../../Pictures/title.png" />
            </image>
        </ImageView>
        <VBox prefWidth="105.0" GridPane.columnIndex="4" GridPane.rowIndex="1" GridPane.rowSpan="2" >
            <children>
                <Button mnemonicParsing="false" text="Make a Graph" />
                <Button mnemonicParsing="false" text="Save" />
                <Button mnemonicParsing="false" text="Delete" />
            </children>
        </VBox>
        <ComboBox prefWidth="150.0" promptText="Sort or Filter" GridPane.rowIndex="1" />
        <ComboBox prefWidth="150.0" GridPane.rowIndex="1" GridPane.columnIndex="1" disable="true"/>
        <CheckBox mnemonicParsing="false" prefHeight="33.0" prefWidth="120.0" text="Favourties" GridPane.rowIndex="1" GridPane.columnIndex="2" disable="true"/>
        <TextField text="Search" GridPane.rowIndex="1" GridPane.columnIndex="3"/>
        <ScrollPane prefHeight="507.0" prefWidth="764.0" GridPane.rowIndex="2" GridPane.columnSpan="4">
            <content>
                <GridPane prefHeight="90.0" prefWidth="762.0">
                    <columnConstraints>
                        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                    <children>
                        <Label prefHeight="21.0" prefWidth="61.0" text="Graph" />
                        <Label text="Description" GridPane.columnIndex="1" />
                        <Label text="Options" GridPane.columnIndex="2" />
                        <Label text="Favourites" GridPane.columnIndex="3" />
                    </children>
                </GridPane>
            </content>
        </ScrollPane>
    </children>
</GridPane>

In your case, Size of Scene is less than size of GridPane 在您的情况下,“场景大小”小于GridPane的大小

 primaryStage.setScene(new Scene(root, 600, 400));

and in grid pane size is 并且在网格窗格中的大小为

 <GridPane alignment="center" hgap="10" prefHeight="633.0" prefWidth="869.0"

because of this there is difference in layout :) 因此,布局有所不同:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM