我想在登录后从userField访问仪表板上的用户名配置文件的方法getUsername。但登录控制器登录后方法会出错。 Thankss
MainApp
public class MainApp extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/view/Login.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setMaximized(false);
primaryStage.setMinHeight(500.0);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
的LoginController
public class LoginController implements Initializable {
public MLogin loginModel = new MLogin();
@FXML
private Label status;
@FXML
private JFXTextField userField;
@FXML
private JFXPasswordField passField;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML
public void Login (ActionEvent event) throws IOException {
try {
if (loginModel.isLogin(userField.getText(), passField.getText())) {
((Node)event.getSource()).getScene().getWindow().hide();
Stage primaryStage = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader();
Parent root = fxmlLoader.load(getClass().getResource("/view/App.fxml").openStream());
AppController appCtrl = (AppController)fxmlLoader.getController();
appCtrl.GetUser(userField.getText());
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setMaximized(false);
primaryStage.show();
} else {
status.setText("Username atau password anda salah");
}
} catch (SQLException e) {
status.setText("Username atau password anda salah");
} catch(IOException e) {
e.printStackTrace();
}
}
@FXML
private void Close(javafx.event.ActionEvent event) {
Platform.exit();
System.exit(0);
}
}
登录模式
public class MLogin {
Connection conection;
public MLogin() {
conection = Connections.Mysql();
if(conection == null) {
System.exit(1);
}
}
public boolean isDbConnected() {
try {
return !conection.isClosed();
} catch (SQLException e) {
return false;
}
}
public boolean isLogin(String username, String password) throws SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
String query = "select * from users where username = ? and password = ?";
try {
ps = conection.prepareStatement(query);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
if (rs.next()) {
return true;
}
else {
return false;
}
} catch (Exception e) {
return false;
} finally {
ps.close();
rs.close();
}
} }
登录FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane fx:id="loginBg" prefHeight="620.0" prefWidth="989.0" stylesheets="@../styles/mainStyle.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.xaxxis.controller.LoginController">
<children>
<HBox alignment="CENTER" prefHeight="535.0" prefWidth="760.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<VBox alignment="CENTER" prefHeight="620.0" prefWidth="726.0">
<children>
<AnchorPane prefHeight="384.0" prefWidth="668.0">
<children>
<Label layoutX="32.0" layoutY="34.0" prefHeight="55.0" prefWidth="283.0" text="Xaxxis Project" textFill="#eeebeb">
<font>
<Font size="40.0" />
</font>
</Label>
<Label layoutX="32.0" layoutY="80.0" text="Manda Putra System Information" textFill="#e8e6e6">
<font>
<Font size="21.0" />
</font>
</Label>
<AnchorPane layoutX="410.0" layoutY="44.0" prefHeight="296.0" prefWidth="273.0" style="-fx-background-color: #050f1a;" styleClass="loginPane" AnchorPane.bottomAnchor="40.0" AnchorPane.rightAnchor="40.0" AnchorPane.topAnchor="40.0">
<children>
<GridPane layoutY="108.0" prefHeight="89.0" prefWidth="273.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="100.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="242.218994140625" minWidth="10.0" prefWidth="139.78271484375" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="141.17401123046875" minWidth="10.0" prefWidth="133.21728515625" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<JFXTextField fx:id="userField" alignment="CENTER" focusColor="#0b9bd7" labelFloat="true" maxWidth="253.0" minWidth="253.0" prefHeight="33.0" prefWidth="253.0" promptText="Username" unFocusColor="#f8f6f6" />
<JFXPasswordField fx:id="passField" focusColor="#0b9bd7" labelFloat="true" maxWidth="253.0" minWidth="253.0" prefHeight="33.0" prefWidth="253.0" promptText="Password" unFocusColor="#fcfcfc" GridPane.rowIndex="1" />
</children>
</GridPane>
<JFXButton buttonType="RAISED" layoutX="38.0" layoutY="210.0" onAction="#Login" prefHeight="33.0" prefWidth="88.0" ripplerFill="#d3cbcb" style="-fx-background-color: #0B9BD7;" text="Login" AnchorPane.leftAnchor="38.0" />
<Label layoutX="16.0" layoutY="34.0" prefHeight="33.0" prefWidth="198.0" text="Login to System" textFill="#f5f5f5">
<font>
<Font size="25.0" />
</font>
</Label>
<Label layoutX="17.0" layoutY="63.0" prefHeight="14.0" prefWidth="249.0" text="Enter your username and password to log on:" textFill="WHITESMOKE">
<font>
<Font size="11.0" />
</font>
</Label>
<JFXButton buttonType="RAISED" layoutX="147.0" layoutY="210.0" onAction="#Close" prefHeight="33.0" prefWidth="88.0" ripplerFill="#d3cbcb" style="-fx-background-color: #0B9BD7;" text="Cancel" AnchorPane.rightAnchor="38.0" />
<Label fx:id="status" layoutX="7.0" layoutY="267.0" prefHeight="19.0" prefWidth="250.0" textFill="#fc0d0d" AnchorPane.bottomAnchor="18.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="267.0" />
<ImageView fitHeight="46.0" fitWidth="37.0" layoutX="215.0" layoutY="15.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../icon/lock.png" />
</image>
</ImageView>
<Text fill="#fcfafa" layoutX="21.0" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Welcome" wrappingWidth="102.99127197265625">
<font>
<Font size="14.0" />
</font>
</Text>
</children>
</AnchorPane>
<ImageView fitHeight="197.0" fitWidth="196.0" layoutX="103.0" layoutY="116.0" pickOnBounds="true" preserveRatio="true" rotate="47.7">
<image>
<Image url="@../images/rocket.gif" />
</image>
</ImageView>
</children></AnchorPane>
<Label text="Copyright by Zaki Ibrahim" textFill="#eee8e8">
<font>
<Font size="16.0" />
</font>
</Label>
</children>
</VBox>
</children>
</HBox>
</children>
</AnchorPane>
AppControler
public class AppController implements Initializable {
@FXML
private Label userLabel;
@FXML
private MenuButton userAcc;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void GetUser(String user) {
// TODO
userLabel.setText(user);
}
}
App FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="593.0" prefWidth="900.0" stylesheets="@../styles/Application.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.xaxxis.controller.AppController">
<children>
<StackPane prefHeight="593.0" prefWidth="767.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<BorderPane prefHeight="400.0" prefWidth="600.0">
<left>
<AnchorPane fx:id="headerLine" prefHeight="593.0" prefWidth="229.0">
<children>
<ScrollPane layoutY="43.0" prefHeight="534.0" prefViewportHeight="678.0" prefViewportWidth="128.0" prefWidth="193.0" translateX="1.0" translateY="1.0" translateZ="1.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="-1.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="59.0">
<content>
<AnchorPane prefHeight="534.0" prefWidth="225.0">
<children>
<MenuButton fx:id="usrAcc" mnemonicParsing="false" prefHeight="79.0" prefWidth="225.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="-4.0" AnchorPane.topAnchor="0.0">
<items>
<MenuItem mnemonicParsing="false">
<graphic>
<AnchorPane prefHeight="140.0" prefWidth="140.0">
<children>
<Circle fill="DODGERBLUE" layoutX="55.0" layoutY="100.0" radius="45.0" stroke="BLACK" strokeType="INSIDE" AnchorPane.topAnchor="0.0" />
<Label text="User Profile" AnchorPane.leftAnchor="110.0" AnchorPane.topAnchor="0.0">
<font>
<Font name="System Bold" size="19.0" />
</font>
</Label>
<Label fx:id="userLabel" text="Username" AnchorPane.leftAnchor="110.0" AnchorPane.topAnchor="25.0" />
<JFXButton buttonType="RAISED" style="-fx-background-color: #373737;" text="Logout.." textFill="#fcfafa" AnchorPane.leftAnchor="22.0" AnchorPane.topAnchor="100.0" />
</children>
</AnchorPane>
</graphic>
</MenuItem>
</items>
<graphic>
<AnchorPane prefHeight="53.0" prefWidth="175.0">
<children>
<Circle fill="DODGERBLUE" layoutX="28.0" layoutY="33.0" radius="30.0" stroke="BLACK" strokeType="INSIDE" />
<Label fx:id="userLabel" layoutX="61.0" layoutY="3.0" prefHeight="25.0" prefWidth="101.0" text="Username" textFill="#f8f8f8">
<font>
<Font name="System Bold" size="17.0" />
</font>
</Label>
<Label layoutX="62.0" layoutY="23.0" prefHeight="15.0" prefWidth="43.0" text="Online" textFill="#0f832e" underline="true" />
</children>
</AnchorPane>
</graphic>
</MenuButton>
<Separator layoutY="78.0" prefHeight="3.0" prefWidth="226.0" />
<JFXButton alignment="BASELINE_LEFT" graphicTextGap="8.0" layoutX="-1.0" layoutY="82.0" prefHeight="50.0" prefWidth="227.0" ripplerFill="#2aa0e7" style="-fx-background-color: TRANSPARENT;" text="Home" textFill="#f5f5f5">
<font>
<Font size="21.0" />
</font>
<graphic>
<ImageView fitHeight="36.0" fitWidth="52.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../icon/computer.png" />
</image>
</ImageView>
</graphic></JFXButton>
<JFXButton alignment="BASELINE_LEFT" graphicTextGap="13.0" layoutX="-1.0" layoutY="125.0" prefHeight="50.0" prefWidth="227.0" style="-fx-background-color: TRANSPARENT;" text="Purchasing" textFill="#f5f5f5">
<font>
<Font size="21.0" />
</font>
<graphic>
<ImageView fitHeight="36.0" fitWidth="52.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../icon/purchase.png" />
</image>
</ImageView>
</graphic>
</JFXButton>
<JFXButton alignment="BASELINE_LEFT" graphicTextGap="13.0" layoutY="174.0" prefHeight="50.0" prefWidth="227.0" style="-fx-background-color: TRANSPARENT;" text="Opname" textFill="#f5f5f5">
<font>
<Font size="21.0" />
</font>
<graphic>
<ImageView fitHeight="36.0" fitWidth="52.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../icon/opname.png" />
</image>
</ImageView>
</graphic>
</JFXButton>
</children></AnchorPane>
</content>
</ScrollPane>
<GridPane prefHeight="60.0" prefWidth="229.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="110.31568145751953" minWidth="10.0" prefWidth="64.64358520507812" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="164.35641479492188" minWidth="10.0" prefWidth="164.35641479492188" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<ToggleButton fx:id="sideMenuBtn" mnemonicParsing="false" prefHeight="41.0" prefWidth="43.0" translateX="5.0" translateY="5.0">
<graphic>
<ImageView fitHeight="38.0" fitWidth="36.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../icon/3%20bar.png" />
</image>
</ImageView>
</graphic>
</ToggleButton>
</children>
</HBox>
</children>
</GridPane>
</children>
</AnchorPane>
</left>
<center>
<BorderPane prefHeight="593.0" prefWidth="565.0" BorderPane.alignment="CENTER">
<top>
<AnchorPane prefHeight="60.0" prefWidth="538.0" style="-fx-background-color: #FFFFFF;" BorderPane.alignment="CENTER">
<children>
<GridPane prefHeight="60.0" prefWidth="538.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
</AnchorPane>
</top>
</BorderPane>
</center>
</BorderPane>
</children>
</StackPane>
</children>
</AnchorPane>
我的错误:
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
at com.xaxxis.controller.LoginController.Login(LoginController.java:57)
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)
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:8411)
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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1118)
at javafx.scene.image.Image.<init>(Image.java:693)
at com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:47)
at com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:37)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:763)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
... 57 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1110)
... 63 more
第57行登录控制器
Parent root = fxmlLoader.load(getClass().getResource("/view/App.fxml").openStream());
答案 0 :(得分:1)
您正尝试使用location resolution加载图片:
<Image url="@../icon/computer.png" />
这仅在FXMLLoader
设置location
时有效。但是,由于您使用load(...)
方法获取输入流,并且从不指定位置,FXMLLoader
无法解析相对于FXML文件位置的位置(就加载器而言,那里如果以这种方式做事,甚至可能不是文件。)
因此,请指定位置并使用无参数load()
方法:
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/view/App.fxml"));
Parent root = fxmlLoader.load();
请注意,您也可以将前两行缩写为
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/App.fxml"));