0
我的程序應該從文件上載圖像,然後將該圖像顯示爲背景。我的問題是,當我在它的參數中創建一個Image
對象時,它會詢問您正在嘗試放置的文件。我試圖把我的File對象放在它的參數中,它不起作用。請幫幫我。我困了。從JavaFX中的FileChooser打開圖像
public class FileOpener extends Application{
public void start(final Stage stage) {
stage.setTitle("File Chooser Sample");
final FileChooser fileChooser = new FileChooser();
final Button openButton = new Button("Choose Background Image");
openButton.setOnAction((final ActionEvent e) -> {
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
// openFile(file);
// where my problem is
Image image1 = new Image("file");
// what I tried to do
// Image image1 = new Image(file);
ImageView ip = new ImageView(image1);
BackgroundSize backgroundSize = new BackgroundSize(100, 100, true, true, true, false);
BackgroundImage backgroundImage = new BackgroundImage(image1, BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
}
});
final StackPane stac = new StackPane();
stac.getChildren().add(openButton);
stage.setScene(new Scene(stac, 500, 500));
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}