簡體   English   中英

是否可以在JavaFX中創建一個控制器數組?

[英]Is it possible to create an array of Controllers in JavaFX?

我進行了很好的搜索,或者我使用的術語不正確,或者這個問題尚未出現。 我從事Java編程已經有幾年了,最近幾周我才開始使用Java FX。

內容:

我正在嘗試創建一個實際上是一個測驗的應用程序,但我不想讓每個問題並排出現,而沒有為每個問題創建一個新的窗格/選項卡。

我已經為單個問題創建了一個questionController,但是我不確定創建許多由fx:id唯一標識的questionController的最佳方法。

我希望我的GUI可擴展,以便用戶可以選擇每輪顯示的問題數。 如果沒有手動設置使用不同數量的QuestionControllers創建視圖,我似乎無法解決該問題。

我嘗試過的事情

  • 添加控制器gridPane,
  • 在FXML中導入控制器

但這無法縮放,因為我的按鈕只是變成button0-buttonN

如果您需要更多信息,請告訴我。

問題Controller.java

public class QuestionController implements Initializable {

    @FXML
    private ComboBox<String> chordCombo;

    @FXML
    private Label questionField;

    @FXML
    private Button submitButton;

    @FXML
    private ToggleGroup answerGroup;

    @FXML
    private RadioButton toggle0, toggle1, toggle2, toggle3, toggle4;

    @Override
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {

        submitButton.setOnAction((event) -> {
            System.out.println("Submit Logic");
        });

        answerGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            public void changed(ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) {
                if (answerGroup.getSelectedToggle() != null) {
                    System.out.println(answerGroup.getSelectedToggle().toString());
                }
            }
        });

    }

QuestionController.fxml

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="235.0" prefWidth="203.0" styleClass="layout" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="uk.co.temp.QuestionController">
   <children>
      <BorderPane layoutX="-11.0" layoutY="35.0" prefHeight="235.0" prefWidth="203.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <bottom>
            <Button fx:id="submitButton" mnemonicParsing="false" text="Submit" BorderPane.alignment="CENTER">
               <padding>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </padding>
               <BorderPane.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </BorderPane.margin>
            </Button>
         </bottom>
         <top>
            <Label fx:id="questionField" text="Insert Question Here" BorderPane.alignment="CENTER">
               <padding>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </padding>
               <BorderPane.margin>
                  <Insets left="10.0" right="10.0" top="10.0" />
               </BorderPane.margin>
            </Label>
         </top>
         <center>
            <GridPane BorderPane.alignment="CENTER">
              <columnConstraints>
                <ColumnConstraints halignment="CENTER" 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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <RadioButton fx:id="toggle0" mnemonicParsing="false" text="Answer A">
                     <toggleGroup>
                        <ToggleGroup fx:id="answerGroup" />
                     </toggleGroup></RadioButton>
                  <RadioButton fx:id="toggle1" mnemonicParsing="false" text="Answer B" toggleGroup="$answerGroup" GridPane.rowIndex="1" />
                  <RadioButton fx:id="toggle2" mnemonicParsing="false" text="Answer C" toggleGroup="$answerGroup" GridPane.rowIndex="2" />
                  <RadioButton fx:id="toggle3" mnemonicParsing="false" text="Answer D" toggleGroup="$answerGroup" GridPane.rowIndex="3" />
                  <RadioButton fx:id="toggle4" mnemonicParsing="false" text="Answer E" toggleGroup="$answerGroup" GridPane.rowIndex="4" />
               </children>
               <padding>
                  <Insets top="10.0" />
               </padding>
            </GridPane>
         </center>
      </BorderPane>
   </children>
</AnchorPane>

我認為您正在尋找這樣的東西:

TilePane pane = new TilePane(); // or whatever you are putting the questions in...

int numQuestions = ... ;

QuestionController[] controllers = new QuestionController[numQuestions];

for (int questionNumber = 0 ; questionNumber < numQuestions ; questionNumber++) {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/QuestionController.fxml"));
    pane.getChildren().add(loader.load());
    controllers[questionNumber] = loader.getController();
}

顯然,如果TilePane (或任何其他方式)在另一個FXML文件中定義並注入到另一個控制器中,則可以根據需要在該控制器中將相同的代碼放入事件處理程序或初始化方法中。

暫無
暫無

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

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