Main中控制器类中的JavaFX访问函数



这让我的头撞到墙上了!!!!希望有人能给我指明正确的方向。我确信我正在做一件非常愚蠢和愚蠢的事情。我搜索了又搜索,但似乎找不到为什么这不起作用!!(在IntelliJ IDE 15.x上使用JDK8.x和Scenebuilder)。我正试图在GUI上显示数据,但希望通过编程从其他类/方法将这些数据发送给它。。。。这是一个简单的概念,我正在努力使工作之前,我的更大的项目:

Main Class:
package sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
Controller myController = new Controller();
@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 345, 200));
    primaryStage.show();
    myController.pushBtn();
}
public static void main(String[] args) {
    launch(args);
   }
  }

我的简单GUI,用Scenebuilder Controller类在FXML中定义:

package sample;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class Controller {
@FXML // ResourceBundle that was given to the FXMLLoader
private ResourceBundle resources;
@FXML // URL location of the FXML file that was given to the FXMLLoader
private URL location;
@FXML // fx:id="txtBox"
private TextField txtBox; // Value injected by FXMLLoader
@FXML // fx:id="myButton"
private Button myButton; // Value injected by FXMLLoader
@FXML // This method is called by the FXMLLoader when initialization is   complete
void initialize() {
    assert txtBox != null : "fx:id="txtBox" was not injected: check your   FXML file 'sample.fxml'.";
    assert myButton != null : "fx:id="myButton" was not injected: check     our FXML file 'sample.fxml'.";
}
@FXML
private void pressBtn(ActionEvent event){
    txtBox.setText("Btn was pushed...");
}
@FXML
public void pushBtn(){
    txtBox.appendText("Sample from Main");
}
}

最后是我非常简单的FXML文件,以及ID和事件处理程序标签:

<GridPane alignment="center" hgap="10" vgap="10"      xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65"       fx:controller="sample.Controller">
  <columnConstraints>
  <ColumnConstraints />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints />
 </rowConstraints>
 <children>
   <Pane prefHeight="200.0" prefWidth="345.0" style="-fx-background-color:  GRAY;">
       <children>
         <TextField fx:id="txtBox" layoutX="63.0" layoutY="28.0"     prefHeight="53.0" prefWidth="224.0" style="-fx-background-color: LIGHTGRAY;" />
          <Button fx:id="myButton" layoutX="140.0" layoutY="100.0"  mnemonicParsing="false" onAction="#pressBtn" text="Button" />
       </children>
      </Pane>
  </children>
 </GridPane>

我所要做的就是调用一个在控制器类上声明为公共的函数,并让该函数将文本放在我的txtBox中,正如你所看到的,它是用FXML定义的。我在main中创建了该控制器类的一个实例,然后使用该实例访问该方法。我一直在这里阅读和搜索,指出这是正确的方法。然而,这永远不会奏效,我得到的只是一个错误:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in   Application start method
  at      com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at    com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImp    l.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at sample.Controller.pushBtn(Controller.java:43)
at sample.Main.start(Main.java:20)
at    com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherIm    pl.java:863)
at     com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at    com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at    com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at     com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at     com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more `

我到底错过了什么?有没有另一种方法可以称之为,我不是在初始化某个东西吗?非常感谢您的反馈!!!

调用new Controller()时,您正在创建第二个Controller实例。当然,这不是FXMLLoader在加载FXML文件时创建的Controller实例,因此它不会初始化任何@FXML注入的字段。

您需要从FXMLLoader:获取Controller实例

package sample ;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
        Parent root = loader.load();
        Controller myController = loader.getController();
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 345, 200));
        primaryStage.show();
        myController.pushBtn();
   }
   public static void main(String[] args) {
       launch(args);
   }
}

最新更新