(JavaFX FXML) 在尝试制作自定义按钮时,如何让图像完全填充按钮?
(JavaFX FXML) While trying to make a custom button, how do I get an image to completely fill a button?
(JavaFX FXML) 我目前正在使用场景生成器制作应用程序,我正在尝试制作自定义按钮,我设法将图像放到按钮上,但我不知道如何获取图像完全填充按钮。有任何想法吗?哦,如果有另一种创建自定义按钮的方法也很有用!
这是我的 FXML 代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button layoutX="165.0" layoutY="125.0" mnemonicParsing="false" prefHeight="150.0" prefWidth="270.0">
<graphic>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@case.png" />
</image>
</ImageView>
</graphic>
</Button>
</children>
</AnchorPane>
图像适合按钮,但我希望它完全填满按钮,或者如果有任何不同的方式,将不胜感激!!!
提前致谢!
PS:我正在使用 Scene Builder,任何有关该应用程序的有用提示也会非常有帮助
如果我没理解错的话,您根本不想看到按钮边框和颜色。
您可以通过 CSS:
自定义您的按钮
.fill-btn {
-fx-graphic: url("@case.png");
-fx-background-color: inherit;
-fx-opacity: inherit;
-fx-border: none;
}
并在您的 fxml 中声明它:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button stylesheets="@buttons-style.css" styleClass="fill-btn" layoutX="165.0" layoutY="125.0" mnemonicParsing="false" prefHeight="150.0" prefWidth="270.0">
</Button>
</children>
(JavaFX FXML) 我目前正在使用场景生成器制作应用程序,我正在尝试制作自定义按钮,我设法将图像放到按钮上,但我不知道如何获取图像完全填充按钮。有任何想法吗?哦,如果有另一种创建自定义按钮的方法也很有用!
这是我的 FXML 代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button layoutX="165.0" layoutY="125.0" mnemonicParsing="false" prefHeight="150.0" prefWidth="270.0">
<graphic>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@case.png" />
</image>
</ImageView>
</graphic>
</Button>
</children>
</AnchorPane>
图像适合按钮,但我希望它完全填满按钮,或者如果有任何不同的方式,将不胜感激!!!
提前致谢!
PS:我正在使用 Scene Builder,任何有关该应用程序的有用提示也会非常有帮助
如果我没理解错的话,您根本不想看到按钮边框和颜色。 您可以通过 CSS:
自定义您的按钮.fill-btn {
-fx-graphic: url("@case.png");
-fx-background-color: inherit;
-fx-opacity: inherit;
-fx-border: none;
}
并在您的 fxml 中声明它:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button stylesheets="@buttons-style.css" styleClass="fill-btn" layoutX="165.0" layoutY="125.0" mnemonicParsing="false" prefHeight="150.0" prefWidth="270.0">
</Button>
</children>