简体   繁体   English

是否可以在JavaFX中创建一个控制器数组?

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

I've had a good search and either I'm not using the right terminology or this question hasn't appeared yet. 我进行了很好的搜索,或者我使用的术语不正确,或者这个问题尚未出现。 I've been coding in Java for a few years and I've just started playing with Java FX over the last few weeks. 我从事Java编程已经有几年了,最近几周我才开始使用Java FX。

Context: 内容:

I'm trying to create an app that is effectively a quiz, but instead of having a new Pane/Tab for each question, I'd like to have them appear side by side. 我正在尝试创建一个实际上是一个测验的应用程序,但我不想让每个问题并排出现,而没有为每个问题创建一个新的窗格/选项卡。

I have created a questionController for a single question, but I'm unsure of the best way of creating many questionControllers that are all uniquely identifiable by the fx:id's. 我已经为单个问题创建了一个questionController,但是我不确定创建许多由fx:id唯一标识的questionController的最佳方法。

I'd like my GUI to be scaleable so the user can select the number of questions displayed per round. 我希望我的GUI可扩展,以便用户可以选择每轮显示的问题数。 I can't seem to work out how to do this without manually setting creating views with different numbers of questionControllers. 如果没有手动设置使用不同数量的QuestionControllers创建视图,我似乎无法解决该问题。

Things I've tried 我尝试过的事情

  • Adding controller gridPane, 添加控制器gridPane,
  • Importing the controller in the FXML 在FXML中导入控制器

But this aren't scaleable as my buttons just become button0 - buttonN 但这无法缩放,因为我的按钮只是变成button0-buttonN

If you require any more information, please let me know. 如果您需要更多信息,请告诉我。

Question Controller.java 问题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 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>

I think you are looking for something like this: 我认为您正在寻找这样的东西:

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();
}

Obviously if the TilePane (or whatever) is defined in another FXML file and injected into another controller, you can put the same code in an event handler or initialize method, as needed, in that controller. 显然,如果TilePane (或任何其他方式)在另一个FXML文件中定义并注入到另一个控制器中,则可以根据需要在该控制器中将相同的代码放入事件处理程序或初始化方法中。

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

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