【问题标题】:SceneBuilder looks different to JavaFX program?SceneBuilder 看起来与 JavaFX 程序不同?
【发布时间】:2016-10-04 19:51:51
【问题描述】:

当我在 SceneBuilder 上编辑时,程序看起来像这样

当我运行程序时,它看起来像这样:

这是我第一次尝试使用 FXML,我不知道出了什么问题。我尝试关注this 问题,但没有看到解决方案..

这是我的 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>

这是我的 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);
    }
}

抱歉,有大量代码。只是不确定问题出在哪里。

【问题讨论】:

    标签: java user-interface javafx fxml


    【解决方案1】:

    您似乎只是将一些布局放在一起,使 UI 在 SceneBuilder 中看起来正确。

    这种方法不好。这样,您几乎可以确定布局在调整大小时会被弄乱。如果调整 Stage 的大小,则会根据 Stage 的大小强制将 Scene 的内容调整为适当的大小。

    您也可以在 SceneBuilder 中观察行为,如果您将根节点包装在 AnchorPane 中,请将原始根的所有锚点设置为 0 并调整 AnchorPane 的大小。

    您应该了解布局的作用并然后在 SceneBuilder 中设计 UI。一般来说,最好保持场景简单,而不是嵌套不必要的布局。
    在您的情况下,3 Panes 似乎就足够了:

    1. GridPane 作为根
    2. 包含Buttons 的VBox
    3. ScrollPane 内的GridPane

    通过使用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>
    

    【讨论】:

    • 哇你绝对的英雄!我不知道你可以这样使用 GridPanes。太感谢了。 200000 票 :)
    【解决方案2】:

    在您的情况下,场景的大小小于 GridPane 的大小

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

    在网格窗格中,大小为

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

    因此布局有所不同:)

    【讨论】:

      猜你喜欢
      • 2020-04-22
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 2014-05-19
      • 2012-05-15
      • 2014-11-22
      • 2018-08-27
      • 1970-01-01
      相关资源
      最近更新 更多