Javafx 中没有颜色方块的 ColorPicker?

ColorPicker without color square in Javafx?

我想实现我自己的 SplitMenu ColorPicker,它的实际功能类似于 SplitMenuButton。为此,我有一个按钮,单击它会将所选文本的颜色更改为当前颜色,还有一个颜色选择器,它的作用相同,但也会更改当前颜色。所以我的 FXML 看起来像这样:

<HBox>
    <Button text="Aa" fx:id="colorTextButton" styleClass="leftButton" onAction="#colorText"/>
    <ColorPicker fx:id="colorPicker" styleClass="rightButton colorPicker" onAction="#setColor">
</HBox>

我的 CSS 看起来像这样:

.leftButton {
    -fx-background-insets: 0;
    -fx-background-radius: 0.25em 0 0 0.25em;
    -fx-border-style: solid;
    -fx-border-color: #bcbcbc;
    -fx-border-width: 0.08em 0.00em 0.08em 0.08em;
    -fx-border-radius: 0.25em 0 0 0.25em;
}

.rightButton {
    -fx-background-insets: 0;
    -fx-background-radius: 0 0.25em 0.25em 0;
    -fx-border-style: solid;
    -fx-border-color: #bcbcbc;
    -fx-border-width: 0.08em 0.08em 0.08em 0.04em;
    -fx-border-radius: 0 0.25em 0.25em 0;
}

.colorPicker {
    -fx-color-label-visible: false;
}

有什么方法可以通过 java 编程或在 CSS 或 FXML 中去除颜色选择器中的颜色方块?在我的另一个按钮上重新创建一个与图形相似的正方形相当容易,但我如何摆脱颜色选择器上的那个?

您可以使用下面的 CSS 从 ColorPicker 中删除预览颜色框:

.color-picker > .color-picker-label > .picker-color > .picker-color-rect {
    -fx-stroke: null;
    -fx-fill : null;   
}

这将导致框完全消失。

编辑:但正如我所看到的,如果您打开调色板并在按下鼠标 (left/right) 的情况下不断改变颜色,那么该框将再次出现,当松开鼠标时它将再次消失。晚点或者明天再看看。

编辑 2:另一种比上述更好的方法是使用以下代码以编程方式隐藏矩形:

Rectangle rec = (Rectangle) colorPicker.lookup("Rectangle");
rec.setVisible(false);