4

在我的应用程序中,我使用 aMenuButton来提供操作的下拉列表。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>

但这给了我一个黑白三角形:

白色和黑色三角形

如果我能以某种方式隐藏可以工作的黑色三角形,但似乎应该有一种方法可以将菜单按钮设置为白色而不是白色。

这里是 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>

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;
}

以及运行它的 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();
    }
}

那么如何让黑色三角形变成白色三角形呢?

4

1 回答 1

8

经过一番挖掘,我找到了答案。原来 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";
}

这甚至允许我在悬停时将箭头的颜色改回黑色。

于 2012-06-20T16:58:02.350 回答