調用setIcon
將覆蓋圖標。但是,您可以嘗試如下所示:
// Assumed that these are non-null
BufferedImage bigIcon, smallIcon;
// Create a new image.
BufferedImage finalIcon = new BufferedImage(
bigIcon.getWidth(), bigIcon.getHeight(),
BufferedImage.TYPE_INT_ARGB)); // start transparent
// Get the graphics object. This is like the canvas you draw on.
Graphics g = finalIcon.getGraphics();
// Now we draw the images.
g.drawImage(bigIcon, 0, 0, null); // start at (0, 0)
g.drawImage(smallIcon, 10, 10, null); // start at (10, 10)
// Once we're done drawing on the Graphics object, we should
// call dispose() on it to free up memory.
g.dispose();
// Finally, convert to ImageIcon and apply.
imageLabel.setIcon(new ImageIcon(finalIcon));
這會創建一個新圖像,繪製大圖標,然後繪製小圖標。
你也可以畫其他的東西,比如outlining a rectangle或filling an oval。
對於更先進的圖形功能,儘量轉換成一Graphics2D對象。
您已經爲'JLabel'提供的代碼,但你標記它的題爲它作爲'JFrame' - 這是什麼呢? – wchargin