gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:28 25 4
gpt4 key购买 nike

我正在尝试创建一个多项选择电影游戏,其中电影出现在标签中, Actor 与该电影匹配,并且 3 个随机数设置为单选按钮。我在网上找到的每个示例或教程都向我展示了如何使用 JavaFx 和 Scene Builder,而无需实现任何已编写的类或对象。我有一个名为 MovieSet 的类,它接受电影的数组列表,我对 Actor 也做了同样的事情。然而,所有在线教程都没有说是否可以为这些数组列表设置标签和单选按钮。

可以这样做吗?如果是的话,这将如何完成?

我使用了 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());
}
}

这是我的 Controller 。我很抱歉。这是我第一次使用 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

关于java - 是否可以使用 ArrayList 中的对象在 JavaFx 和 Scene Builder 中设置标签和单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49635282/

25 4 0