2013-01-05 121 views
2

我有問題動態地設置錨定窗格的背景圖像。 爲了開發目的,我將外部資源放在項目的dist文件夾中,因此這些文件是可用的。JavaFx以編程方式設置外部背景圖像

代碼爲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文件的路徑。我也嘗試去創建網址和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文件的路徑似乎不起作用,那麼正確的路徑是什麼?

回答

1

這似乎如果現在的工作我使用它將其放入URL中。

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

由此產生的字符串是相同的在我的情況7 tho,我不知道爲什麼它沒有工作之前,現在呢。

我第一次在SceneController的構造函數中設置背景,現在我將它設置爲一個額外的方法,在場景添加到舞臺後調用,也許這有些事情要做,但我不能圖出錯的地方在哪裏。

至於任何人遇到類似問題的答案,最好使用HostServices來解析路徑,如上例所示。

0

不知道,如果你得到了它的工作,但這裏是正確的答案:

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

文件執行應該是「文件:// PATH」

+0

不,這是絕對錯誤的。 String filename =「worldmap.jpg」; HostServices services = getHostServices(); String url = services.resolveURI(services.getCodeBase(),「Resources/Wallpapers /」+ filename); 將返回'文件:/ ',現在我再次測試它似乎工作,我想知道爲什麼它不適用之前工作。 –

+0

嗯,絕對錯誤是不合適的,因爲這至少可以在Mac上工作: imageView.setStyle(「 - fx-background-image:url('file://」+ imagePath +「');」); (帶有imagePath ='/ Users/Toto/Workspace/MyProject/images/test。png') 而這不起作用: imageView.setStyle(「 - fx-background-image:url('file:/」+ imagePath +「');」); – Badisi

+0

然後這在Mac上是特別的。雖然在你的例子中字符串會導致tripple /,但不知道這是否會產生影響。但我平時讓java解析URI和URL,所以它應該適合運行代碼的mashine。 –

相關問題