我嘗試通過根據功能創建單獨的包來整理我的應用程式的檔案并遇到此錯誤:
java.lang.IllegalAccessException: class javafx.fxml.FXMLLoader$ValueElement (in module javafx.fxml) cannot access class application.login.Login (in module eSentral_Desktop_App) because module eSentral_Desktop_App does not export application.login to module javafx.fxml
我的問題是如何解決這個問題以及導致它發生的原因?
以下是一些額外資訊:
這是我的類層次結構:

這是我的 Main.java 代碼:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import jfxtras.styles.jmetro.JMetro;
import jfxtras.styles.jmetro.Style;
import javafx.scene.Parent;
import javafx.scene.Scene;
import java.io.IOException;
import application.login.Users;
import application.listing.Homepage;
public class Main extends Application{
public static Object currentUser = new Object();
double x,y = 0;
private static Stage stg;
@Override
public void start(Stage primaryStage) throws Exception{
try {
stg = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("login/LoginUI.fxml"));
Scene scene = new Scene(root);
JMetro jMetro = new JMetro(scene, Style.LIGHT);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public void changeScene(String fxml) throws IOException {
Parent pane = FXMLLoader.load(getClass().getResource(fxml));
stg.getScene().setRoot(pane);
}
public static void main(String[] args) {
launch(args);
}
}
這是我的 Login.java LoginUI.fxml 代碼的控制器類
package application.login;
import application.utils.Constants;
import application.Main;
import application.utils.ESentralConstants;
import application.utils.HttpUrlConnectionAPI;
import application.login.Users;
import application.utils.WhiteLabel;
import application.utils.SerializationUtil;
import application.utils.Utils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Cursor;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import java.io.IOException;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Login extends Main{
private ESentralConstants eConstants;
@FXML
private Label lblEmail, lblPassword, lblStatus,lblForgot;
@FXML
private TextField txtUsername;
@FXML
private TextField txtPassword;
@FXML
private Button btnLogin, btnFB, btnRegister;
public void loginAuthentication() throws Exception
{
eConstants = new ESentralConstants();
final String logIn_UserName = txtUsername.getText();
String logIn_UserPassword = txtPassword.getText();
txtPassword.setText("");
lblStatus.setText("");
lblEmail.setText("");
lblPassword.setText("");
if (logIn_UserName.length() == 0) {
lblEmail.setText("Email field is empty");
Platform.runLater(() -> txtUsername.requestFocus());
} else if (logIn_UserPassword.length() == 0) {
lblPassword.setText("Password field is empty");
Platform.runLater(() -> txtPassword.requestFocus());
}else
{
txtUsername.setText("");
// HttpUrlConnectionAPI httpUrlConnectionAPI = new HttpUrlConnectionAPI();
String urlParameters = eConstants.getTAG() eConstants.getVALUE()
Constants.LOGIN_API_TAG_USERNAME logIn_UserName
Constants.LOGIN_API_TAG_PASSWORD logIn_UserPassword;
// Constants.LOGIN_API_TAG_DEVICEID uDI.uniqueId();
Task<String> signInTask = new HttpUrlConnectionAPI(eConstants.getLOGIN_API_LOGIN_URL(), urlParameters);
//Call HttpUrlConnectionAPI Task instance
Thread signInThread = new Thread(signInTask);
signInThread.setDaemon(true);
signInThread.start();
signInTask.setOnSucceeded((WorkerStateEvent t) -> {
String resultToCheck = signInTask.getValue();
//Handling Server reply when user trying to SignIn
if (resultToCheck.matches(Constants.LOGIN_API_REPLY_FULL)) {
lblEmail.setText(Constants.ERR_DIALOG_TITLE " " Constants.MGS_ACTIVATION " " Constants.MGS_DEACTIVATE);
} else if (resultToCheck.matches(Constants.LOGIN_API_REPLY_FAILED)) {
lblEmail.setText("Invalid Email or Password");
} else {
lblStatus.setText("Login successful");
//Utils.makeDirectory(WhiteLabel.STORAGE_USERS_FOLDER logIn_UserName Constants.DOWNLOADED_BOOKS);
// Parsing String into Json Object
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(resultToCheck);
JsonObject jsonObject = jsonElement.getAsJsonObject();
// Parsing String into Java Object
Users user = new Users(jsonObject.get(Constants.LOGIN_API_REPLY_ID).toString().replace("\"", ""), Utils.md5Hash(logIn_UserPassword),
jsonObject.get(Constants.LOGIN_API_REPLY_USERNAME).toString().replace("\"", ""), jsonObject.get(Constants.LOGIN_API_REPLY_CONTACT).toString().replace("\"", ""),
jsonObject.get(Constants.LOGIN_API_REPLY_EMAIL).toString().replace("\"", ""), jsonObject.get(Constants.LOGIN_API_REPLY_ENCRYPTKEY).toString().replace("\"", ""),
jsonObject.get(Constants.LOGIN_API_REPLY_LOGINKEY).toString().replace("\"", ""), jsonObject.get(Constants.LOGIN_API_REPLY_NAME).toString().replace("\"", ""), true, Constants.EMPTY);
Main.currentUser = (Users) user;
try {
Main m = new Main();
m.changeScene("homepage.fxml");
}
catch(Exception e)
{
e.printStackTrace();
}
/*try {
// Storing user object into storage
// Navigate to the Listing Scene
}catch (IOException e) {
lblStatus.setText("Permission not granted");
}*/
//Utils.addOrDeleteUserFromList(logIn_UserName, true);
}
});
signInTask.setOnFailed((event) -> {
lblStatus.setText("Process failed.");
});
signInTask.setOnCancelled((event) -> {
lblStatus.setText("Process cancelled.");
});
}
}
public void login(ActionEvent event) throws Exception
{
this.loginAuthentication();
}
public void forgetPassword(MouseEvent ev) throws Exception
{
try {
Main m = new Main();
m.changeScene("forgetPassword.fxml");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void MouseEnter(MouseEvent ev)
{
lblForgot.setCursor(Cursor.HAND);
btnRegister.setCursor(Cursor.HAND);
btnLogin.setCursor(Cursor.HAND);
btnFB.setCursor(Cursor.HAND);
}
public void MouseExit(MouseEvent ev)
{
lblForgot.setCursor(Cursor.DEFAULT);
btnRegister.setCursor(Cursor.DEFAULT);
btnLogin.setCursor(Cursor.DEFAULT);
btnFB.setCursor(Cursor.DEFAULT);
}
}
這是我的 LoginUI.fxml 代碼:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="700.0" prefWidth="800.1" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.login.Login">
<top>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="115.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<ImageView fitHeight="80.0" fitWidth="249.0" layoutX="276.0" layoutY="18.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/ic_eSentral_Logo.png" />
</image>
</ImageView>
</children>
</AnchorPane>
</top>
<bottom>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="133.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="btnRegister" layoutX="248.0" layoutY="63.0" mnemonicParsing="false" onm ouseEntered="#MouseEnter" onm ouseExited="#MouseExit" prefHeight="44.0" prefWidth="318.0" style="-fx-background-color: #169fe3;" text="Register a new account" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Label alignment="CENTER" layoutX="311.0" layoutY="24.0" text="Don't have an account yet?" textFill="#1286bc">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
</children>
</AnchorPane>
</bottom>
<center>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="487.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<Label fx:id="lblSuccess" alignment="CENTER" layoutX="247.0" layoutY="2.0" prefHeight="24.0" prefWidth="318.0" textFill="#00ff26">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<TextField fx:id="txtUsername" layoutX="247.0" layoutY="77.0" prefHeight="44.0" prefWidth="318.0" promptText="Email">
<font>
<Font size="15.0" />
</font>
</TextField>
<Label fx:id="lblEmail" alignment="CENTER" layoutX="247.0" layoutY="121.0" prefHeight="24.0" prefWidth="318.0" textFill="#ec1e1e">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<PasswordField fx:id="txtPassword" layoutX="247.0" layoutY="145.0" prefHeight="44.0" prefWidth="318.0" promptText="Password">
<font>
<Font size="15.0" />
</font>
</PasswordField>
<Label fx:id="lblPassword" alignment="CENTER" layoutX="247.0" layoutY="189.0" prefHeight="24.0" prefWidth="318.0" textFill="#ec1e1e">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<Label fx:id="lblForgot" alignment="CENTER" layoutX="326.0" layoutY="226.0" onm ouseClicked="#forgetPassword" onm ouseEntered="#MouseEnter" onm ouseExited="#MouseExit" text="Forget your password? " textFill="#1286bc" underline="true">
<font>
<Font size="15.0" />
</font>
</Label>
<Label fx:id="lblForgot1" alignment="CENTER" layoutX="380.0" layoutY="35.0" text="Login" textFill="#1286bc">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Label>
<Button fx:id="btnLogin" layoutX="247.0" layoutY="274.0" mnemonicParsing="false" onAction="#login" onm ouseEntered="#MouseEnter" onm ouseExited="#MouseExit" prefHeight="44.0" prefWidth="318.0" style="-fx-background-color: #169fe3;" text="Log in" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
<Button fx:id="btnFB" alignment="CENTER" layoutX="247.0" layoutY="380.0" mnemonicParsing="false" onm ouseEntered="#MouseEnter" onm ouseExited="#MouseExit" prefHeight="44.0" prefWidth="318.0" style="-fx-background-color: #4766a9;" text="Log in with Facebook" textFill="WHITE">
<font>
<Font name="System Bold" size="20.0" />
</font>
<graphic>
<ImageView fitHeight="35.0" fitWidth="63.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/ic_facebook.png" />
</image>
</ImageView>
</graphic>
</Button>
<Separator layoutX="247.0" layoutY="338.0" prefHeight="28.0" prefWidth="130.0" />
<Separator layoutX="434.0" layoutY="338.0" prefHeight="28.0" prefWidth="130.0" />
<Label layoutX="392.0" layoutY="337.0" text="OR" textFill="#1286bc">
<font>
<Font name="System Bold" size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
</center>
</BorderPane>
這是模塊資訊代碼:
module eSentral_Desktop_App {
requires javafx.controls;
requires java.net.http;
requires javafx.fxml;
requires javafx.base;
requires javafx.graphics;
requires com.google.gson;
requires java.logging;
requires java.desktop;
requires org.jfxtras.styles.jmetro;
opens application to javafx.graphics, javafx.fxml;
}
uj5u.com熱心網友回復:
謝謝至 珠寶海,這個問題就解決了。我需要做的就是添加
opens application.login to javafx.fxml;
進入模塊資訊.java。
它看起來像這樣:
module eSentral_Desktop_App {
requires javafx.controls;
requires java.net.http;
requires javafx.fxml;
requires javafx.base;
requires javafx.graphics;
requires com.google.gson;
requires java.logging;
requires java.desktop;
requires org.jfxtras.styles.jmetro;
opens application.login to javafx.fxml;
opens application to javafx.graphics, javafx.fxml;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/412025.html
標籤:
下一篇:GWT超級開發模式內容安全策略
