在我的应用程序中,我使用a MenuButton来提供操作的下拉列表.a上的默认下拉指示器MenuButton是指向下方的黑色三角形.我想将其更改为指向下方的白色三角形以匹配文本的颜色.
为了防止我不清楚,这里有一个屏幕截图,应该清除它.

我试过在fxml文件中放置一个图形,如下所示:
<MenuButton contentDisplay="RIGHT" graphicTextGap="10.0" layoutX="92.0" layoutY="73.0" mnemonicParsing="false" styleClass="toolbar-button" text="MenuButton">
<graphic>
<ImageView fitHeight="4.0" fitWidth="7.0" mouseTransparent="true" preserveRatio="true">
<image>
<Image url="@Arrow_Down.png" preserveRatio="true" smooth="false" />
</image>
</ImageView>
</graphic>
<items>
<MenuItem mnemonicParsing="false" text="Action 1" />
<MenuItem mnemonicParsing="false" text="Action 2" />
</items>
</MenuButton>
Run Code Online (Sandbox Code Playgroud)
但这给了我一个黑色和白色的三角形:
如果我可以以某种方式隐藏可行的黑色三角形,但看起来确实应该有一种方式来设置菜单按钮,使其变为白色.
对于那些想要帮助的人来说,这是Sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml">
<children>
<MenuButton contentDisplay="RIGHT" graphicTextGap="10.0" layoutX="92.0" layoutY="73.0" mnemonicParsing="false" styleClass="toolbar-button" text="MenuButton">
<graphic>
<ImageView fitHeight="4.0" fitWidth="7.0" mouseTransparent="true" preserveRatio="true">
<image>
<Image url="@Arrow_Down.png" preserveRatio="true" smooth="false" />
</image>
</ImageView>
</graphic>
<items>
<MenuItem mnemonicParsing="false" text="Action 1" />
<MenuItem mnemonicParsing="false" text="Action 2" />
</items>
</MenuButton>
</children>
<stylesheets>
<URL value="@test.css" />
</stylesheets>
</AnchorPane>
Run Code Online (Sandbox Code Playgroud)
test.css:
root {
display: block;
}
.toolbar-button {
-fx-background-color: #006699;
-fx-padding: 2 4 4 4;
-fx-text-base-color: #FFFFFF;
-fx-font-weight: bold;
-fx-font-size: 12px;
}
.toolbar-button:hover {
-fx-background-color: #B2E1FF;
-fx-padding: 2 4 4 4;
-fx-text-base-color: #000000;
-fx-font-weight: bold;
-fx-font-size: 12px;
}
Run Code Online (Sandbox Code Playgroud)
并运行Test.java:
package test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Test extends Application {
public static void main(String[] args) {
Application.launch(Test.class, args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
stage.setScene(new Scene(root));
stage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
那么如何将黑色三角形变成白色三角形呢?
经过一番挖掘,我找到了答案.结果是MenuButton没有使用图像作为按钮,而是使用-fx-shapecss属性来执行此操作.在caspian.css文件中,它应用于.menu-button .arrow和.menu-button:openvertically .arrow部分.由于我已经应用了toolbar-button我的MenuButton,我只是将以下内容添加到我的css文件中:
.toolbar-button .arrow {
-fx-background-insets: 1 0 -1 0, 0;
-fx-background-color: -fx-mark-color, #FFFFFF;
-fx-padding: 0.25em; /* 3 */
-fx-shape: "M 0 -3.5 v 7 l 4 -3.5 z";
}
.toolbar-button:hover .arrow {
-fx-background-insets: 1 0 -1 0, 0;
-fx-background-color: -fx-mark-highlight-color, -fx-mark-color;
-fx-padding: 0.25em; /* 3 */
-fx-shape: "M 0 -3.5 v 7 l 4 -3.5 z";
}
.toolbar-button:openvertically .arrow {
-fx-padding: 0.166667em 0.333333em 0.166667em 0.333333em; /* 2 4 2 4 */
-fx-shape: "M 0 0 h 7 l -3.5 4 z";
}
Run Code Online (Sandbox Code Playgroud)
甚至允许我在悬停时将箭头的颜色更改为黑色.