gpt4 book ai didi

css - 如何在 JavaFX 中更改 TreeItem 的下划线颜色?

转载 作者:行者123 更新时间:2023-11-28 10:28:41 25 4
gpt4 key购买 nike

I have a JavaFX TreeView which has TreeItems with two basic colors , red and white . When they are selected they both have -fx-underline:true but i have a problem with the line being Red or White i want the underline to be always white , is that possible with css in JavaFX ?

enter image description here

我已经尝试了下面的方法,但它不起作用。

.tree-cell-white:selected {
-fx-font-weight: bold ;
-fx-text-fill:white;
-fx-underline: true;
-fx-underline-fill: orange;
-fx-underline-color: blue;
}

.tree-cell-red:selected {
-fx-font-weight: bold ;
-fx-text-fill:firebrick;
-fx-underline: true;
}

最佳答案

不幸的是,我找不到如何通过 JavaFX CSS 来实现它,但我找到了另一种解决方案。那就是我创建了 MyTrack 派生自 HBox 的类,并添加了一个 ImageView 作为音乐/mp3 图标和 Label与文字。这是一个小技巧,我通过设置标签的底部边框颜色和将 MyTrack 设置为 TreeItem 的图形来为标签添加下划线。

package underlined.treeview;

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;

import java.util.Arrays;
import java.util.List;

public class TreeViewApp extends Application {

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

private Parent createContent() {
TreeItem<String> treeRoot = new TreeItem<>("Root node");
List<TreeItem<String>> tracks = Arrays.asList(
new TreeItem<>(""),
new TreeItem<>(""),
new TreeItem<>(""));

for (TreeItem<String> track : tracks)
track.setGraphic(new MyTrack("Track"));

treeRoot.getChildren().addAll(tracks);

TreeView treeView = new TreeView();
treeView.setShowRoot(true);
treeView.setRoot(treeRoot);
treeRoot.setExpanded(true);
return treeView;
}

@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(createContent());
primaryStage.setScene(scene);
primaryStage.show();
}
}

MyTrack类:

package underlined.treeview;

import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

public class MyTrack extends HBox {

public MyTrack(String text) {

Label label = new Label(text);
label.setStyle("-fx-border-color: aqua;" +
"-fx-border-width: 0 0 1 0;");

Image image = new Image(getClass().getResource("/img/icon/mp3.png")
.toExternalForm());
ImageView imageView = new ImageView(image);
imageView.setFitWidth(16);
imageView.setFitHeight(16);

getChildren().addAll(imageView, label);
}
}

瞧!!!

在我的示例中,底部边框颜色为浅绿色。请注意,TreeItem 应使用空 String 进行实例化。这只是您需要自己优化的示例。

关于css - 如何在 JavaFX 中更改 TreeItem 的下划线颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47463877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com