【问题标题】:JavaFx :Exception in Application start methodJavaFx:应用程序启动方法中的异常
【发布时间】:2018-05-29 01:17:03
【问题描述】:

我正在尝试创建一个 JavaFX 程序,每次当我从 jfoenix 库中添加任何东西时,我都会尝试运行我的代码,但我并不完全确定它的含义。 .

我的代码是:

public class home extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("home.fxml"));
        Scene scene = new Scene(root,1300,768);
        scene.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());
        stage.setScene(scene);
        stage.show();

     }

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

我的控制器是:

public class HomeController implements Initializable {

    @FXML
    private JFXButton log;

    @FXML
    private JFXButton engr;

    @FXML
    private Pane login,eng;

    @FXML
    private void changeofpages(MouseEvent event) {
        if (event.getTarget()== log){
            login.setVisible(true);
            eng.setVisible(false);
        }else
            if (event.getTarget()== engr){
            eng.setVisible(true);
            login.setVisible(false);
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
}

【问题讨论】:

  • 如果您遇到异常,请至少在问题中包含异常的全文(包括完整的堆栈跟踪,如果有的话)。 minimal reproducible example 有时会有所帮助。
  • @FXML 在逗号分隔的变量列表上看起来很奇怪,@FXML private Pane login,eng;,我以前从未见过。它可能有效,也可能无效,我不知道。
  • 你能发布你的 fxml 文件和你的堆栈跟踪吗?
  • 为什么你用两个 fx:id 来标识窗格,这是不正确的。
  • 当我运行它时:` 原因:java.lang.UnsupportedClassVersionError: com/jfoenix/controls/JFXTextField 已由 Java 运行时的更新版本(类文件版本 53.0)编译,这Java 运行时的版本仅在 java.lang.ClassLoader.defineClass1(Native Method)` 处识别最高 52.0 的类文件版本`

标签: java javafx


【解决方案1】:

这个问题在javaFx中经常重复,我的意思是这个例外:

应用程序启动方法中的异常

因为你没有发布你的堆栈跟踪任何人都可以找到你的问题,我建议确定以下几点:

在您的主应用程序中:

  1. 确保您的 fxml 文件的路径正确。
  2. 确保您的 css 文件的路径正确。

在您的控制器中:

  1. 确保节点 fx:id 存在于 fxml 中并且是正确的。
  2. 确保为事件导入类。

有时你会发现类名相同但包不同

我给你一个MouseEvent的例子,它出现在awt包和javafx包中

我尝试在所有条件下编写您的代码,它调用异常,因为它可以找到 Style.css(也许这是错误),但之后一切都很好:

这是您的主要应用程序: 包堆栈溢出;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Xlint Xms
 */
public class home extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("home.fxml")); //Be sure of your path
        Scene scene = new Scene(root, 1300, 768);
         scene.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());//Be sure of your Style.css file
        stage.setScene(scene);
        stage.show();

    }

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

还有你的控制器:

public class HomeController implements Initializable {

    @FXML
    private JFXButton log;

    @FXML
    private JFXButton engr;

    @FXML
    private Pane login,eng;

    /*Be sure of MouseEvent class :It is in javafx package not awt package*/
    @FXML
    private void changeofpages(MouseEvent event) {
        if (event.getTarget() == log) {
            login.setVisible(true);
            eng.setVisible(false);
        } else if (event.getTarget() == engr) {
            eng.setVisible(true);
            login.setVisible(false);
        }
    }
    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

这段代码对我有用,我也希望对你有用。

【讨论】:

  • 我认为我的问题在这里:/*请确保 MouseEvent 类:它在 javafx 包中而不是 awt 包中*/ 但我该怎么做
  • 我的问题是在我从 jfoenix 库中添加任何东西时显示(例如 JFXButton
  • 当我运行它时:Caused by: java.lang.UnsupportedClassVersionError: com/jfoenix/controls/JFXTextField has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0 at java.lang.ClassLoader.defineClass1(Native Method)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2021-01-13
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
相关资源
最近更新 更多