You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX文档加载主程序失败,onAction='#LoadDB'解析错误求助

Fixing "Error resolving onAction='#LoadDB'" in JavaFX

Hey there, let's break down this error you're facing and get your JavaFX app up and running. That error message means JavaFX can't find the LoadDB method you referenced in your FXML file—either because the method doesn't exist in the right place, has the wrong signature, or your FXML isn't pointing to the correct controller.

Here are the key fixes to check:

1. Move the LoadDB method to a dedicated controller class (not Main!)

Your Main class is meant to launch the app and load the FXML, not handle UI events. You need a separate controller class for your FXML's logic.

Example controller class (application/DBController.java):

package application;

import javafx.event.ActionEvent;

public class DBController {
    // This method matches the #LoadDB in your FXML
    public void LoadDB(ActionEvent event) {
        // Add your database loading logic here
        System.out.println("Database loading started!");
    }
}

Make sure your FXML file specifies the controller using the fx:controller attribute on the root node. For example:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Button?>

<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="application.DBController">
    <!-- Your UI elements here -->
    <Button text="Load Database" onAction="#LoadDB"/>
</BorderPane>

3. Verify the method signature is correct

The LoadDB method must follow these rules:

  • It's public (JavaFX can't access private methods)
  • Returns void
  • Takes a single parameter of type ActionEvent (or the appropriate event type for your UI element)
  • Don't forget to import javafx.event.ActionEvent in the controller!

4. Double-check case sensitivity

Java is case-sensitive, so #LoadDB in FXML must match exactly with the method name in your controller (capital L, capital D, capital B—no typos!).

5. Ensure your FXML path is correct in Main

Update your start method to properly load the FXML file (replace your_fxml_file.fxml with your actual file name):

@Override
public void start(Stage primaryStage) {
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("your_fxml_file.fxml"));
        BorderPane root = loader.load();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Database Loader");
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

If you follow these steps, that error should disappear and your LoadDB action will trigger correctly.

内容的提问来源于stack exchange,提问作者Kevin Wang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:28:55