Java 不能更改标签和按钮值
我正在使用JavaFx编写一个简单的问答游戏应用程序。我想要设置标签和按钮上的文本,以及列表和表格中的数据 我创建了三个类:Main、Controller和quick.fxml。我在沟通课程和方法上有问题。 在“Main.java”中,我创建了对象“Controller Controller=new Controller()”,并从“Controller.java”类调用方法“Controller.setText” 现在,我有几个选择: -如果构造函数“public Controller(){}”应用程序中的declate list(ArrayList questions)和tab(String[][]answers)不起作用,则此行出错“” -如果在“Text()”方法(列表、选项卡和设置标签上的文本)中声明所有内容,则应用程序将运行,但值按钮和标签不会更改 -如果我在“setData()”中声明列表和选项卡,然后我想从“Text()”方法更改按钮和标签值,我看不到列表,我必须在“Text()”中声明相同的列表“问题” 控制器类:Java 不能更改标签和按钮值,java,javafx,Java,Javafx,我正在使用JavaFx编写一个简单的问答游戏应用程序。我想要设置标签和按钮上的文本,以及列表和表格中的数据 我创建了三个类:Main、Controller和quick.fxml。我在沟通课程和方法上有问题。 在“Main.java”中,我创建了对象“Controller Controller=new Controller()”,并从“Controller.java”类调用方法“Controller.setText” 现在,我有几个选择: -如果构造函数“public 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");
}
公共类控制器{
@FXML专用标签;
@FXML专用单选按钮应答\u btn1;
@FXML专用单选按钮应答器\u btn2;
@FXML专用单选按钮应答器\u btn3;
@FXML专用单选按钮应答器4;
@FXML专用按钮nextBtn;
公共无效文本(){
列出问题=新建ArrayList();
问题。添加(“pytanie1”);
问题。添加(“pytanie2”);
问题。添加(“pytanie3”);
问题。添加(“pytanie4”);
问题。添加(“pytanie5”);
问题。添加(“pytanie6”);
问题。添加(“pytanie7”);
问题。添加(“pytanie8”);
问题。添加(“pytanie9”);
问题。添加(“pytanie10”);
字符串[][]答案=新字符串[1][4];
答案[1][1]=“a)odp”;
答案[1][2]=“b)odp”;
答案[1][3]=“c)odp”;
答案[1][4]=“d)odp”;
questionLabel.setText(“”);
questionLabel.setText(questionLabel.getText()+答案[1][1]);
答案1.setText(“aaa”);
}
要更改名称按钮和标签,我必须做什么?您的问题是,您正在实例化一个未绑定到视图的新控制器 从加载程序获取控制器,如下所示:
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Controller/Quiz.fxml"));
Parent root = loader.load();
Controller controller = loader.getController();
这样,所有内容都将正确连接。您的代码有几个问题
答案[1][1]=“a)odp”…答案[1][4]=“d)odp”
。应该是什么:答案[0][0]=“a)odp”…答案[0][3]=“d)odp”
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");
}
}
控制器:
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");
}
}
import java.net.URL;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.ResourceBundle;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.RadioButton;
/**
*
*@author blj0011
*/
公共类FXMLDocumentController实现可初始化
{
@FXML
自有标签;
@FXML
专用无线按钮应答器;
@FXML
专用无线按钮应答器2;
@FXML
专用无线按钮应答器(btn3);
@FXML
专用无线按钮应答器4;
@FXML
私人按钮nextBtn;
@凌驾
公共void初始化(URL、ResourceBundle rb)
{
//待办事项
列出问题=新建ArrayList();
问题。添加(“pytanie1”);
问题。添加(“pytanie2”);
问题。添加(“pytanie3”);
问题。添加(“pytanie4”);
问题。添加(“pytanie5”);
问题。添加(“pytanie6”);
问题。添加(“pytanie7”);
问题。添加(“pytanie8”);
问题。添加(“pytanie9”);
问题。添加(“pytanie10”);
字符串[][]答案=新字符串[1][4];
答案[0][0]=“a)odp”;
答案[0][1]=“b)odp”;
答案[0][2]=“c)odp”;
答案[0][3]=“d)odp”;
questionLabel.setText(“”);
questionLabel.setText(questionLabel.getText()+答案[0][0]);
答案1.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=new Controller();Controller.Text();
?我看不出它的用途。如果需要初始化控制器
,请使用控制器的
@覆盖公共无效初始化(URL,ResourceBundle rb){//您的代码在这里!}
method.ok,我在您的示例中更改了Parent root=fxmloader.load(getClass().getResource(“/Controller/quick.fxml”);
。应用程序正在运行,但仍然没有更改值按钮。如果关闭应用程序窗口,控制台中会出现错误:“java.lang.reflect.InvocationTargetException”,并将焦点放在以下行:Controller.Text()谢谢!我还有一个问题。一开始我创建了三个包:Controller(inside Controller.java)、View(inside quick.fxml)和Model(inside Main.java)(其他包)。只有当我将quick.fxml移动到包控制器时,我才成功地设置了controll类。从另一个包中设置*.fxml控制器的方法是什么?问一个新问题并显示您的项目结构。我有一个猜测,但仅此而已。