Хорошо, поэтому я разработал два графических интерфейса, а также по 2 контроллера для каждого из них. Таким образом, в первом окне / gui (LoginMain.f xml) есть кнопка регистрации, и при нажатии на нее открывается другое gui (SignUP.f xml). Но по какой-то причине, когда я связываю файл SignUP.f xml с его контроллером, он не открывается, когда я нажимаю кнопку регистрации. Но когда я не связываю это, это работает отлично.
Вот мой основной метод:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("LoginMain.fxml"));
primaryStage.setTitle("Ford Car Review");
primaryStage.setScene(new Scene(root, 550, 450));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Вот мой loginMain.f xml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="426.0" prefWidth="547.0" style="-fx-background-color: #2D3447;
-fx-border- color: yellow;"
xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.LoginController">
<TextField fx:id="username" layoutX="195.0" layoutY="155.0" prefHeight="25.0" prefWidth="197.0"
promptText="Enter username" style="-fx-background-color: white;"><font>
<Font size="14.0" />
</font>
</TextField>
<PasswordField fx:id="password" layoutX="195.0" layoutY="200.0"
prefHeight="25.0" prefWidth="197.0"
promptText="Enter password" style="-fx-background-color: white;">
<font>
<Font size="14.0" />
</font>
</PasswordField>
<Button fx:id="signUpButton" layoutX="446.0" layoutY="368.0"
onAction="#signUp" prefHeight="30.0"
prefWidth="70.0" text="Sign Up">
<font>
<Font size="14.0" />
</font>
</Button>
<Button fx:id="login" layoutX="297.0" layoutY="267.0" onAction="#loginAction"
prefHeight="30.0" prefWidth="61.0" text="Login" />
<CheckBox fx:id="remember" layoutX="152.0" layoutY="267.0" prefHeight="30.0"
prefWidth="107.0" text="Remember Me" textFill="WHITE" />
<Button fx:id="forgotPassword" layoutX="167.0" layoutY="316.0" mnemonicParsing="false"
prefHeight="25.0" prefWidth="197.0" style="-fx-background-color: #2D3447;"
text="Forgot Your Password?"
textFill="WHITE" underline="true" />
<ImageView fitHeight="83.0" fitWidth="129.0" layoutX="152.0"
layoutY="23.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/images.jpg" />
</image>
</ImageView>
<ImageView fitHeight="30.0" fitWidth="35.0" layoutX="152.0" layoutY="155.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/username.jpg" />
</image>
</ImageView>
<ImageView fitHeight="30.0" fitWidth="30.0" layoutX="152.0" layoutY="200.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/password.png" />
</image>
</ImageView>
<Label layoutX="285.0" layoutY="23.0" prefHeight="30.0" prefWidth="84.0"
text="FORD" textFill="#6958d7">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<Label layoutX="285.0" layoutY="53.0" prefHeight="25.0" prefWidth="70.0"
text="CAR" textFill="#6958d7">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<Label layoutX="284.0" layoutY="81.0" text="REVIEW" textFill="#6958d7">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<ImageView fx:id="progress" fitHeight="83.0" fitWidth="125.0" layoutX="210.0" layoutY="341.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/source.gif" />
</image>
</ImageView>
</AnchorPane>
Вот мой класс Login Controller:
package sample;
import javafx.animation.PauseTransition;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.IOException;
public class LoginController {
@FXML
private Button signUpButton;
@FXML
private TextField username;
@FXML
private CheckBox remember;
@FXML
private Button login;
@FXML
private Button forgotPassword;
@FXML
private ImageView progress;
@FXML
private PasswordField password;
public void initialize() {
progress.setVisible(false);
}
@FXML
public void loginAction() {
progress.setVisible(true);
PauseTransition pt = new PauseTransition();
pt.setDuration(Duration.seconds(3));
pt.setOnFinished(e -> System.out.println("Login Successfully"));
pt.play();
}
@FXML
public void signUp() {
try {
login.getScene().getWindow().hide();
Stage signUpStage = new Stage();
Parent root = FXMLLoader.load(Main.class.getResource("/sample/SignUP.fxml"));
Scene scene = new Scene(root);
signUpStage.setScene(scene);
signUpStage.show();
signUpStage.setResizable(false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Вот мой файл SignUP.f xml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="462.0" prefWidth="547.0" style="-fx-background-color: #2D3447;
-fx-border-color: yellow;"
xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.SignUpController">
<TextField layoutX="160.0" layoutY="145.0" prefHeight="25.0" prefWidth="228.0"
promptText="Enter Username" style="-fx-background-color: white;">
<font>
<Font size="14.0" />
</font>
</TextField>
<PasswordField layoutX="160.0" layoutY="190.0" prefHeight="25.0" prefWidth="228.0"
promptText="Enter Password" style="-fx-background-color: #2D34white47;">
<font>
<Font size="14.0" />
</font>
</PasswordField>
<RadioButton layoutX="168.0" layoutY="248.0" mnemonicParsing="false" selected="true"
text="Male" textFill="#a0a2ab">
<font>
<Font size="14.0" />
</font>
<toggleGroup>
<ToggleGroup fx:id="gender" />
</toggleGroup>
</RadioButton>
<RadioButton layoutX="239.0" layoutY="248.0" mnemonicParsing="false" text="Female"
textFill="#a0a2ab" toggleGroup="$gender">
<font>
<Font size="14.0" />
</font>
</RadioButton>
<TextField layoutX="160.0" layoutY="289.0" prefHeight="25.0" prefWidth="228.0"
promptText="Enter Location">
<font>
<Font size="14.0" />
</font>
</TextField>
<Button layoutX="136.0" layoutY="350.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="239.0"
onAction="#signUP" style="-fx-background-color: #2196f3;" text="Sign UP" textFill="#ddd1d1">
<font>
<Font size="14.0" />
</font>
</Button>
<ImageView fitHeight="30.0" fitWidth="33.0" layoutX="121.0" layoutY="243.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/gender.png" />
</image>
</ImageView>
<ImageView fitHeight="30.0" fitWidth="30.0" layoutX="121.0" layoutY="145.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/username.jpg" />
</image>
</ImageView>
<ImageView fitHeight="30.0" fitWidth="30.0" layoutX="121.0" layoutY="190.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/password.png" />
</image>
</ImageView>
<ImageView fitHeight="30.0" fitWidth="30.0" layoutX="121.0" layoutY="289.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/location.png" />
</image>
</ImageView>
<Button layoutX="448.0" layoutY="36.0" mnemonicParsing="false" prefHeight="30.0"
prefWidth="56.0" style="-fx-background-color: #2196f3;" text="Login" textFill="WHITE" />
<ImageView fitHeight="89.0" fitWidth="131.0" layoutX="121.0" layoutY="14.0"
pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/images.jpg" />
</image>
</ImageView>
<Label layoutX="260.0" layoutY="14.0" prefHeight="30.0" prefWidth="84.0"
text="FORD" textFill="#7a6ae4">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<Label layoutX="260.0" layoutY="43.0" prefHeight="30.0" prefWidth="70.0"
text="CAR" textFill="#7a6ae4">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<Label layoutX="260.0" layoutY="73.0" prefHeight="30.0" prefWidth="92.0"
text="REVIEW" textFill="#7a6ae4">
<font>
<Font name="Century Schoolbook" size="20.0" />
</font>
</Label>
<ImageView fx:id="progress" fitHeight="80.0" fitWidth="105.0" layoutX="215.0"
layoutY="382.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../img/source.gif" />
</image>
</ImageView>
<RadioButton layoutX="320.0" layoutY="250.0" mnemonicParsing="false" text="Other"
textFill="#c3b5b5">
<font>
<Font size="14.0" />
</font>
</RadioButton>
</AnchorPane>
Вот мой класс SignUpController:
package sample;
import javafx.animation.PauseTransition;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Duration;
public class SignUpController {
@FXML
private AnchorPane parentPane;
@FXML
private Button login;
@FXML
private TextField name;
@FXML
private Button signUp;
@FXML
private RadioButton male;
@FXML
private ToggleGroup gender;
@FXML
private RadioButton female;
@FXML
private RadioButton other;
@FXML
private TextField location;
@FXML
private ImageView progress;
@FXML
private PasswordField password;
public void initialize() {
progress.setVisible(false);
}
@FXML
public void signUP() {
progress.setVisible(true);
PauseTransition pt = new PauseTransition();
pt.setDuration(Duration.seconds(3));
pt.setOnFinished(e -> System.out.println("Sign Up Successful!"));
pt.play();
}
}
Наконец, вот ошибка, которую я получил:
javafx.fxml.LoadException:
/C:/Users/Owner/Desktop/JavaPrograms/GaveUp/out/production/GaveUp/sample/SignUP.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at GaveUp/sample.LoginController.signUp(LoginController.java:53)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at
javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent
(CompositeEventHandler.java:86)
at
javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent
(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased
(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord
.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent
(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent
(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent
(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent
(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run
(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run
(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2
(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock
(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent
(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.TextField
field sample.SignUpController.location to java.net.URL
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException
(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException
(UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.set
(UnsafeObjectFieldAccessorImpl.java:81)
at java.base/java.lang.reflect.Field.set(Field.java:780)
at javafx.fxml/javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1174)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
... 66 more