我无法将fxml文件加载到我的JavaFX应用程序中。 任何人都可以帮我处理我的代码吗? 我是JavaFx的新手,所以告诉我它是否不正确
这是我的项目结构 enter image description here
这是来自
的登录控制器package Controllers;
import Models.System.LoginModel;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Paint;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class SigninController implements Initializable {
@FXML
public JFXButton admSection;
@FXML
public JFXButton opSection;
@FXML
public JFXTextField username;
@FXML
public JFXPasswordField password;
@FXML
public Label loginHint;
@FXML
public JFXButton btnLogin;
@FXML
public AnchorPane bottomBorder;
@FXML
private StageController stageController;
// Class Login
LoginModel loginModel;
{
try {
loginModel = new LoginModel();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
username.setDisable(true);
password.setDisable(true);
bottomBorder.setStyle("-fx-background-color: #e8e8e8");
loginHint.setText("Please select your section");
}
@FXML
public void admAction(ActionEvent actionEvent) {
setField("#03A9F4");
}
@FXML
public void opAction(ActionEvent actionEvent) {
setField("#E91E63");
}
@FXML
public void loginAction(ActionEvent actionEvent) {
try {
if (loginModel.loginAdmin(username.getText(), password.getText())) {
try {
StackPane home = FXMLLoader.load(getClass().getResource("../Views/Dashboard.fxml"));
stageController.stackPane.getChildren().setAll(home);
} catch (IOException e) {
e.printStackTrace();
}
} else {
loginHint.setText("Invalid username or password");
}
} catch (SQLException e) {
loginHint.setText("Login Failed");
e.printStackTrace();
}
}
public void setParentController(StageController stageController) {
this.stageController = stageController;
}
public void setField(String color) {
String admin = "#03A9F4";
String operator = "#E91E63";
username.setDisable(false);
password.setDisable(false);
loginHint.setText("Please insert your username and password");
username.requestFocus();
username.setFocusColor(Paint.valueOf(String.valueOf(color)));
password.setFocusColor(Paint.valueOf(String.valueOf(color)));
bottomBorder.setStyle("-fx-background-color: " + color);
}
}
我想将另一个fxml文件加载到主舞台的窗格中 这是我的主舞台控制器
package Controllers;
import Helpers.EffectUtilities;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialog;
import com.jfoenix.controls.JFXDialogLayout;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ToggleButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.Window;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class StageController implements Initializable {
@FXML
public StackPane stackPane;
@FXML
public AnchorPane stagePane;
@FXML
public AnchorPane outterPane;
@FXML
public AnchorPane stageControl;
@FXML
public Button closeBtn;
@FXML
public Button minimizeBtn;
@FXML
public ToggleButton maximizeBtn;
@FXML
private SigninController signinController;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
StackPane home = FXMLLoader.load(getClass().getResource("../Views/Signin.fxml"));
stagePane.getChildren().addAll(home);
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
public void closeAction(ActionEvent actionEvent) {
JFXDialogLayout closeDialog = new JFXDialogLayout();
closeDialog.setBody(new Text("Are you sure want to exit Inventory Management?"));
JFXDialog dialog = new JFXDialog(stackPane, closeDialog, JFXDialog.DialogTransition.CENTER);
// Yes Button
JFXButton yesButton = new JFXButton("Yes");
yesButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dialog.close();
System.exit(0);
}
});
// No Button
JFXButton noButton = new JFXButton("No");
noButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dialog.close();
}
});
closeDialog.setActions(noButton, yesButton);
dialog.show();
}
@FXML
public void minimizeAction(ActionEvent actionEvent) {
Stage primaryStage = (Stage) outterPane.getScene().getWindow();
primaryStage.setIconified(true);
}
@FXML
public void maximizeAction(ActionEvent actionEvent) {
if (maximizeBtn.isSelected()) {
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
Node source = (Node) actionEvent.getSource();
Window primaryStage = source.getScene().getWindow();
primaryStage.setX(bounds.getMinX());
primaryStage.setY(bounds.getMinY());
primaryStage.setWidth(bounds.getWidth());
primaryStage.setHeight(bounds.getHeight());
} else {
Node source = (Node) actionEvent.getSource();
Window primaryStage = source.getScene().getWindow();
primaryStage.setWidth(1200);
primaryStage.setHeight(600);
}
}
public void controlDragged(MouseEvent mouseEvent) {
Stage primaryStage = (Stage) outterPane.getScene().getWindow();
EffectUtilities.makeDraggable(primaryStage, stageControl);
}
}
我不知道这是怎么回事 我有这样的错误
"C:\Program Files\Java\jdk1.8.0_151\bin\java" -
agentlib:jdwp=transport=dt_socket,address=127.0.0.1:52060,suspend=y,server=n -javaagent:C:\Users\MUHAMA~1\AppData\Local\Temp\capture12724jars\debugger-
agent.jar=C:\Users\MUHAMA~1\AppData\Local\Temp\capture10492.props -
Dfile.encoding=UTF-8 -classpath "C:\Program
Files\Java\jdk1.8.0_151\jre\lib\charsets.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\deploy.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\access-bridge-64.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\cldrdata.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\dnsns.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\jaccess.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\jfxrt.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\localedata.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\nashorn.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\sunec.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\sunjce_provider.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\sunmscapi.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\sunpkcs11.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\ext\zipfs.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\javaws.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\jce.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\jfr.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\jfxswt.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\jsse.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\management-agent.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\plugin.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\resources.jar;C:\Program
Files\Java\jdk1.8.0_151\jre\lib\rt.jar;C:\Users\Muhamad
Iqbal\IdeaProjects\Inventory Management\out\production\Inventory
Management;C:\Users\Muhamad Iqbal\IdeaProjects\Inventory
Management\lib\mysql-connector-java-5.1.44.jar;C:\Users\Muhamad
Iqbal\Downloads\jfoenix.jar;D:\Program Files\IntelliJ IDEA Community Edition
2017.3.2\lib\idea_rt.jar" Main
Connected to the target VM, address: '127.0.0.1:52060', transport: 'socket'
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException:
java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at
com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException
at Controllers.SigninController.loginAction(SigninController.java:76)
... 58 more
我在第7行遇到错误:
@FXML
public void loginAction(ActionEvent actionEvent) {
try {
if (loginModel.loginAdmin(username.getText(), password.getText())) {
try {
StackPane home = FXMLLoader.load(getClass().getResource("../Views/Dashboard.fxml"));
stageController.stackPane.getChildren().setAll(home);
} catch (IOException e) {
e.printStackTrace();
}
} else {
loginHint.setText("Invalid username or password");
}
} catch (SQLException e) {
loginHint.setText("Login Failed");
e.printStackTrace();
}
}
错误表明我有一个NullPointerException,我该如何解决这个问题?