3
我想有一個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不顯示。有任何想法嗎?
在paint()中創建Swing對象的不好主意!您應該在Table構造函數或您的類的某些方法中創建它們。 – PhiLho 2010-01-26 17:03:36
是的抱歉,我只是在那裏舉例說明 – Aly 2010-01-26 17:05:46
您不是在繪製面板,而是在創建它。 表是哪種對象?您應該將面板添加到Table對象(但不是在paint()方法中) – Fortega 2010-01-26 17:20:44