【发布时间】: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!}方法。