gpt4 book ai didi

java - 无法更改标签和按钮值

转载 作者:行者123 更新时间:2023-12-02 10:58:19 24 4
gpt4 key购买 nike

我正在使用 JavaFx 编写一个简单的问答游戏应用程序。我想使用列表和表格中的数据在标签和按钮上设置文本。

我创建了三个类:Main、Controller 和 Quiz.fxml。我对通信类和方法有疑问。在“Main.java”中,我创建了对象“Controllercontroller = new Controller()”,并从“Controller.java”类中调用方法“controller.setText”。

现在,我有几个选择:

-如果构造函数“public Controller(){}”应用程序中的 delate list(ArrayList questions) 和 tab(String[][] Answers) 不起作用,我会在这一行“”中收到错误

-如果声明“Text()”方法中的所有内容(列表、选项卡和标签上的设置文本)应用程序运行,但值按钮和标签不会更改

-如果我在“setData()”中声明列表和选项卡,接下来我想更改“Text()”方法中的按钮和标签值,我看不到该列表,并且必须声明相同的列表““Text()”中的问题

public class Main extends Application {



@Override
public void start(Stage primaryStage) throws Exception {
try {

Parent root = FXMLLoader.load(getClass().getResource("/Controller/Quiz.fxml"));
Scene scene = new Scene(root,500,400)
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
Controller controller = new Controller();
controller.Text();}

Controller 类:

public class Controller  {

@FXML private Label questionLabel;
@FXML private RadioButton answer_btn1;
@FXML private RadioButton answer_btn2;
@FXML private RadioButton answer_btn3;
@FXML private RadioButton answer_btn4;
@FXML private Button nextBtn;



public void Text() {
List <String> questions = new ArrayList<String>();

questions.add("pytanie1");
questions.add("pytanie2");
questions.add("pytanie3");
questions.add("pytanie4");
questions.add("pytanie5");
questions.add("pytanie6");
questions.add("pytanie7");
questions.add("pytanie8");
questions.add("pytanie9");
questions.add("pytanie10");

String[][] answers = new String[1][4];
answers[1][1] = "a) odp";
answers[1][2] = "b) odp";
answers[1][3] = "c) odp";
answers[1][4] = "d) odp";


questionLabel.setText("");

questionLabel.setText(questionLabel.getText()+answers[1][1]);
answer_btn1.setText("aaa");
}

如何更改名称按钮和标签?

最佳答案

您的代码存在一些问题。

  1. 数组索引从零开始,如有必要,应以 arrayLength - 1 结束。您的代码:answers[1][1] = "a) odp"; ... 答案[1][4] = "d) odp";。它应该是什么:answers[0][0] = "a) odp"; ... 答案[0][3] = "d) odp";
  2. 初始化 Controller 应该在 Controller 的初始化方法中完成。

示例代码如下:

主要:

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

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

@Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("Controller/Quiz.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.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;

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

@FXML
private Label questionLabel;
@FXML
private RadioButton answer_btn1;
@FXML
private RadioButton answer_btn2;
@FXML
private RadioButton answer_btn3;
@FXML
private RadioButton answer_btn4;
@FXML
private Button nextBtn;

@Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
List<String> questions = new ArrayList();

questions.add("pytanie1");
questions.add("pytanie2");
questions.add("pytanie3");
questions.add("pytanie4");
questions.add("pytanie5");
questions.add("pytanie6");
questions.add("pytanie7");
questions.add("pytanie8");
questions.add("pytanie9");
questions.add("pytanie10");

String[][] answers = new String[1][4];
answers[0][0] = "a) odp";
answers[0][1] = "b) odp";
answers[0][2] = "c) odp";
answers[0][3] = "d) odp";

questionLabel.setText("");

questionLabel.setText(questionLabel.getText() + answers[0][0]);
answer_btn1.setText("aaa");
}

}

FXML

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication226.FXMLDocumentController">
<children>
<Button fx:id="nextBtn" layoutX="118.0" layoutY="161.0" text="Click Me!" />
<Label fx:id="questionLabel" layoutX="124.0" layoutY="36.0" minHeight="16" minWidth="69" />
<RadioButton fx:id="answer_btn1" layoutX="47.0" layoutY="92.0" mnemonicParsing="false" text="RadioButton" />
<RadioButton fx:id="answer_btn2" layoutX="193.0" layoutY="92.0" mnemonicParsing="false" text="RadioButton" />
<RadioButton fx:id="answer_btn3" layoutX="47.0" layoutY="128.0" mnemonicParsing="false" text="RadioButton" />
<RadioButton fx:id="answer_btn4" layoutX="183.0" layoutY="128.0" mnemonicParsing="false" text="RadioButton" />
</children>
</AnchorPane>

我假设您的项目结构如下所示。 enter image description here

关于java - 无法更改标签和按钮值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51545757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com