Can';t从其他类编辑Javafx应用程序类中的非静态文本区域
我正在尝试在JavaFX中为我一直在制作的基于文本的游戏实现GUI 主类的这一部分设置了所有内容:Can';t从其他类编辑Javafx应用程序类中的非静态文本区域,java,javafx,fxml,Java,Javafx,Fxml,我正在尝试在JavaFX中为我一直在制作的基于文本的游戏实现GUI 主类的这一部分设置了所有内容: public class Main extends Application{ @FXML protected TextField input; @FXML protected TextArea output, inventory, commands; protected static List<String> history; protected static int hist
public class Main extends Application{
@FXML
protected TextField input;
@FXML
protected TextArea output, inventory, commands;
protected static List<String> history;
protected static int historyPointer;
protected static String textToRead = null;
private Service<Void> backgroundThread;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("Console.fxml"));
BorderPane root = (BorderPane) loader.load();
history = new ArrayList<>();
historyPointer = 0;
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("MyConsoleFXGUI"); //Could later be changed so that the actual game title is displayed here.
stage.show();
这个方法应该有效,我的问题是我不知道如何从游戏类调用它。因为Main类没有实例化,所以我不能调用Main对象,也不能使文本区域保持静态,因为这不适用于JavaFx应用程序
那么,我如何从一个单独的类调用“printGameInfo”来为文本区域设置一些字符串呢
非常感谢 主类肯定是实例化的,或者任何东西都应该如何调用其非静态的start方法?当您实例化游戏类时,可以将引用传递给主类实例。然而,这是一个非常全面的软件设计 您应该阅读有关JavaFx控制器的内容:如果未实例化
Main
类,则该类不应具有实例成员。不要将应用程序
类用作控制器类。首先为控制器创建一个单独的类,然后从那里开始。看到了,非常感谢,我会试试的!这是一个多么糟糕的软件设计?我只是在努力改进,所以我很高兴收到关于如何编写更好代码的反馈。
/**
* Called when the game wants to print something to the game
* @param message The text to be printed to the console.
*/
public void printGameInfo(String message) {
System.out.println("This method was attempted!");
output.setText(message + System.lineSeparator());
}