GUI未显示

时间:2016-02-27 00:45:12

标签: java

所以我必须非常快地构建这个JavaFX应用程序,我的代码编译但GUI没有启动,我得到例外。一旦实现了FileChooser代码,问​​题就开始了。

public class Main extends Application {

    @FXML //  fx:id="openButton"
    private Button openButton; // Value injected by FXMLLoader

    @Override
    public void start(Stage primaryStage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("Plot.fxml"));

        openButton.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent arg0) {
                FileChooser fileChooser = new FileChooser();
                FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
                fileChooser.getExtensionFilters().add(extFilter);
                File file = fileChooser.showOpenDialog(primaryStage);
                System.out.println(file);
            }
        });

        primaryStage.setTitle("Plotter");
        primaryStage.setScene(new Scene(root, 1024 , 768));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

FXML文件是这样的:

<BorderPane prefHeight="400.0" prefWidth="723.0" xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <HBox alignment="TOP_CENTER" prefHeight="50.0" spacing="10.0" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="openButton" mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Open Dataset" />
            <Button mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Button" />
            <Button mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Button" />
         </children>
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
         <BorderPane.margin>
            <Insets />
         </BorderPane.margin>
      </HBox>
   </top>
</BorderPane>

我完全是JavaFX的新手。任何提示表示赞赏。附:我正在使用Gluon场景构建器。

感谢。

例外情况:

  

应用程序启动方法中的异常线程“main”中的异常   java.lang.RuntimeException:Application start方法中的异常   com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)     在   com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 155(LauncherImpl.java:182)     在java.lang.Thread.run(Thread.java:745)引起:   在sample.Main.start(Main.java:29)处的java.lang.NullPointerException   com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 162(LauncherImpl.java:863)     在   com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 175(PlatformImpl.java:326)     在   com.sun.javafx.application.PlatformImpl.lambda为$ null $ 173(PlatformImpl.java:295)     在java.security.AccessController.doPrivileged(Native Method)at   com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 174(PlatformImpl.java:294)     在   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个

     

使用退出代码1完成处理

1 个答案:

答案 0 :(得分:0)

答案是分离Controller和Application类,这样你就不会得到Application类的两个实例。否则,Application类的一个实例将不会初始化某些成员,并最终会抛出空指针异常。

package plot;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;

import java.io.File;

public class Controller {

    @FXML //  fx:id="openButton"
    private Button openButton; // Value injected by FXMLLoader

    @FXML
    public void open(ActionEvent e) {
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
        fileChooser.getExtensionFilters().add(extFilter);
        File file = fileChooser.showOpenDialog(openButton.getScene().getWindow());
        System.out.println(file);
    }

}
package plot;

import javafx.application.Application;
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 stage) throws Exception{
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Plot.fxml"));
        Parent root = loader.load();

        stage.setTitle("Plotter");
        stage.setScene(new Scene(root, 1024 , 768));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<BorderPane prefHeight="400.0" prefWidth="723.0" xmlns="http://javafx.com/javafx/null"
            xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="plot.Controller"
>
  <top>
    <HBox alignment="TOP_CENTER" prefHeight="50.0" spacing="10.0" BorderPane.alignment="CENTER">
      <children>
        <Button fx:id="openButton" mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Open Dataset" onAction="#open"/>
        <Button mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Button"/>
        <Button mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Button"/>
      </children>
      <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
      </padding>
      <BorderPane.margin>
        <Insets/>
      </BorderPane.margin>
    </HBox>
  </top>
</BorderPane>

参见相关内容(我建议研究第一个链接以了解此解决方案的工作原理)。