2012-04-19 73 views
1

我有一個JSplitPane。我想將背景圖像添加到JSplitPane的左側。JSplitPane右側組件背景圖片

mySplitPane.getLeftComponent 

這會使左側組件回退。然後我想將圖像背景添加到左側。我想我可以使用Paint() 將背景圖像設置爲JSplitPane。但我如何設置唯一的左側組件。

我在我的JSplitPane的左側有一個JTable。我想讓JTable透明。然後顯示背景圖像。

現在我已經爲我的JTable設置了背景。我不想用JTable Scroll滾動背景。這就是爲什麼我想添加背景圖像到分割窗格而不是表格。

+0

試試這個由@AndrewThompson在這裏給出的精彩答案,[如何將JPanel添加到JLabel以顯示動畫圖像作爲背景](http://stackoverflow.com/a/10141111/1057230) – 2012-04-19 12:50:35

回答

4

難道這就是你要找的?

class ImagePanel extends JPanel { 
    private Image image; 
    public ImagePanel(Image image) { 
     this.image = image; 
    } 
    @Override 
    protected void paintComponent(Graphics g) { 
     g.drawImage(image, 0, 0, getWidth(), getHeight(), this); 
    } 
} 

用法:

BufferedImage myImage = ImageIO.load(...); 
JPanel leftPanel = new ImagePanel(myImage); 

//Add panel to splitpanel 
JSplitPane mySplitPane= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT) 
mySplitPane.setLeftComponent(leftPanel); 

上面我已經創建的JComponent的子類。覆蓋 paintComponent(Graphics g)方法來繪製我想要 顯示的圖像。然後我設置JPanel的內容窗格,然後最終 面板傳遞給拆分窗格

的左側更多關於水印背景,參見here對於實施例和代碼示例。 編輯

+0

我有一張桌子在左側組件。我想讓表格透明,然後在JTable後面顯示背景圖片。如果我將左側組件設置爲圖像,那麼在哪裏添加我的表格? – Asghar 2012-04-19 12:27:14

+0

將您的JTable添加到leftPanel – Bitmap 2012-04-19 12:30:05

+0

@AndrewThompson非常感謝指針,我編輯了它。 – Bitmap 2012-04-19 12:39:53