gpt4 book ai didi

java - 由 : javafx. fxml.LoadException 错误引起

转载 作者:行者123 更新时间:2023-12-01 10:36:45 25 4
gpt4 key购买 nike

这段代码有效,但现在我遇到了麻烦。我认为这就是问题所在,但我不知道:

Caused by: javafx.fxml.LoadException: 
/C:/Users/alex/workspace/FXMLExample/bin/application/login.fxml:10

我不知道/理解这些代码有什么问题。主要只是运行场景。» Main.java

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;

public class Main extends Application {

Parent root;

@Override
public void start(Stage stage) throws Exception {
root = FXMLLoader.load(getClass().getResource("login.fxml"));

Scene scene = new Scene(root, 800, 600);

stage.setTitle("Login system");
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(args);
}
}

»login.fxml,我认为这就是问题所在

<?xml version="1.0" encoding="UTF-8"?>

<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="fxmlexample.Buton"
alignment="center" hgap="10" vgap="10" styleClass="root">
<padding>
<Insets top="25" right="25" bottom="10" left="25" />
</padding>

<Text fx:id="bunvenit" text="Bun venit!" GridPane.columnIndex="0"
GridPane.rowIndex="0" />

<Text fx:id="text" text="Logheaza-te folosind datele tale personale:"
GridPane.columnIndex="0" GridPane.rowIndex="1" GridPane.columnSpan="2" />

<Label text="Username:" GridPane.columnIndex="0"
GridPane.rowIndex="2" />

<TextField fx:id="username" GridPane.columnIndex="1"
GridPane.rowIndex="2" />

<Label text="Parola:" GridPane.columnIndex="0" GridPane.rowIndex="3" />

<PasswordField fx:id="parola" GridPane.columnIndex="1"
GridPane.rowIndex="3" />

<HBox spacing="10" alignment="bottom_right" GridPane.columnIndex="1"
GridPane.rowIndex="5">
<Button text="Logheaza-te" onAction="#loginButton" />
</HBox>

<Text fx:id="eroare" GridPane.columnIndex="1" GridPane.rowIndex="7" />

<Button text="Am uitat parola" GridPane.columnIndex="0"
GridPane.rowIndex="8" onAction="#passwordButton" />

<Button text="Creeaza un cont nou" GridPane.columnIndex="1"
GridPane.rowIndex="8" onAction="#createAccountButton" />

<stylesheets>
<URL value="@application.css" />
</stylesheets>

</GridPane>

»Buton.java,所有按钮操作

package fxmlexample;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public abstract class Buton {

private static final int SCENE_HEIGHT = 600;
private static final int SCENE_WIDTH = 800;
@FXML
private Text eroare;
@FXML
private TextField username;
@FXML
private PasswordField parola;
@FXML
private Parent root;

/* loginButton */
@FXML
protected void loginButton(ActionEvent event) throws IOException {
boolean gasit = false;
try (BufferedReader buf = new BufferedReader(new FileReader("data//in//users.txt"))) {
String linie;
gasit = false;
while ((linie = buf.readLine()) != null) {
String part[] = linie.split(" ");
if (username.getText().equals(part[0]) && parola.getText().equals(part[1])) {
gasit = true;
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

if (gasit) {
Parent root = FXMLLoader.load(getClass().getResource("../application/logged.fxml"));
Scene scene = new Scene(root, SCENE_WIDTH, SCENE_HEIGHT);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setScene(scene);
stage.setTitle("Felicitari, te-ai logat cu succes!");
stage.show();
} else {
eroare.setText("Username sau parola incorecta!");
}
}
}

» 错误:

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(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
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(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Users/alex/workspace/FXMLExample/bin/application/login.fxml:10

at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.access$700(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at application.Main.start(Main.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
... 1 more
Caused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
... 23 more
Exception running application application.Main

最佳答案

它清楚地表明:

java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)

它只是无法实例化您的抽象 Buton 类。使其具体化。

关于java - 由 : javafx. fxml.LoadException 错误引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34660512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com