如何在 java eclipse 中向 JPanel 添加背景 (URL)

how to add a background (URL) to a JPanel in java eclipse

我们刚刚开始使用 java。我们想使用来自互联网的图像并将其作为我们的 Jpanel 的背景。有人可以帮助我们吗?我们尝试使用以下代码:

 public class Achtergrond3 extends JPanel { 
        private ImageIcon img;
        private JLabel label;

        public Achtergrond3() {
             img = new ImageIcon("res/textures/newbackground.jpg"); 
             label= new JLabel(img);

             this.add(label);
        }

        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
        }
}

我认为这 link 会对您有所帮助...

Simplest way to set image as JPanel background

讲的是如何将图片设置为JPanel背景