更改JavaFX MenuButton图标

时间:2015-04-11 01:00:56

标签: javafx

是否有一种简单的方法可以将JavaFX MenuButton箭头更改为字符或完全隐藏它?

MenuButton hamburgerMenu = new MenuButton("\u2630");
hamburgerMenu.getItems().addAll(new MenuItem("Ham"), new MenuItem("Buger"));

2 个答案:

答案 0 :(得分:2)

添加以下样式以隐藏箭头

.menu-button > .arrow-button > .arrow {
    -fx-background-color: transparent;
}

<强>结果

enter image description here

您还可以使用-fx-shape后跟SVG路径定义不同的SHAPE。

.menu-button > .arrow-button > .arrow {
    -fx-shape: "M 0 -3.5 v 7 l 4 -3.5 z";
}

输出

enter image description here

答案 1 :(得分:-1)

有几种方法可以做到这一点,这里有四种。代码是Jython和JavaFX。您可以编辑此代码以满足您的需求。

  

首先,enum,for context。

public enum URLBarArrowConstants {
     //URLBarArrow Constants
     BYCSS_AND_SHAPE,
     BYCSS_AND_NO_SHAPE,
     NOCSS_AND_SHAPE,
     NOCSS_AND_NO_SHAPE;
}
  

其次,css文件,用于上下文。

EG#1

/*ComboBox's Arrow is a Region.*/
.menu-button .arrow-button .arrow {
     -fx-shape: "...";
     -fx-scale-shape: true;
     -fx-position-shape: true;
}

EG#2

/*ComboBox's Arrow is a Region.*/
.menu-button .arrow-button .arrow {
    /*Setting either of these two will do.*/
     -fx-background-color: transparent; 
     -fx-opacity: 0.0;  
}

/*ComboBox's Arrow Button is a Stack Pane.*/
.menu-button .arrow-button{
    -fx-background-position: center;
    -fx-background-repeat: no-repeat;
    -fx-background-image: url("..<file>.png");
}
  

该方法,在我的主文件中。

def setCustomURLBarArrow(self, url_bar, scene, URLBarArrowConstant):
    from javafx.scene.paint import Paint
    from javafx.scene.shape import Shape, SVGPath, FillRule

不要通过CSS配置ComboBox箭头,而是以编程方式执行并更改区域SVG形状

if URLBarArrowConstant == URLBarArrowConstants.NOCSS_AND_SHAPE:

    #SVG Object
    previous_url_bar = SVGPath()

    #SVG Path
    previous_url_bar.setContent("...") # edit this 

    #SVG Fill Rule
    previous_url_bar.setFillRule(FillRule.NON_ZERO)

    #Set Fill -- 
    previous_url_bar.setFill(Paint.valueOf(Color.web("...").toString())) //edit here

    #Apply CSS Sheet
    url_bar.applyCss()

    #Set Region's Shape
    arrow_region = url_bar.lookup(".arrow").setShape(previous_url_bar)

通过CSS配置ComboBox箭头并更改区域SVG形状

elif URLBarArrowConstant == URLBarArrowConstants.BYCSS_AND_SHAPE:
    #Apply Stylesheet for URL Bar
    scene.getStylesheets().add(File("..<file>.css").toURI().toString()) //edit here

通过CSS配置ComboBox箭头,而只是通过设置透明度/不透明度值并设置背景来隐藏箭头。

elif URLBarArrowConstant == URLBarArrowConstants.BYCSS_AND_NO_SHAPE:
    #Apply Stylesheet for URL Bar
    scene.getStylesheets().add(File("..<file>.css").toURI().toString()) //edit here

不要通过CSS配置ComboBox箭头,而是以编程方式执行,只需通过设置透明度/不透明度值并设置背景来隐藏箭头。

elif URLBarArrowConstant == URLBarArrowConstants.NOCSS_AND_NO_SHAPE:

    from javafx.scene.paint import Paint
    from javafx.scene.layout import CornerRadii
    from javafx.scene.layout import Background, BackgroundSize, BackgroundImage, BackgroundPosition, BackgroundRepeat, BackgroundFill

    #Apply CSS Sheet
    url_bar.applyCss()

    #Grab Arrow(Region), ArrowButton(StackPane) ComboBox properties
    arrow_region = url_bar.lookup(".arrow")
    arrow_button = url_bar.lookup(".arrow-button")

    #Either Set Opacity to 0 or set background color to transparent.
    arrow_region.setOpacity(0.0)
    arrow_region.setBackground( Background( array(BackgroundFill, [BackgroundFill( Paint.valueOf(Color.TRANSPARENT.toString()), CornerRadii.EMPTY, Insets.EMPTY)]) ) )

    #Set a Background Image for the .arrow-button StackPane.
    arrow_button.setBackground(Background( array(BackgroundImage, [BackgroundImage( Image( String(File('..<file>.png').toURI().toString()), True) , BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, BackgroundSize.DEFAULT)] ) ) )       //if you want, edit this
相关问题