带背景图像的 JPanel,与其他面板重叠
我想要一个使用图像作为背景的 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 不显示。有什么想法吗?
I want to have a JPanel which uses an image as a background, with this I want to add new panels to this panels so that they sit on top of this background image. I have tried the following:
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);
}
The background image is displayed but the OverlayedPanel1 doesnt show. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有将
OverlayedPanel1
添加到面板中。You didn't add
OverlayedPanel1
to the panel.