【问题标题】:Using Font Awesome 5 SVGs as JavaFX Button shapes使用 Font Awesome 5 SVG 作为 JavaFX 按钮形状
【发布时间】:2019-02-07 17:52:51
【问题描述】:

我目前正在开发未修饰的 JavaFX 应用程序,并尝试添加关闭、最大化和最小化按钮并将它们的形状设置为 Font Awesome 5 SVG。

但某些 SVG 似乎无法正确显示,例如 "window-minimize" 显示为只是一个块。 (见图)

用于按钮的 FXML

<Button fx:id="min" mnemonicParsing="false">
 <HBox.margin>
  <Insets right="2.5" />
 </HBox.margin>
</Button>
<Button fx:id="max" mnemonicParsing="false">
 <HBox.margin>
  <Insets left="2.5" right="2.5" />
 </HBox.margin></Button>
<Button fx:id="exit" mnemonicParsing="false">
 <HBox.margin>
  <Insets left="2.5" />
 </HBox.margin>
</Button>

CSS 应用

#toolbar .button {
    -fx-background-color: #ecf0f1;

    -size: 16px;

    -fx-min-width: -size;
    -fx-min-height: -size;
    -fx-pref-width: -size;
    -fx-pref-height: -size;
}

#exit {
    -fx-shape: "M193.94 256L296.5 153.44l21.15-21.15c3.12-3.12 3.12-8.19 0-11.31l-22.63-22.63c-3.12-3.12-8.19-3.12-11.31 0L160 222.06 36.29 98.34c-3.12-3.12-8.19-3.12-11.31 0L2.34 120.97c-3.12 3.12-3.12 8.19 0 11.31L126.06 256 2.34 379.71c-3.12 3.12-3.12 8.19 0 11.31l22.63 22.63c3.12 3.12 8.19 3.12 11.31 0L160 289.94 262.56 392.5l21.15 21.15c3.12 3.12 8.19 3.12 11.31 0l22.63-22.63c3.12-3.12 3.12-8.19 0-11.31L193.94 256z";
}

#max {
    -fx-shape: "M464 32H48C21.5 32 0 53.5 0 80v352c0 26.5 21.5 48 48 48h416c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48zm0 394c0 3.3-2.7 6-6 6H54c-3.3 0-6-2.7-6-6V192h416v234z";
}

#min {
    -fx-shape: "M480 480H32c-17.7 0-32-14.3-32-32s14.3-32 32-32h448c17.7 0 32 14.3 32 32s-14.3 32-32 32z";
}

【问题讨论】:

    标签: java svg javafx font-awesome font-awesome-5


    【解决方案1】:

    默认情况下,形状会被缩放以适应它所应用的Region 的区域。这种缩放导致最小化按钮的形状几乎呈二次方外观。使用未缩放的形状也不是一种选择,因为形状太大了。

    使用-fx-background-insets 创建正确的比率。此外,我建议在Button 内放置一个Region 作为图形并将形状应用于此节点。这允许您为按钮添加背景或增加 hitbox:

    <Button fx:id="min" mnemonicParsing="false" pickOnBounds="true">
     <HBox.margin>
      <Insets right="2.5" />
     </HBox.margin>
     <graphic>
       <Region styleClass="shape"/>
     </graphic>
    </Button>
    <Button fx:id="max" mnemonicParsing="false" pickOnBounds="true">
     <HBox.margin>
      <Insets left="2.5" right="2.5" />
     </HBox.margin>
     <graphic>
       <Region styleClass="shape"/>
     </graphic>
    </Button>
    <Button fx:id="exit" mnemonicParsing="false" pickOnBounds="true">
     <HBox.margin>
      <Insets left="2.5" />
     </HBox.margin>
     <graphic>
       <Region styleClass="shape"/>
     </graphic>
    </Button>
    
    #toolbar .button {
        -size: 20px;
        -fx-padding: 2px;
        -fx-background-color: null;
    
        -fx-min-width: -size;
        -fx-min-height: -size;
        -fx-pref-width: -size;
        -fx-pref-height: -size;
    }
    
    #toolbar .button .shape {
        -fx-background-color: #ecf0f1;
    }
    
    #toolbar .button:pressed .shape {
        /* different background for pressed button */
        -fx-background-color: lightgreen;
    }
    
    #exit .shape {
        -fx-shape: "M193.94 256L296.5 153.44l21.15-21.15c3.12-3.12 3.12-8.19 0-11.31l-22.63-22.63c-3.12-3.12-8.19-3.12-11.31 0L160 222.06 36.29 98.34c-3.12-3.12-8.19-3.12-11.31 0L2.34 120.97c-3.12 3.12-3.12 8.19 0 11.31L126.06 256 2.34 379.71c-3.12 3.12-3.12 8.19 0 11.31l22.63 22.63c3.12 3.12 8.19 3.12 11.31 0L160 289.94 262.56 392.5l21.15 21.15c3.12 3.12 8.19 3.12 11.31 0l22.63-22.63c3.12-3.12 3.12-8.19 0-11.31L193.94 256z";
    }
    
    #max .shape {
        -fx-shape: "M464 32H48C21.5 32 0 53.5 0 80v352c0 26.5 21.5 48 48 48h416c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48zm0 394c0 3.3-2.7 6-6 6H54c-3.3 0-6-2.7-6-6V192h416v234z";
    }
    
    #min .shape {
        -fx-background-insets: 6px 0 6px 0; /* use proper area for shape */
        -fx-shape: "M480 480H32c-17.7 0-32-14.3-32-32s14.3-32 32-32h448c17.7 0 32 14.3 32 32s-14.3 32-32 32z";
    }
    

    【讨论】:

      猜你喜欢
      • 2013-09-02
      • 2016-07-25
      • 2018-07-23
      • 1970-01-01
      • 2019-02-26
      • 2020-03-21
      相关资源
      最近更新 更多