语句 1 从 css 获取图像文件,即使我提供了绝对 url,也找不到图像文件。为什么?
图像文件位置是正确的,因为在语句 3 中,它有效。这是对jewelsea 发布的解决方案的进一步查询。
import javafx.scene.image.Image;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TextFieldCssSample extends Application {
@Override
public void start(Stage stage) {
TextField textField = new TextField();
textField.setId("textField");
StackPane layout = new StackPane();
layout.getChildren().addAll(textField);
// 1) following statement **fails**. resources folder is below the root level folder
textField.setStyle("-fx-border-color: red; -fx-background-image:url('/resources/pumpkin-icon.png'); -fx-background-repeat: no-repeat; -fx-background-position: right center;");
// 2) following statement succeeds in finding a http url.
// textField.setStyle("-fx-border-color: red; -fx-background-image:url('http://icons.iconarchive.com/icons/rockettheme/halloween/32/pumpkin-icon.png'); -fx-background-repeat: no-repeat; -fx-background-position: right center;");
// 3) following statement succeeds finding image file.
stage.getIcons().add(new Image("/resources/pumpkin-icon.png"));
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
更新:cnahr 的解决方法奏效了!我还打开了 JavaFX 错误报告。RT-31131