我已经检查了所有与我的问题相似的解决方案,但是我的问题仍然保持不变。我的Painter.fxml文件,PainterController.java文件和包含main方法的Painter.java文件都在同一文件夹中,但是仍然给我运行时错误。主要问题在于,包含FXMLLoader的load方法的主类无法在程序执行期间隐式创建控制器类的实例。请问我该如何解决这个问题?
MAIN CLASS:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Painter extends Application
{
public void start(Stage stage) throws Exception
{
Parent root =FXMLLoader.load(getClass().getResource("Painter.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Painter");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
CONTROLLER CLASS:
// PainterController.java
// Controller for the Painter app
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
public class PainterController
{
// enum representing pen sizes
private enum PenSize{
SMALL(2),
MEDIUM(4),
LARGE(6);
private final int radius;
PenSize(int radius) {this.radius = radius;}
public int getRadius() {return radius;}
};
// instance variables that refer to GUI components
@FXML private RadioButton blackRadioButton;
@FXML private RadioButton redRadioButton;
@FXML private RadioButton greenRadioButton;
@FXML private RadioButton blueRadioButton;
@FXML private RadioButton smallRadioButton;
@FXML private RadioButton mediumRadioButton;
@FXML private RadioButton largeRadioButton;
@FXML private ToggleGroup colorToggleGroup;
@FXML private ToggleGroup sizeToggleGroup;
@FXML private Pane drawingAreaPane;
// instance variables for managing Painter state
private PenSize radius = PenSize.MEDIUM; // radius of circle
private Paint brushColor = Color.BLACK; // drwing color
// set user data for the RadioButtons
@FXML
public void initialize()
{
// user data on a control can be any Object
blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);
smallRadioButton.setUserData(PenSize.SMALL);
mediumRadioButton.setUserData(PenSize.MEDIUM);
largeRadioButton.setUserData(PenSize.LARGE);
}
// handles drawingArea's onMouseDragged MouseEvent
@FXML
private void drawingAreaMouseDragged(MouseEvent event)
{
Circle newCircle = new Circle(event.getX(), event.getY(),
radius.getRadius(), brushColor);
drawingAreaPane.getChildren().add(newCircle);
}
// handles color RadioButton's ActionEvents
@FXML
private void colorRadioButtonSelected(ActionEvent event)
{
// user data for each color RadioButton is the corresponding Color
brushColor =
(Color) colorToggleGroup.getSelectedToggle().getUserData();
}
// handles size RadioButton's ActionEvents
@FXML
private void sizeRadioButtonSelected(ActionEvent event)
{
// user data for each size RadioButton is the corresponding PenSize
radius =
(PenSize) sizeToggleGroup.getSelectedToggle().getUserData();
}
// handles Undo Button's ActionEvents
@FXML
private void undoButtonPressed(ActionEvent event)
{
int count = drawingAreaPane.getChildren().size();
// if there are any shapes remove the last one added
if (count > 0)
{
drawingAreaPane.getChildren().remove(count - 1);
}
}
// handles Clear Button's ActionEvents
@FXML
private void clearButtonPressed(ActionEvent event)
{
drawingAreaPane.getChildren().clear(); // clear the canvas
}
}
RUN TIME ERRORS:
C:\JAVAFX2>java Painter
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
产生于:javafx.fxml.LoadException: /C:/JAVAFX2/Painter.fxml:12
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at Painter.start(Painter.java:12)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at 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$152(WinApplication.java:177)
... 1 more
Caused by: java.lang.ClassNotFoundException: PainterController
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
... 22 more
Exception running application Painter