使用NetBeans IDE将图像添加到JPanel

Ari*_*ule 1 java swing netbeans image jlabel

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

JLabel label = new JLabel(new ImageIcon("images\\BrokenFrameResized.jpg"));
    paneImage.add(label);
Run Code Online (Sandbox Code Playgroud)

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

使用paintComponent(Graphics g)是否更好?

And*_*son 6

使用paintComponent(Graphics g)是否更好?

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

例如

URL urlToImage = this.getClass().getResource("/images/BrokenFrameResized.jpg");
ImageIcon icon = new ImageIcon(urlToImage);
...
Run Code Online (Sandbox Code Playgroud)