我想要一个使用图像作为背景的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不显示。有什么想法吗?
答案 0 :(得分:2)
您没有在面板中添加OverlayedPanel1
。
add(OverlayedPanel1);