英文:
FXML Exception error while running my code
问题
以下是您的应用程序包内的主要内容:
package application;
import java.net.URL;
import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.Model;
public class Main extends Application {
Parent root;
Scene scene;
@Override
public void start(Stage primaryStage) {
try {
URL url = getClass().getResource("/view/AddCustomer.fxml");
root = FXMLLoader.load(url);
scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(e -> {
int save = JOptionPane.showConfirmDialog(null, "Do you want to save changes in the product table?");
if (save == 0) {
Model.getInstance().SaveCustomers();
Model.getInstance().SaveSessions();
Model.getInstance().SaveAdministrators();
Model.getInstance().SaveCoachs();
}
System.exit(0);
});
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
这是控制器包内的addCustomerController:
package controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
public class AddCustomerController {
@FXML
private Button cancelMemberButton;
@FXML
private Pane customerPane;
@FXML
private RadioButton dailyMemberType;
@FXML
private TextField memberDiscountField;
@FXML
private RadioButton memberFemaleButton;
@FXML
private TextField memberIdField;
@FXML
private RadioButton memberMaleButton;
@FXML
private TextField memberMobileField;
@FXML
private TextField memberNameField;
@FXML
private TextField memberNationalityField;
@FXML
private RadioButton monthlyMemberType;
@FXML
private Button saveMemberButton;
@FXML
private RadioButton yearlyMemberType;
@FXML
void cancelButtonClick(ActionEvent event) {
}
@FXML
void saveButtonClick(ActionEvent event) {
}
}
这是视图包内的FXML文件(称为AddCustomer.fxml):
<?xml version="1.0" encoding="UTF-8"?>
<?import com.gluonhq.charm.glisten.control.ToggleButtonGroup?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<Pane fx:id="customerPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="532.0" prefWidth="789.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.AddCustomerController">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" layoutX="258.0" layoutY="34.0" prefHeight="41.0"
prefWidth="274.0" text="Add Customer" textFill="#860b0b">
<font>
<Font name="Arial" size="37.0" />
</font>
</Label>
<Separator layoutY="98.0" prefHeight="1.0" prefWidth="789.0" style="-fx-background-color: #860b0b;">
<opaqueInsets>
<Insets />
</opaqueInsets>
</Separator>
<Label layoutX="27.0" layoutY="144.0" prefHeight="23.0" prefWidth="103.0" text="Member ID" />
<Label layoutX="27.0" layoutY="195.0" prefHeight="23.0" prefWidth="103.0" text="Name" />
<Label layoutX="27.0" layoutY="243.0" prefHeight="23.0" prefWidth="103.0" text="Mobile" />
<Label layoutX="27.0" layoutY="296.0" prefHeight="23.0" prefWidth="103.0" text="Nationality" />
<Label layoutX="27.0" layoutY="345.0" prefHeight="23.0" prefWidth="103.0" text="Gender" />
<Label layoutX="27.0" layoutY="394.0" prefHeight="23.0" prefWidth="103.0" text="MemberShip Type" />
<Label layoutX="27.0" layoutY="440.0" prefHeight="23.0" prefWidth="103.0" text="Discount" />
<TextField fx:id="memberIdField" layoutX="246.0" layoutY="143.0" prefHeight="30.0" prefWidth="486.0" />
<TextField fx:id="memberNameField" layoutX="246.0" layoutY="194.0" prefHeight="25.0" prefWidth="486.0" />
<TextField fx:id="memberMobileField" layoutX="246.0" layoutY="242.0" prefHeight="25.0" prefWidth="486.0" />
<TextField fx:id="memberNationality" layoutX="246.0" layoutY="295.0" prefHeight="25.0" prefWidth="486.0" />
<RadioButton fx:id="memberMaleButton" layoutX="341.0" layoutY="348.0" mnemonicParsing="false" text="Male" />
<RadioButton fx:id="memberFemaleButton" layoutX="412.0" layoutY="348.0" mnemonicParsing="false"
text="Female" />
<RadioButton fx:id="dailyMemberType" layoutX="341.0" layoutY="397.0" mnemonicParsing="false" text="Daily" />
<RadioButton fx:id="monthlyMemberType" layoutX="412.0" layoutY="397.0" mnemonicParsing="false"
text="Monthly" />
<RadioButton fx:id="yearlyMemberType" layoutX="499.0" layoutY="397.0" mnemonicParsing="false" text="Yearly" />
<TextField fx:id="discountField" editable="false" layoutX="246.0" layoutY="439.0" prefHeight="25.0"
prefWidth="486.0" />
<Button fx:id="saveMemberButton" layoutX="246.0" layoutY="482.0" mnemonicParsing="false"
onAction="#saveButtonClick" prefHeight="30.0" prefWidth="164.0" text="Save" />
<Button fx:id="cancelMemberButton" layoutX="499.0" layoutY="482.0" mnemonicParsing="false"
onAction="#cancelButtonClick" prefHeight="30.0" prefWidth="164.0" text="Cancel" />
<details>
<summary>英文:</summary>
This is my main inside application package:
package application;
import java.net.URL;
import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.Model;
public class Main extends Application {
Parent root;
Scene scene;
@Override
public void start(Stage primaryStage) {
try {
URL url = getClass().getResource("/view/AddCustomer.fxml");
root = FXMLLoader.load(url);
scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(e -> {
int save = JOptionPane.showConfirmDialog(null, "Do you want to save changes in the product table?");
if (save == 0) {
Model.getInstance().SaveCustomers();
Model.getInstance().SaveSessions();
Model.getInstance().SaveAdministrators();
Model.getInstance().SaveCoachs();
}
System.exit(0);
});
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
This is my addCustomerController inside controller package:
package controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
public class AddCustomerController {
@FXML
private Button cancelMemberButton;
@FXML
private Pane customerPane;
@FXML
private RadioButton dailyMemberType;
@FXML
private TextField memberDiscountField;
@FXML
private RadioButton memberFemaleButton;
@FXML
private TextField memberIdField;
@FXML
private RadioButton memberMaleButton;
@FXML
private TextField memberMobileField;
@FXML
private TextField memberNameField;
@FXML
private TextField memberNationalityField;
@FXML
private RadioButton monthlyMemberType;
@FXML
private Button saveMemberButton;
@FXML
private RadioButton yearlyMemberType;
@FXML
void cancelButtonClick(ActionEvent event) {
}
@FXML
void saveButtonClick(ActionEvent event) {
}
}
This is the FXML file inside view package(Its called AddCustomer.fxml):
<?xml version="1.0" encoding="UTF-8"?>
<?import com.gluonhq.charm.glisten.control.ToggleButtonGroup?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<Pane fx:id="customerPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="532.0" prefWidth="789.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.AddCustomerController">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" layoutX="258.0" layoutY="34.0" prefHeight="41.0" prefWidth="274.0" text="Add Customer" textFill="#860b0b">
<font>
<Font name="Arial" size="37.0" />
</font>
</Label>
<Separator layoutY="98.0" prefHeight="1.0" prefWidth="789.0" style="-fx-background-color: #860b0b;">
<opaqueInsets>
<Insets />
</opaqueInsets>
</Separator>
<Label layoutX="27.0" layoutY="144.0" prefHeight="23.0" prefWidth="103.0" text="Member ID" />
<Label layoutX="27.0" layoutY="195.0" prefHeight="23.0" prefWidth="103.0" text="Name" />
<Label layoutX="27.0" layoutY="243.0" prefHeight="23.0" prefWidth="103.0" text="Mobile" />
<Label layoutX="27.0" layoutY="296.0" prefHeight="23.0" prefWidth="103.0" text="Nationality" />
<Label layoutX="27.0" layoutY="345.0" prefHeight="23.0" prefWidth="103.0" text="Gender" />
<Label layoutX="27.0" layoutY="394.0" prefHeight="23.0" prefWidth="103.0" text="MemberShip Type" />
<Label layoutX="27.0" layoutY="440.0" prefHeight="23.0" prefWidth="103.0" text="Discount" />
<TextField fx:id="memberIdField" layoutX="246.0" layoutY="143.0" prefHeight="30.0" prefWidth="486.0" />
<TextField fx:id="memberNameField" layoutX="246.0" layoutY="194.0" prefHeight="25.0" prefWidth="486.0" />
<TextField fx:id="memberMobileField" layoutX="246.0" layoutY="242.0" prefHeight="25.0" prefWidth="486.0" />
<TextField fx:id="memberNationality" layoutX="246.0" layoutY="295.0" prefHeight="25.0" prefWidth="486.0" />
<RadioButton fx:id="memberMaleButton" layoutX="341.0" layoutY="348.0" mnemonicParsing="false" text="Male" />
<RadioButton fx:id="memberFemaleButton" layoutX="412.0" layoutY="348.0" mnemonicParsing="false" text="Female" />
<RadioButton fx:id="dailyMemberType" layoutX="341.0" layoutY="397.0" mnemonicParsing="false" text="Daily" />
<RadioButton fx:id="monthlyMemberType" layoutX="412.0" layoutY="397.0" mnemonicParsing="false" text="Monthly" />
<RadioButton fx:id="yearlyMemberType" layoutX="499.0" layoutY="397.0" mnemonicParsing="false" text="Yearly" />
<TextField fx:id="discountField" editable="false" layoutX="246.0" layoutY="439.0" prefHeight="25.0" prefWidth="486.0" />
<Button fx:id="saveMemberButton" layoutX="246.0" layoutY="482.0" mnemonicParsing="false" onAction="#saveButtonClick" prefHeight="30.0" prefWidth="164.0" text="Save" />
<Button fx:id="cancelMemberButton" layoutX="499.0" layoutY="482.0" mnemonicParsing="false" onAction="#cancelButtonClick" prefHeight="30.0" prefWidth="164.0" text="Cancel" />
<ToggleButtonGroup layoutX="130.0" layoutY="170.0" selectionType="SINGLE" />
</children>
</Pane>
This is the error I get when run my main:
javafx.fxml.LoadException:
/C:/Users/ADMIN/eclipse-workspace/Project_V2_V2/bin/view/AddCustomer.fxml
at javafx.fxml@19/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml@19/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2949)
at javafx.fxml@19/javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2793)
at javafx.fxml@19/javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2758)
at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2624)
at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
at javafx.fxml@19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
at javafx.fxml@19/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
at Project_V2_V2/application.Main.start(Main.java:22)
at javafx.graphics@19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics@19/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics@19/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics@19/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@19/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.ClassNotFoundException: com.gluonhq.charm.glisten.control.ToggleButtonGroup
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at javafx.fxml@19/javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:3017)
at javafx.fxml@19/javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:3006)
at javafx.fxml@19/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2947)
... 20 more
I searched online for problems, i found all of the issues that has been offered online are not similar to mine at least from what i understood. I fixed some things that were in my code. However, it still did not solve the issue for me. Please i need help fixing this issue.
</details>
# 答案1
**得分**: 4
你正在使用Gluons的移动工具com.gluonhq.charm.glisten.control.ToggleButtonGroup。你真的想这样做吗?也许使用标准的JavaFX `ToggleGroup` 就足够了。如果你确实想使用Gluons库,那么你必须将这个`implementation 'com.gluonhq:charm-glisten:6.2.2'`添加到你的依赖项中。但请注意,Glisten是一项具有与JavaFX不同许可证的专有产品。
<details>
<summary>英文:</summary>
You are using Gluons com.gluonhq.charm.glisten.control.ToggleButtonGroup from their mobile tooling. Do you really want to do that? Maybe using the standard JavaFX `ToggleGroup` would be sufficient. If you really want to use Gluons lib then you have to add this `implementation 'com.gluonhq:charm-glisten:6.2.2'` to your dependencies. But note, that glisten is a proprietary offering with a different license than JavaFX.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论