簡體   English   中英

SceneBuilder看起來與JavaFX程序不同嗎?

[英]SceneBuilder looks different to JavaFX program?

當我在SceneBuilder上進行編輯時,程序如下所示 在此處輸入圖片說明

當我運行程序時,它看起來像這樣: 在此處輸入圖片說明

這是我第一次嘗試使用FXML,但我不知道出了什么問題。 我嘗試遵循問題,但未找到解決方案。

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

對不起,海量的代碼。 只是不確定問題出在哪里。

您似乎只是把一些布局放在一起,使UI在SceneBuilder中看起來正確。

這種方法是不好的。 這樣,您幾乎可以確定調整大小后布局將被弄亂。 如果調整Stage的大小,這將根據Stage大小將Scene的內容強制為適當的大小。

如果將根節點包裝在AnchorPane ,將原始根的所有錨點都設置為0並調整AnchorPane大小,那么您也可以在SceneBuilder中觀察到該行為。

您應該了解布局的用途, 然后在SceneBuilder中設計UI。 通常,最好保持場景簡單,而不是嵌套不必要的更多布局。
在您的情況下,3 Pane似乎就足夠了:

  1. GridPane為根
  2. 包含ButtonVBox
  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>

在您的情況下,“場景大小”小於GridPane的大小

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

並且在網格窗格中的大小為

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

因此,布局有所不同:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM