【问题标题】:GUI Not ShowingGUI 不显示
【发布时间】:2016-06-10 09:51:01
【问题描述】:

所以我必须非常快速地构建这个 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:应用程序启动方法中的异常 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) 引起: java.lang.NullPointerException at sample.Main.start(Main.java:29) at 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) 在 com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 在 com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 在 com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 在 com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) ... 1 更多

进程以退出代码 1 结束

【问题讨论】:

  • 您能否提供您看到的具体错误消息?
  • @ChiefTwoPencils 添加。
  • @jewelsea 如果除了标记重复和指向 NullPointers 之外,您实际上有正确的答案,请写一个,否则,请原谅!当人们没有正确的答案并指出不相关的东西时,我讨厌这种情况!原谅这个问题,去别的地方吧!
  • @jewelsea 非常感谢!非常感谢!

标签: java


【解决方案1】:

答案是将 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>

查看相关内容(我建议研究第一个链接以了解此解决方案为何有效)。

【讨论】:

    猜你喜欢
    • 2016-03-02
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2012-11-11
    • 1970-01-01
    • 2018-02-21
    相关资源
    最近更新 更多