我想要一个使用图像作为背景的 JPanel,为此我想向该面板添加新面板,以便它们位于该背景图像之上。我尝试了以下方法:
Image background;
public Table(){
super();
ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTable.png"));
background = ii.getImage();
setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (background != null){
g.drawImage(background, 0,0,this.getWidth(),this.getHeight(),this);
}
JButton button = new JButton("hello world");
JPanel OverlayedPanel1 = new JPanel();
OverlayedPanel1.setMinimumSize(new Dimension(600,50));
OverlayedPanel1.setMaximumSize(new Dimension(600,50));
OverlayedPanel1.setPreferredSize(new Dimension(600,50));
OverlayedPanel1.add(button, BorderLayout.CENTER);
OverlayedPanel1.setBackground(Color.yellow);
}
显示背景图像,但不显示 OverlayedPanel1。有什么想法吗?
最佳答案
您没有将 OverlayedPanel1
添加到面板。
add(OverlayedPanel1);
关于java - 带有背景图像的 JPanel,其他面板被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2141020/