好吧,我是JavaFx的新手,而且我已经很长时间没有使用Java了,所以我遇到了很多问题。最大的问题是如何更改该死窗格的bg。
下面是Controller类
//Styling prePane
public class Controller {
//Declaring elements
public Pane prePane;
public Button generate;
public TextArea info;
@FXML
ProgressBar progressBar;
public void onGenerate() throws IOException {
//Styling prePane
prePane=new Pane();
prePane.getStyleClass().add("prePane");
//Creating and embedding progressBar
generate.setDisable(true);
progressBar.setProgress(0);
//Creating task object
Task copyWorker = createWorker();
progressBar.progressProperty().unbind();
progressBar.progressProperty().bind(copyWorker.progressProperty());
copyWorker.messageProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println(newValue);
}
});
//Starting task thread
new Thread(copyWorker).start();
//QR Code generation
String details;
info.getParagraphs();
details=String.valueOf(info.getText());
ByteArrayOutputStream out= net.glxn.qrgen.QRCode.from(details).to(ImageType.GIF).stream();
File file=new File("D:\\JavaFXQRGenerator-master\\QrGenerator\\QrCode\\details.jpg");
FileOutputStream fos=new FileOutputStream(file);
fos.write(out.toByteArray());
fos.flush();
}
//Defining the task
public Task createWorker() {
return new Task() {
@Override
protected Object call() throws Exception {
for (int i = 0; i < 10; i++) {
updateProgress(i + 1, 10);
}
return true;
}
};
}
}
主班
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("QR Generator");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
在实际的样式表下方
.prePane{
-fx-background-image: url("D:\JavaFXQRGenerator-master\QrGenerator\resources\genPane.jpg");
}
任何帮助都将受到赞赏。
最佳答案
假设resources
文件夹是构建路径的一部分,则genPane.jpg
将位于类路径的根目录中。所以根据CSS documentation的正确路径就是
.prePane{
-fx-background-image: url("/genPane.jpg");
}