【问题标题】:cant change label and buttons values无法更改标签和按钮值
【发布时间】:2018-07-26 18:53:59
【问题描述】:

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

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

现在,我有几个选择:

-如果构造函数“public Controller(){}”应用程序中的 declate 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();}

控制器类:

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

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

【问题讨论】:

  • 你为什么要Controller controller = new Controller(); controller.Text();?我看不出它的目的。如果您需要初始化Controller,请使用Controller's @Override public void initialize(URL url, ResourceBundle rb) {//your code here!} 方法。

标签: java javafx


【解决方案1】:

您的代码存在一些问题。

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

下面的示例代码:

主要:

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

}

控制器:

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>

我假设您的项目结构在您的代码中如下所示。

【讨论】:

  • 谢谢!我还有一个问题。一开始我创建了三个包:Controller(在 Controller.java 中)、View(在 Quiz.fxml 中)和 Model(在 Main.java 中)。当我使用 Scene Builder 打开 Quiz.fxml 时,我想在我的 AnchorPane 上设置 Controller 类,但列表中没有选择(其他包)。只有当我将 Quiz.fxml 移动到包控制器时,我才设法设置控制类。从另一个包中设置 *.fxml 控制器的方法是什么?
  • 提出一个新问题并展示您的项目结构。我有一个猜测,但仅此而已。
【解决方案2】:

您的问题是您正在实例化一个未绑定到视图的新控制器。

您可以像这样从加载器中获取控制器:

FXMLLoader loader = new FXMLLoader(getClass().getResource("/Controller/Quiz.fxml"));
Parent root = loader.load();
Controller controller = loader.getController();

这样所有东西都会正确连接。

【讨论】:

  • 好的,我在您的示例中更改了Parent root = FXMLLoader.load(getClass().getResource("/Controller/Quiz.fxml"));。应用程序正在运行,但仍然没有更改值按钮。如果我关闭应用程序窗口,控制台中会出现错误:“java.lang.reflect.InvocationTargetException”并专注于行:controller.Text();answers[1][1] = "a) odp";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多