使用NetBeans IDE将图像添加到JPanel [英] Adding an Image to JPanel using NetBeans IDE

查看:102
本文介绍了使用NetBeans IDE将图像添加到JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Netbeans将n个图像添加到面板中. 我从面板上将面板拖到框架上,然后根据我想要的图像大小来调整其大小. 然后在构造函数中,将图像添加到面板(我将其命名为panelImage),就像这样.

I am trying to add n image to a panel using Netbeans. I dragged a Panel from the palette on to the frame and sized it according to what I want the image size to be. In the constructor I then added the image to the panel(I named it panelImage) like this.

JLabel label = new JLabel(new ImageIcon("images\\BrokenFrameResized.jpg"));
    paneImage.add(label);

但是图像不显示. 使用Matisse布局管理器(即拖放面板)将图像显示为面板大小的最佳方法是什么.

The image however does not display. What is the best way to have the an image displayed as the size of the Panel using Matisse layout manager(i.o.w. dragging and dropping the panel).

使用paintComponent(Graphics g)更好吗?

Is it better to use paintComponent(Graphics g)?.

推荐答案

使用paintComponent(Graphics g)更好吗?

Is it better to use paintComponent(Graphics g)?.

如果要让图像显示在其他组件的后面,否则将其显示在JLabel中.这里的问题很可能是您正在访问嵌入式应用程序资源,就像它是File一样.嵌入式资源必须通过URL访问.

It is if you want the image to appear behind other components, otherwise display it in a JLabel. The problem here is most likely that you are accessing an embedded application resource as if it were a File. Embedded resources must be accessed via URL.

URL urlToImage = this.getClass().getResource("/images/BrokenFrameResized.jpg");
ImageIcon icon = new ImageIcon(urlToImage);
...

这篇关于使用NetBeans IDE将图像添加到JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆