简体   繁体   English

如何在 java fx 的另一个按钮方法中访问图像变量

[英]how to access a image variable in another button method in java fx

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 *
}
            

I need to access the variable userselectedimage (it is a image selected by the user for the background) in another button.我需要在另一个按钮中访问变量 userselectedimage(它是用户选择的背景图像)。 is it possible since the scope is defined in that onclick action to access that variable in another button?由于 scope 是在 onclick 操作中定义的,是否有可能在另一个按钮中访问该变量? I need it since its the users selected picture (it is a file that the user selects).我需要它,因为它是用户选择的图片(它是用户选择的文件)。 I tried defining it outside of the method wiht a static variable but that didn't work either, so im out of ideas.我尝试使用 static 变量在方法之外定义它,但这也不起作用,所以我没有想法。

thanks.谢谢。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM