繁体   English   中英

JavaFx以编程方式设置外部背景图像

[英]JavaFx set external background image programmatically

我无法动态设置锚定窗格的背景图像。 出于开发目的,我将外部资源放在项目的dist文件夹中,因此可以使用这些文件。

AnchorPane的代码:

AnchorPane root = new AnchorPane();
// this works fine, styles inside the css are used in components on the root pane
root.getStylesheets().add("file:/D:/ProjectFolder/global.css");

我不知道的是如何设置背景图像的路径。 仔细阅读CSS和JavaFX参考,应相对于css文件给出路径。 我还尝试创建URL和URI以及绝对路径。

// path of compiled jar
"D:/ProjectFolder/project.jar"
// path of css file
"D:/ProjectFolder/global.css"
// path of background file
"D:/ProjectFolder/Resources/Wallpapers/worldmap.jpg"

所有这些都不起作用:

root.setStyle("-fx-background-image: url('Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
root.setStyle("-fx-background-image: url('/Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
root.setStyle("-fx-background-image: url('./Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
root.setStyle("-fx-background-image: url('@Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
root.setStyle("-fx-background-image: url('@/Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
root.setStyle("-fx-background-image: url('@./Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
root.setStyle("-fx-background-image: url('D:/ProjectFolder/Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
root.setStyle("-fx-background-image: url('file:/D:/ProjectFolder/Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");

(在旁注中,文件存在,我检查了文件夹和文件的名称以防万一,这也是正确的)

出于测试目的:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class BackGroundTest extends Application {

    private int backgroundtest = 0;
    private AnchorPane root;

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Test Background");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                switch (backgroundtest) {
                    case 0: {
                        root.setStyle("-fx-background-image: url('Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
                        break;
                    }
                    case 1: {
                        root.setStyle("-fx-background-image: url('/Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
                        break;
                    }
                    case 2: {
                        root.setStyle("-fx-background-image: url('./Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
                        break;
                    }
                    case 3: {
                        root.setStyle("-fx-background-image: url('@Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
                        break;
                    }
                    case 4: {
                        root.setStyle("-fx-background-image: url('@/Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
                        break;
                    }
                    case 5: {
                        root.setStyle("-fx-background-image: url('@./Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
                        break;
                    }
                    case 6: {
                        root.setStyle("-fx-background-image: url('D:/ProjectFolder/Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
                        break;
                    }
                    case 7: {
                        root.setStyle("-fx-background-image: url('file:/D:/ProjectFolder/Resources/Wallpapers/worldmap.jpg'); -fx-background-repeat: stretch; -fx-background-size: stretch; -fx-background-position: center center;");
                        break;
                    }
                }
                backgroundtest++;
                if (backgroundtest == 8) {backgroundtest = 0;}
            }
        });
        root = new AnchorPane();
        root.getStylesheets().add("file:/D:/ProjectFolder/global.css");
        StackPane stack = new StackPane();
        root.getChildren().add(stack);
        AnchorPane.setBottomAnchor(stack, 0d);
        AnchorPane.setLeftAnchor(stack, 0d);
        AnchorPane.setRightAnchor(stack, 0d);
        AnchorPane.setTopAnchor(stack, 0d);
        stack.getChildren().add(btn);

        Scene scene = new Scene(root, 800, 600);

        primaryStage.setTitle("Test Background");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

css文件:

.button {
    -fx-background-color:
        rgb(0, 50, 0, 0.08),
        rgb(0, 0, 0, 0.8),
        #090a0c,
        linear-gradient(#4a5661 0%, #1f2429 20%, #1f242a 80%),
        linear-gradient(#242a2e, #23282e),
        radial-gradient(center 50% 0%, radius 100%, rgba(135,142,148,0.9), rgba(255,255,255,0));
    -fx-background-radius: 7, 6, 5, 4, 3, 5;
    -fx-background-insets: -3 -3 -4 -3, -3, 0, 1, 2, 0;
    -fx-font-family: "Arial";
    -fx-font-size: 14;
    -fx-text-fill: white;
    -fx-padding: 5 10 5 10;
    -fx-effect: dropshadow( one-pass-box , rgb(0, 255, 0, 0.6), 10, 0.5 , 0 , 1 );
}

如果我将css和图像包括到jar文件中,那么一切都很好,但是我想使用外部文件。 像所有引用一样,设置相对于css文件的路径似乎不起作用,那么正确的路径是什么?

如果我使用它将其放在URL中,这现在似乎可以工作。

String filename = "worldmap.jpg"; 
HostServices services = this.getHostServices(); 
String url = services.resolveURI(services.getCodeBase(), "Resources/Wallpapers/" + filename);

生成的字符串与我的案例7 tho中的字符串相同,我不知道为什么以前和现在都不起作用。

我第一次在SceneController的构造函数中设置背景,现在我将其设置为在将场景添加到舞台后调用的额外方法中设置它的背景,也许这可以做到这一点,但我无法弄清楚错误的出处真正的谎言。

对于任何有类似问题的人的答案,最好使用HostServices来解析路径,如上面的示例所示。

不知道您是否可以正常使用,但这是正确的答案:

root.setStyle("-fx-background-image: url('file://D:/ProjectFolder/Resources/Wallpapers/worldmap.jpg');");

文件实现应为“ file:// PATH”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM