有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法更改标签和按钮值

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

我创建了三个类:Main、Controller和quick。fxml。我在沟通课程和方法上有问题。 总的来说。java'我创建了对象“Controller Controller=new Controller()”,并从“Controller”调用方法“Controller.setText”。java的类

现在,我有几个选择:

-如果构造函数“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");
}

要更改名称按钮和标签,我必须做些什么


共 (2) 个答案

  1. # 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. 应在控制器的初始化方法中初始化控制器

    下面是示例代码:

    Main:

    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>
    

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

  2. # 2 楼答案

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

    从加载程序获取控制器,如下所示:

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

    这样一切都会连接好