在我的應用程序中,我使用了一個MenuButton
來提供操作的下拉列表。 MenuButton
上的默認下拉指示符是朝下的黑色三角形。我想將它改爲一個白色的三角形,以便與我的文字顏色相匹配。更改JavaFX 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>
但是這給了我兩個黑色和白色的三角:
如果我可以以某種方式隱藏可以工作的黑色三角形,但它確實似乎應該有一種方法來爲我設計菜單按鈕t代替白色。
這裏是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();
}
}
那麼如何讓黑色三角形變成白色三角形呢?