我在springboot应用程序中发现以下错误。 FXMLController.java中的错误部分已在此处上传。还上传了必要的文件。我正在尝试连接Java和fxml文件。请帮助我。预先感谢
LibraryAutomationApplication.java
package com.alphabot.library;
import java.io.IOException;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import com.alphabot.library.config.StageManager;
import com.alphabot.library.view.FxmlView;
import javafx.application.Application;
import javafx.stage.Stage;
@SpringBootApplication
public class LibraryAutomationApplication extends Application {
protected ConfigurableApplicationContext springContext;
protected StageManager stageManager;
@Override
public void init() throws Exception {
springContext = springBootApplicationContext();
}
@Override
public void start(Stage stage) throws IOException {
stageManager = springContext.getBean(StageManager.class, stage);
//stage.setResizable(false);
//stage.setFullScreen(true);
showLoginView();
stage.setMinWidth(1000);
stage.setMinHeight(650);
}
private void showLoginView() throws IOException {
stageManager.switchScene(FxmlView.MAIN);
}
public static void main(final String[] args) {
launch(args);
}
@Override
public void stop() throws Exception {
springContext.close();
}
private ConfigurableApplicationContext springBootApplicationContext() {
SpringApplicationBuilder builder = new SpringApplicationBuilder(LibraryAutomationApplication.class);
String[] args = getParameters().getRaw().stream().toArray(String[]::new);
return builder.run(args);
}
}
AppJavaConfig.java
package com.alphabot.library.config;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ResourceBundle;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import com.alphabot.library.logging.ExceptionWriter;
@Configuration
public class AppJavaConfig {
@Autowired
SpringFXMLLoader springFXMLLoader;
@Bean
@Scope("prototype")
public ExceptionWriter exceptionWriter() {
return new ExceptionWriter(new StringWriter());
}
@Bean
public ResourceBundle resourceBundle() {
return ResourceBundle.getBundle("Bundle");
}
@Bean
@Lazy(value = true)
public StageManager stageManager(Stage stage) throws IOException {
return new StageManager(springFXMLLoader, stage);
}
}
SpringFXMLLoader.java
package com.alphabot.library.config;
import java.io.IOException;
import java.util.ResourceBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
@Component
public class SpringFXMLLoader {
private final ResourceBundle resourceBundle;
private final ApplicationContext context;
@Autowired
public SpringFXMLLoader(ApplicationContext context, ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;
this.context = context;
}
public Parent load(String fxmlPath) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setControllerFactory(context::getBean);
loader.setResources(resourceBundle);
loader.setLocation(getClass().getResource(fxmlPath));
return loader.load();
}
}
StageManager.java
package com.alphabot.library.config;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Objects;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.alphabot.library.view.FxmlView;
import com.alphabot.library.config.StageManager;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class StageManager {
private static final Logger LOG = getLogger(StageManager.class);
private SpringFXMLLoader springFXMLLoader;
private final Stage primaryStage;
public StageManager(SpringFXMLLoader springFXMLLoader, Stage stage) {
this.springFXMLLoader = springFXMLLoader;
this.primaryStage = stage;
}
@Autowired
public Node switchScene(final FxmlView view) {
Parent viewRootNodeHierarchy = loadViewNodeHierarchy(view.getFxmlFile());
show(viewRootNodeHierarchy, view.getTitle());
return viewRootNodeHierarchy;
}
private void show(final Parent rootnode, String title) {
Scene scene = prepareScene(rootnode);
primaryStage.setTitle(title);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.centerOnScreen();
try {
primaryStage.show();
} catch (Exception exception) {
logAndExit ("Unable to show scene for title" + title, exception);
}
}
private Scene prepareScene(Parent rootnode){
Scene scene = primaryStage.getScene();
if (scene == null) {
scene = new Scene(rootnode);
}
scene.setRoot(rootnode);
return scene;
}
private Parent loadViewNodeHierarchy(String fxmlFilePath) {
Parent rootNode = null;
try {
rootNode = springFXMLLoader.load(fxmlFilePath);
Objects.requireNonNull(rootNode, "A Root FXML node must not be null");
} catch (Exception exception) {
logAndExit("Unable to load FXML view" + fxmlFilePath, exception);
}
return rootNode;
}
private void logAndExit(String errorMsg, Exception exception) {
LOG.error(errorMsg, exception, exception.getCause());
Platform.exit();
}
}
FXMLController.java
package com.alphabot.library.controller;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.alphabot.library.config.StageManager;
import com.alphabot.library.view.FxmlView;
import com.jfoenix.controls.JFXButton;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
@Component
public class FXMLController implements Initializable {
protected StageManager stageManager;
protected ConfigurableApplicationContext springContext;
@FXML
private StackPane mainPane;
@FXML
private JFXButton btnHome;
@FXML
private JFXButton btnAuthors;
@FXML
private JFXButton btnBooks;
@FXML
private JFXButton btnJournals;
@FXML
private JFXButton btnIssueReturn;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
mainPane.getChildren().clear();
mainPane.getChildren().add(FXMLLoader.load(getClass().getResource("/fxml/HomePanel.fxml")));
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
private void homeOnAction(ActionEvent event) {
Stage stage = (Stage) btnHome.getScene().getWindow();
stage.setTitle("Library Management- Home");
try {
mainPane.getChildren().clear();
mainPane.getChildren().add(FXMLLoader.load(getClass().getResource("/fxml/HomePanel.fxml")));
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
private void authorsOnAction(ActionEvent event) throws IOException {
mainPane.getChildren().clear();
mainPane.getChildren().add(showAuthorsView());
}
@Bean
private Node showAuthorsView() throws IOException {
stageManager = springContext.getBean(StageManager.class, mainPane);
return stageManager.switchScene(FxmlView.AUTHORS);
}
@FXML
private void booksOnAction(ActionEvent event) {
// Stage stage = (Stage) btnBooks.getScene().getWindow();
// stage.setTitle("Library Management- Books");
try {
mainPane.getChildren().clear();
mainPane.getChildren().add(FXMLLoader.load(getClass().getResource("/fxml/BooksPanel.fxml")));
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
private void journalsOnAction(ActionEvent event) {
Stage stage = (Stage) btnJournals.getScene().getWindow();
stage.setTitle("Library Management- Journals");
try {
mainPane.getChildren().clear();
mainPane.getChildren().add(FXMLLoader.load(getClass().getResource("/fxml/JournalsPanel.fxml")));
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
private void issueReturnOnAction(ActionEvent event) {
Stage stage = (Stage) btnIssueReturn.getScene().getWindow();
stage.setTitle("Library Management- Issue or Return");
try {
mainPane.getChildren().clear();
mainPane.getChildren().add(FXMLLoader.load(getClass().getResource("/fxml/IssueReturnPanel.fxml")));
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
ExceptionWriter.java
package com.alphabot.library.logging;
import java.io.PrintWriter;
import java.io.Writer;
public class ExceptionWriter extends PrintWriter {
public ExceptionWriter(Writer writer) {
super(writer);
}
private String wrapAroundWithNewlines(String stringWithoutNewlines) {
return ("\n" + stringWithoutNewlines + "\n");
}
public String getExceptionAsString(Throwable throwable) {
throwable.printStackTrace(this);
String exception = super.out.toString();
return (wrapAroundWithNewlines(exception));
}
}
FxmlView.java
package com.alphabot.library.view;
import java.util.ResourceBundle;
public enum FxmlView {
LOGIN {
@Override
public String getTitle() {
return getStringFromResourceBundle("login.title");
}
@Override
public String getFxmlFile() {
return "/fxml/Login.fxml";
}
},
MAIN {
@Override
public String getTitle() {
return getStringFromResourceBundle("login.title");
}
@Override
public String getFxmlFile() {
return "/fxml/MainPanel.fxml";
}
},
AUTHORS {
@Override
public String getTitle() {
return getStringFromResourceBundle("login.title");
}
@Override
public String getFxmlFile() {
return "/fxml/AuthorsPanel.fxml";
}
},
FPASS {
@Override
public String getTitle() {
return getStringFromResourceBundle("forgotPassword.title");
}
@Override
public String getFxmlFile() {
return "/fxml/ForgotPassword.fxml";
}
};
public abstract String getTitle();
public abstract String getFxmlFile();
String getStringFromResourceBundle(String key){
return ResourceBundle.getBundle("Bundle").getString(key);
}
}
答案 0 :(得分:0)
我这样更改了FXMLController.java
package com.alphabot.library.controller;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import com.alphabot.library.config.StageManager;
import com.alphabot.library.view.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
@Component
public class FXMLController implements Initializable {
@FXML
private StackPane mainPane;
protected Node home, authors, books, journals, issue_return;
@Lazy
@Autowired
private StageManager stageManager;
@Override
public void initialize(URL location, ResourceBundle resources) {
mainPane.getChildren().clear();
mainPane.getChildren().add(stageManager.switchScene(FxmlView.HOME));
home = stageManager.switchScene(FxmlView.HOME);
authors = stageManager.switchScene(FxmlView.AUTHORS);
books = stageManager.switchScene(FxmlView.BOOKS);
journals = stageManager.switchScene(FxmlView.JOURNALS);
issue_return = stageManager.switchScene(FxmlView.ISSUE_RETURN);
}
@FXML
private void homeOnAction(ActionEvent event) throws IOException {
mainPane.getChildren().clear();
mainPane.getChildren().add(home);
}
@FXML
private void authorsOnAction(ActionEvent event) throws IOException {
mainPane.getChildren().clear();
mainPane.getChildren().add(authors);
}
@FXML
private void booksOnAction(ActionEvent event) throws IOException {
mainPane.getChildren().clear();
mainPane.getChildren().add(books);
}
@FXML
private void journalsOnAction(ActionEvent event) throws IOException {
mainPane.getChildren().clear();
mainPane.getChildren().add(journals);
}
@FXML
private void issueReturnOnAction(ActionEvent event) throws IOException {
mainPane.getChildren().clear();
mainPane.getChildren().add(issue_return);
}
}