且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用NetBeans IDE将图像添加到JPanel

更新时间:2023-12-05 20:22:58

使用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);
...