样式的 JavaFX 图像路径

时间:2021-05-19 00:32:13

标签: java javafx

我目前正在尝试使用 JavaFX 制作自定义按钮。我定义了 2 种样式,其中包含 2 个按钮状态(按下和自由)的 2 个不同图像。我正在使用 Intellij Idea 并且在定义路径时它没有显示错误,但是按钮没有显示。它只是透明的,但我可以点击它。我尝试了指定的不同路径,但没有得到任何结果。这是我定义样式和文件树的代码。谢谢!

public class CustomButton extends Button {
    private final String FONT_PATH = "src/Resources/GUI/pixelFont.ttf";

    private final String BUTTON_PRESSED_STYLE = "-fx-background-color: transparent;" +
            " -fx-background-image: url('../../Resources/GUI/Buttondown.png');";
    private final String BUTTON_FREE_STYLE = "-fx-background-color: transparent;" +
            " -fx-background-image: url('../../Resources/GUI/Buttonup.png');";

    public CustomButton(String text) {
        setFont();
        setPrefWidth(180);
        setPrefHeight(53);
        setText(text);
        setStyle(BUTTON_PRESSED_STYLE);
        initButtonListeners();
    }
}

File tree

1 个答案:

答案 0 :(得分:0)

将所有文件移动到由 Intellij Idea 预先生成的资源文件夹中,然后使用该文件夹解决了问题。