【问题标题】:How to set javafx button Icon using shape?如何使用形状设置 javafx 按钮图标?
【发布时间】:2014-05-08 22:13:12
【问题描述】:

我想使用一些几何形状来设置我的按钮图标,例如圆形、方形等。

我还想在 FXML 中设置它,例如:

<Button .....>
  <graphics>
    <group>
      <shape>
      ......
      </shape>
    </group>
  </graphics>
</Button>

有可能吗?我确实知道如何将图像图标加载到 ImageView 然后 setGraphic。

TIA

【问题讨论】:

    标签: button javafx icons shape


    【解决方案1】:

    按钮中的graphic 可以是任何节点。 Shape 例如 Circle 是一个节点。

    所以只需将按钮图形设置为形状即可。您甚至可以将许多形状放入容器(例如 VBox 或 StackPane)中,并将按钮的图形设置为容器,以便图形由许多形状组成。

    这是一个示例 FXML 文件,您可以在 SceneBuilder 2 中复制和粘贴并加载。

    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.shape.*?>
    
    <VBox spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
      <children>
        <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Panic">
          <graphic>
            <Circle fill="RED" radius="8.0" stroke="BLACK"/>
          </graphic>
        </Button>
        <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="All OK">
          <graphic>
            <StackPane>
              <children>
                <Rectangle arcHeight="5.0" arcWidth="5.0" fill="LEMONCHIFFON" height="15.0" stroke="BLACK"
                           strokeType="INSIDE" width="15.0"/>
                <SVGPath
                    content="M 1.3657704,4.938667 C 1.1462945,5.1582921 1.1462945,5.5133282 1.3657704,5.7329533 L 3.7236648,8.0924507 C 3.9431407,8.3120765 4.2979355,8.3120765 4.5174113,8.0924507 L 9.5366913,3.0697579 C 9.7561672,2.8501328 9.7561674,2.4950969 9.5366913,2.2754716 L 8.6962538,1.4344625 C 8.4767779,1.2148374 8.121983,1.2148374 7.9025071,1.4344625 L 4.6107933,4.7284147 L 3.4902099,3.6070693 C 3.270734,3.3874441 2.9159392,3.387444 2.6964632,3.6070693 L 1.3657704,4.938667 z"
                    fill="GREEN"/>
              </children>
            </StackPane>
          </graphic>
        </Button>
      </children>
      <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
      </padding>
    </VBox>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 2022-07-14
      • 1970-01-01
      • 2019-02-07
      • 2017-03-19
      • 2018-09-03
      相关资源
      最近更新 更多