我希望能够将我的 JPanel 背景之一更改为我创建的图像,并且仍然在其上添加组件,但是它们没有显示出来。
package userInterface;
import javax.swing.*;
import java.awt.*;
@SuppressWarnings("serial") public class BoardPanel extends JPanel {
private Image img;
public BoardPanel() {
img = new ImageIcon("images/board.png").getImage();
JButton button = new JButton("TEST ME");
add(button);
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}