来自Main

时间:2016-04-11 22:58:06

标签: javafx scenebuilder

这让我撞到了墙上!希望有人能指出我的方向。我确信我正在做一些完全愚蠢和愚蠢的事情。我搜索和搜索,但似乎无法找出为什么这不工作! (在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,在FXML中用Scenebuilder Controller类定义:

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>

我想要做的就是调用一个在控制器类中声明为public的函数,并让该函数将文本放在我的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 `

我错过了什么?还有另一种方法可以称之为,我是不是在初始化某些东西?真的很感激任何反馈!!!

1 个答案:

答案 0 :(得分:2)

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

您需要从Controller

获取FXMLLoader个实例
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);
   }
}