我想构建一个库管理系统,我希望将该书添加到我的数据库中,该书工作正常。但是当我想从数据库中检索数据时,我的加载器类无法加载我的fxml文件 这是我项目的结构 enter image description here
这是我的代码 Book_listController.java
package library.listbook;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import library.database.DatabaseHandler;
public class Book_listController implements Initializable {
ObservableList<Book> list = FXCollections.observableArrayList();
@FXML
private TableView<Book> tableview;
@FXML
private TableColumn<Book, String> titlecol;
@FXML
private TableColumn<Book, String> idcol;
@FXML
private TableColumn<Book, String> availabilitycol;
@FXML
private TableColumn<Book, String> authorcol;
@FXML
private TableColumn<Book, String> publishercol;
@Override
public void initialize(URL url, ResourceBundle rb) {
iniCol();
try {
loadData();
} catch (SQLException ex) {
Logger.getLogger(Book_listController.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void iniCol(){
titlecol.setCellValueFactory(new PropertyValueFactory<>("title"));
idcol.setCellValueFactory(new PropertyValueFactory<>("id"));
authorcol.setCellValueFactory(new PropertyValueFactory<>("author"));
publishercol.setCellValueFactory(new PropertyValueFactory<>("publisher"));
availabilitycol.setCellValueFactory(new PropertyValueFactory<>("available"));
}
private void loadData() throws SQLException{
DatabaseHandler handler=new DatabaseHandler();
String qu="select * from book";
ResultSet resultset=handler.exeQuery(qu);
try{
while(resultset.next()){
String id=resultset.getString("id");
String title=resultset.getString("title");
String author=resultset.getString("author");
String publisher=resultset.getString("publisher");
String avail=resultset.getString("isAvail");
list.add(new Book(id,title,author,publisher,avail));
}
}catch(SQLException ex){
Logger.getLogger(Book_listController.class.getName()).log(Level.SEVERE, null, ex);
}
tableview.getItems().setAll(list);
}
public static class Book{
private final SimpleStringProperty title;
private final SimpleStringProperty id;
private final SimpleStringProperty author;
private final SimpleStringProperty publisher;
private final SimpleStringProperty available;
Book(String title,String id,String author,String pub,String avail){
this.title=new SimpleStringProperty(title);
this.id=new SimpleStringProperty(id);
this.author=new SimpleStringProperty(author);
this.publisher=new SimpleStringProperty(pub);
this.available=new SimpleStringProperty(avail);
}
public String getTitle() {
return title.get();
}
public String getId() {
return id.get();
}
public String getAuthor() {
return author.get();
}
public String getPublisher() {
return publisher.get();
}
public String getAvailable() {
return available.get();
}
}
}
Book_listloader.java `
package library.listbook;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Dell
*/
public class Book_listloader extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("book_list.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
这是我的fxml文件 的 book_list.fxml `
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="library.listbook.Book_listController">
<stylesheets>
<URL value="@book_list.css" />
</stylesheets>
<children>
<TableView fx:id="tableview" layoutX="37.0" layoutY="14.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="titlecol" prefWidth="125.0" text="Book Title" />
<TableColumn fx:id="idcol" minWidth="2.0" prefWidth="111.0" text="Book id" />
<TableColumn fx:id="authorcol" prefWidth="110.0" text="Author" />
<TableColumn fx:id="publishercol" prefWidth="141.0" text="Publisher" />
<TableColumn fx:id="availabilitycol" prefWidth="112.0" text="Availability" />
</columns>
</TableView>
</children>
</AnchorPane>
`
答案 0 :(得分:0)
您的文件book_list.fxml
位于文件夹library.listbook
内,但当您致电getClass().getResource
时,它会在资源文件夹中找到资源。让我们尝试修改文件的路径。例如:library.listbook\book_list.fxml