2

アンカーペインの背景画像を動的に設定するのに問題があります。開発の目的で、外部リソースをプロジェクトの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ファイルに相対的なパスを設定することは機能していないようです。正しいパスは何でしょうか?

4

2 に答える 2

1

これを使用してURLに入れると、これでうまくいくようです。

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

結果の文字列は私の場合の7thoと同じですが、なぜ以前は機能しなかったのか、現在は機能しているのかわかりません。

初めてSceneControllerのコンストラクターで背景を設定しましたが、シーンがステージに追加された後に呼び出される追加のメソッドで設定しました。おそらくそれは何かをする必要がありますが、どこが間違っているのかわかりません。本当に嘘です。

同様の問題を抱えている人への回答としては、上記の例のようにHostServicesを使用してパスを解決するのが最善です。

于 2013-01-15T18:00:27.633 に答える
0

動作したかどうかはわかりませんが、正解は次のとおりです。

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

ファイルの実装は「file://PATH」である必要があります

于 2013-01-14T19:15:20.610 に答える