从JavaFX中的FileChooser打开图像
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从JavaFX中的FileChooser打开图像,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1612字,纯文字阅读大概需要3分钟。
内容图文
![从JavaFX中的FileChooser打开图像](/upload/InfoBanner/zyjiaocheng/695/d5ee90d295104ea6a1d6d273a2d0f349.jpg)
我的程序应该从文件上传图像,然后将其显示为背景.我的问题是,当我在其参数中创建Image对象时,它会询问您要放置的文件.我试图将我的File对象放入其参数内,但无法正常工作.请帮我.我被卡住了
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);
}
}
解决方法:
问题在于Image的构造函数需要一个String url,而您正在传递给它一个File.任何优秀的IDE都会告诉您给定方法的参数期望值.找到该键盘快捷方式并使用它(IntelliJ中的Ctrl P).从那里开始,您所要做的就是找到一种将文件转换为表示其URL的字符串的方法.在这种情况下:
Image image1 = new Image(file.toURI().toString());
请注意,您实际上从未设置过背景图片,需要在lambda中添加以下行:
stac.setBackground(new Background(backgroundImage));
但是,为此,您将必须将stac声明移到动作侦听器上方.
内容总结
以上是互联网集市为您收集整理的从JavaFX中的FileChooser打开图像全部内容,希望文章能够帮你解决从JavaFX中的FileChooser打开图像所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。