是否可以使用ArrayList中的对象在JavaFx和Scene Builder中设置标签和单选按钮?

我正在尝试创建一个多选电影游戏,其中电影出现在标签中,并且与该电影匹配的演员和3个随机数被设置为单选按钮.我在网上找到的每个示例或教程都向我展示了如何使用JavaFx和Scene Builder,而不实现任何已编写的类或对象.我有一个名为MovieSet的类,它接收电影的Arraylist,并且对演员也是如此.但是,所有在线教程都从未说过是否可以将标签和单选按钮设置为这些arraylist.

是否有可能做到这一点?如果是这样,该怎么做?

我已经使用了for循环来遍历movieList,但这是告诉我找不到movieList的符号

public static void displayMovies(ArrayList<Movie> movieList) {
    for (int x = 0; x < movieList.size(); x++) {
        Movie movie = movieList.get(x);
        System.out.printf("%s", movie.toString());
    }
}

这是我的控制器.我深感抱歉.这是我第一次使用JavaFX和

public class FXMLDocumentController implements Initializable {

    @FXML
    private Label movielabel;

    @FXML
    private void handleButtonAction(ActionEvent event) {

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
       MovieSet movie = new MovieSet(); 
       this.movielabel.getLabelFor(MovieSet.displayMovies(movieList));
    }    

}
最佳答案
此示例应用程序是处理选择题和答案应用程序的一般方法.代码注释.

Main Class

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class MultipleChoiceGameExample extends Application
{

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

Controller

import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{

    @FXML
    private Label lblQuestionNumber;
    @FXML
    private TextArea taQuestionDisplay;
    @FXML
    private Button btnAnswer1, btnAnswer2, btnAnswer3, btnAnswer4, btnNextQuestion;

    List<Question> questions;
    int currentQuestion = 0;

    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        //Get Questions from Database or what ever source you use
        FakeDatabaseHandler fakeDatabaseHandler = new FakeDatabaseHandler();
        questions = fakeDatabaseHandler.getQuestions();
        //Shuffle the questions
        Collections.shuffle(questions);
        //Load first question
        lblQuestionNumber.setText("Question 1 of " + questions.size());
        loadQuestion(questions.get(currentQuestion));
    }

    @FXML
    public void handleBtnNextQuestion(ActionEvent actionEvent)
    {
        currentQuestion++;
        if (currentQuestion < questions.size()) {
            lblQuestionNumber.setText("Question " + (currentQuestion + 1) + " of " + questions.size());
            loadQuestion(questions.get(currentQuestion));
        }
        else {
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setTitle("Game Over Alert");
            alert.setContentText("There are no more questions!");
            alert.showAndWait();
        }
    }

    public void loadQuestion(Question question)
    {
        taQuestionDisplay.setText(question.getQuestion());//Set the question
        List<String> choices = question.getIncorrectAnswers();//Get the incorrect answers
        choices.add(question.getCorrectAnswer());////Get the correct answer and add it.
        Collections.shuffle(choices);//Randomize the choices

        //Add buttons to List to make creating event handlers easier
        List<Button> choicesButtons = new ArrayList();
        choicesButtons.add(btnAnswer1);
        choicesButtons.add(btnAnswer2);
        choicesButtons.add(btnAnswer3);
        choicesButtons.add(btnAnswer4);
        //Set the choices to a Button
        for (int i = 0; i < choices.size(); i++) {
            choicesButtons.get(i).setText(choices.get(i));
        }

        //Create Action handlers for each button
        for (Button button : choicesButtons) {
            button.setOnAction(actionEvent -> {
                //Check if button's text equals the correct answer
                if (button.getText().equals(question.getCorrectAnswer())) {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("Answer Alert");
                    alert.setHeaderText("Correct!");
                    alert.setContentText("You got the answer right!");
                    alert.showAndWait();
                }
                else {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("Answer Alert");
                    alert.setHeaderText("Incorrect!");
                    alert.setContentText("You got the answer wrong!\nThe correct answer is " + question.getCorrectAnswer());
                    alert.showAndWait();
                }
            });
        }

    }

}

FXML

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" prefHeight="476.0" prefWidth="668.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="multiplechoicegameexample.FXMLDocumentController">
    <children>
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <VBox alignment="TOP_CENTER" prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <Label fx:id="lblQuestionNumber" alignment="CENTER" maxWidth="1.7976931348623157E308" text="Label" />
            <TextArea fx:id="taQuestionDisplay" prefHeight="300.0" prefWidth="200.0">
               <VBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </VBox.margin>
            </TextArea>
            <GridPane hgap="5.0" maxWidth="400.0" vgap="5.0">
              <columnConstraints>
                <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>
               <children>
                  <Button fx:id="btnAnswer1" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button" />
                  <Button fx:id="btnAnswer2" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button" GridPane.columnIndex="1" />
                  <Button fx:id="btnAnswer3" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button" GridPane.rowIndex="1" />
                  <Button fx:id="btnAnswer4" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button" GridPane.columnIndex="1" GridPane.rowIndex="1" />
               </children>
            </GridPane>
              <Button fx:id="btnNextQuestion" onAction="#handleBtnNextQuestion" text="Next Question">
               <VBox.margin>
                  <Insets top="20.0" />
               </VBox.margin>
            </Button>
         </children>
      </VBox>
    </children>
</AnchorPane>

Question Class

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author blj0011
 */
public class Question
{

    private String question;
    private String correctAnswer;
    private List<String> incorrectAnswers = new ArrayList();

    public Question(String question, String correctAnswer, List<String> incorrectAnswers)
    {
        this.question = question;
        this.correctAnswer = correctAnswer;
        this.incorrectAnswers = incorrectAnswers;
    }

    public String getQuestion()
    {
        return question;
    }

    public void setQuestion(String question)
    {
        this.question = question;
    }

    public String getCorrectAnswer()
    {
        return correctAnswer;
    }

    public void setCorrectAnswer(String correctAnswer)
    {
        this.correctAnswer = correctAnswer;
    }

    public List<String> getIncorrectAnswers()
    {
        return incorrectAnswers;
    }

    public void setIncorrectAnswers(List<String> incorrectAnswers)
    {
        this.incorrectAnswers = incorrectAnswers;
    }
}

Database Handler(You may get your data from a different source)

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author blj0011
 */
public class FakeDatabaseHandler
{

    List<Question> questions = new ArrayList();

    public FakeDatabaseHandler()
    {
        //connect to db!
        //Simulate getting data from db!
        List<String> incorrectAnswersQuestion1 = new ArrayList();
        incorrectAnswersQuestion1.add("Pakistan");
        incorrectAnswersQuestion1.add("Palau");
        incorrectAnswersQuestion1.add("Panama");
        questions.add(new Question("Which is the only American state to begin with the letter 'p'?", "Pennsylvania", incorrectAnswersQuestion1));
        List<String> incorrectAnswersQuestion2 = new ArrayList();
        incorrectAnswersQuestion2.add("Mississppi");
        incorrectAnswersQuestion2.add("Nile");
        incorrectAnswersQuestion2.add("Yangtze");
        questions.add(new Question("What is the world's longest river?", "Amazon", incorrectAnswersQuestion2));
        List<String> incorrectAnswersQuestion3 = new ArrayList();
        incorrectAnswersQuestion3.add("6,000 miles");
        incorrectAnswersQuestion3.add("10,000 miles");
        incorrectAnswersQuestion3.add("12,000 miles");
        questions.add(new Question("What is the diameter of Earth?", "8,000 miles", incorrectAnswersQuestion3));
    }

    public List<Question> getQuestions()
    {
        return questions;
    }

}

enter image description here

转载注明原文:是否可以使用ArrayList中的对象在JavaFx和Scene Builder中设置标签和单选按钮? - 代码日志