【发布时间】:2021-12-10 08:08:38
【问题描述】:
我正在创建一个 JavaFX 应用程序。我开始使用本地 SDK 开发它,现在我尝试使用 Gradle。我按如下方式创建了 build.gradle:
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.10'
}
application {
mainClassName='Main.GUI.Main'
}
dependencies {
implementation 'org.controlsfx:controlsfx:11.1.0'
implementation 'org.apache.derby:derby:10.15.2.0'
}
javafx {
version = "17.0.1"
modules = [ 'javafx.controls', 'javafx.fxml']
}
repositories {
mavenCentral()
}
这是我的代码的一部分,它会抛出 javafx.collections.ObservableList 错误:
package GUI;
import Main.Archive;
import Main.Document;
import Main.Node;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import java.net.URL;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
public class MainController implements Initializable
{
@FXML private TreeView<Node> tagTree;
@FXML private TableView<Document> fileTable;
@FXML private TableColumn<Document, String> name;
@FXML private TableColumn<Document, Date> date;
@FXML private TableColumn<Document, Integer> size;
@FXML private TableColumn<Document, Path> path;
private static Archive archive;
private static Main mainApp;
public void setMainApp(Main mainApp){
this.mainApp = mainApp;
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
if(tagTree!=null)
setTagTree(archive.getTagTree());
}
public void setTagTree(Node tags){
TreeItem rootItem = new TreeItem("Tags");
if(tags.getChildren()!=null)
for (Node n:tags.getChildren())
addNodes(rootItem, n);
tagTree.setRoot(rootItem);
}
// Recursively creates a TreeItem for each tag and adds it to its root
void addNodes(TreeItem<String> rootItem, Node tags){
List<Node> children = tags.getChildren();
// If it's the last item just add it
if(children.isEmpty())
rootItem.getChildren().add(new TreeItem<>(tags.getData().getName())); // HERE
else{
// Otherwise, add each child to the root
TreeItem<String> son = new TreeItem(tags.getData().getName());
for (Node n : children)
addNodes(son, n);
rootItem.getChildren().add(son); // ALSO HERE
}
}
public void setArchive(Archive archive){
MainController.archive = archive;
}
@FXML
private void logout(){
archive.logout();
System.exit(0);
}
@FXML
private void addDocument(){
mainApp.showAddDocument();
}
@FXML
private void addTag(){
}
}
javafx.util.Callback 和 javafx.event.EventTarget 也有类似的问题。
【问题讨论】:
-
你得到的确切错误是什么?
-
我也不太了解 gradle,通常认为 Maven 对大多数事情来说是不太必要的邪恶,其他人不同意,这只是一个无关紧要的意见。但是看看openjfx.io gradle JavaFX hello world。它看起来不像你的项目。让 hello world 示例在您的环境中工作,然后将项目的一部分一点一点地集成到工作示例中,检查每个小添加的一切是否正常,直到一切正常。
-
我正在使用 org.openjfx.javafxplugin。确切的错误是 java: cannot access javafx.util.Callback class file for javafx.util.Callback not found.
-
您原来的问题的一个问题是您指定的 JavaFX 版本
17.0.1不存在。应该是17.0.0.1。在我看来,降级到版本11.0.2并不是一个好的解决方案。 -
如果您有答案,请不要编辑问题并将答案放在那里。相反,创建一个答案并将答案放在那里。如果它是正确的答案,一旦你有足够的声望,你就可以在一段时间后mark it as such。