如何进入一个图像变量在另一个按钮的方法在外汇java

0

的问题

openButton.setOnAction((final ActionEvent X) -> {
            File newfile = fileChooser.showOpenDialog(stage);

            Image userselectedimage = new Image(file.toURI().toString());

more code


 newactionbutton.setOnAction((final ActionEvent X) ->{
              * i need access to the user selected image variable here *
}
            

我需要访问的可变userselectedimage(这是一个图像用户选择的背景),在另一个按钮。 它是可能的,因为该范围的定义是在这击行动,访问这个变量在另一个按钮? 我需要它由于其用户所选择的画面(这是一个文件,用户选择). 我试图定义以外的方法的室内用一个静态的变量,但没有工作,所以我的想法。

谢谢。

java javafx
2021-11-23 20:03:17
1

最好的答案

-1
Image userselectedimage = null;    
openButton.setOnAction((final ActionEvent X) -> {
            File newfile = fileChooser.showOpenDialog(stage);
            userselectedimage = new Image(file.toURI().toString());
}

 newactionbutton.setOnAction((final ActionEvent X) ->{
              userselectedimage.doSomething();
}
2021-11-23 20:15:53

我认为,这一守则将无法编纂,因为userselectedimage被定义为非终地的变量,用于其正在试图制造分配一个值,在一个lambda和参考其价值在另一个lambda。 参见: baeldung.com/java-lambda-effectively-final-local-variables
jewelsea

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................