我想知道如何使用我当前的代码将我的JPanel图像添加到JFrame中。 我知道我有Jframe工作但我需要Panel来显示我的图像,这样我就可以在同一个JFrame中设置多个重叠的图像。如何通过更改当前代码来完成此操作?
import javax.imageio.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.awt.FlowLayout;
import java.awt.Dimension;
public class Background {
public Background(JFrame frame) throws IOException{
BufferedImage castles = ImageIO.read
(newFile("C:/Users/dude42/Documents/game/castles.png"));
JLabel label = new JLabel();
label.setLocation(0,0);
label.setIcon(new ImageIcon(castles));
frame.add(label);
frame.setVisible(true);
frame.setSize(1080,1080);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane0 = new JPanel();
pane0.setPreferredSize(new Dimension(1080, 1080));
pane0.setLayout(null);
pane0.add(label,0);
pane0.setVisible(true);
}
public static void main(String avg[]) throws IOException{
JFrame frame = new JFrame();
Background background = new Background(frame);
}
}