简体   繁体   English

JavaFX 缺少 Legend 类

[英]JavaFX missing Legend class

I have this code:我有这个代码:

List<Node> legends = new ArrayList<>(lineChart.lookupAll("Label.chart-legend-item"));
Legend legend1 = (Legend)legends.get(0);

The problem is that my IDE can't find any Legend class to import it.问题是我的 IDE 找不到任何Legend类来导入它。

I'm talking about this class: https://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#legend我在谈论这个类: https : //docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html#legend

I'm using Java 10. Why my IDE don't find Legend class?我使用的是 Java 10。为什么我的 IDE 找不到Legend类?

What I want to achieve is to take the name of legend.我要实现的,就是取传奇之名。

It looks like Legend is from an internal api: 看起来Legend是来自内部api的:

import com.sun.javafx.charts.Legend;

Probably removed in Java9+ 可能已在Java9 +中删除

You have export the module com.sun.javafx.charts to access the Legend class as the Legend class is part of the internal API. 您已导出模块com.sun.javafx.charts来访问Legend类,因为Legend类是内部API的一部分。 In IntelliJ you have to add an override to the compiler: 在IntelliJ中,您必须向编译器添加替代:

(Settings -->Build, Execution, Deployment --> Compiler --> Java compiler -->Override compiler parameters) (设置->构建,执行,部署->编译器-> Java编译器->覆盖编译器参数)

--add-exports=javafx.controls/com.sun.javafx.charts=ALL-UNNAMED

And in the run configuration add to the VM Options. 并在运行配置中添加到VM Options。

The import is: 导入为:

import com.sun.javafx.charts.Legend;

Edit: It has not been removed as the accepted answer says. 编辑:尚未删除,如接受的答案所说。

Example code: 示例代码:

import com.sun.javafx.charts.Legend;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Legend legend = new Legend();
        Legend.LegendItem legendItem = new Legend.LegendItem("Test legend item");
        ObservableList<Legend.LegendItem> items = FXCollections.observableArrayList(legendItem);
        legend.setItems(items);
        AnchorPane anchorPane = new AnchorPane(legend);
        Scene scene = new Scene(anchorPane, 400, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

And to compiler options add: 并向编译器选项添加:

IntelliJ

If you use gradle you have to add the compiler option to the build.gradle too. 如果使用gradle,则还必须将编译器选项添加到build.gradle中。

compileJava {
    options.compilerArgs += ["--add-exports=javafx.controls/com.sun.javafx.charts=ALL-UNNAMED"]
}

TilePane is super class of Legend. TilePane 是 Legend 的超类。 So you can cast to TilePane.所以你可以投射到 TilePane。

List<Node> legends = new ArrayList<>(lineChart.lookupAll("Label.chart-legend-item"));
TilePane legend1 = (TilePane) legends.get(0);    
legend1.setHgap(10);
legend1.getChildren().remove(..);
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM