首页 > 解决方案 > 列表中带有奇怪边框的JavaFx

问题描述

我有一个使用 flowless 项目的 JavaFX 列表。但是我得到了一个奇怪的边框(我没有启用任何边框并强制边框具有宽度= 0px),它存在并且具有渐变效果:

带有渐变边框的列表

当我将单元格背景插入设置为-1时,边框消失了,所以我认为这个问题与背景有关。虽然这不是一个解决方案,因为如果我启用边框(我想要),渐变效果仍然存在。

有谁知道我怎样才能摆脱这个边界?

我使用带有最新 201 版本的 JavaFX 8。

编辑:我做了以下示例显示问题:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
import org.fxmisc.flowless.Cell;
import org.fxmisc.flowless.VirtualFlow;
import org.fxmisc.flowless.VirtualizedScrollPane;

public class Main extends Application {

  private ObservableList<Line> logLines;

  private VirtualFlow<Line, Cell<Line, LineCell>> listView;

  private VirtualizedScrollPane<VirtualFlow> listScrollPane;

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

  public void start(Stage primaryStage) {

    logLines = FXCollections.observableArrayList();
    for(int i=1; i < 50; i++) {
      logLines.add(new Line("Line "+i));
    }

    listView = VirtualFlow.createVertical(logLines, (line) -> Cell.wrapNode(new LineCell(line)));
    listScrollPane = new VirtualizedScrollPane<>(listView);

    Scene scene = new Scene(listScrollPane, 200, 600, Color.BLACK);

    primaryStage.setScene(scene);
    primaryStage.show();
  }


  private class Line {

    private String text;

    public Line(String text) {
      this.text = text;
    }

    public String getText() {
      return text;
    }
  }

  private class LineCell extends TextFlow {
    public LineCell(Line line) {
      super();
      Text t = new Text(line.getText());
      t.setFill(Color.BLACK);
      super.setStyle("-fx-background-color: green;");
      super.getChildren().add(t);
    }
  }
}

谢谢!

标签: javajavafxjava-8

解决方案


我找到了解决这个问题的方法。LineCell 不是扩展 TextFlow,而是扩展 StackPane,我将 TextFlow 添加到 Stackpane。

  private class LineCell extends StackPane {
    public LineCell(Line line) {
      super();
      Text t = new Text(line.getText());
      t.setFill(Color.BLACK);

      TextFlow tf = new TextFlow(t);
      super.setStyle("-fx-background-color: green;");
      super.getChildren().add(tf);
    }
  }

推荐阅读