如何將wall1.jpg
設置爲我的JFrame
或JPanel
的背景?如何在我的JPanel或JFrame中設置背景圖標?
public class JBackGroundImageClass extends JFrame
{
Image img;
private final JPanel JPanel;
public JBackGroundImageClass()
{
setLayout (new BorderLayout());
setBounds(22,33,400, 400);
setVisible(true);
img = Toolkit.getDefaultToolkit().createImage("wall1.jpg");
JPanel = new JPanel()
{
public void paintComponent(Graphics g)
{img = Toolkit.getDefaultToolkit().createImage("wall1.jpg");
g.drawImage(img, 0, 0, null);
}
};
this.add("North" , JPanel);
JPanel.setSize(400, 400);
JPanel.setBackground(Color.red);
JPanel.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new JBackGroundImageClass().setVisible(true);
}
}
「 Swing程序應該重寫'的paintComponent()',而不是覆蓋'的paint()'」 - [在AWT和Swing中繪製:繪製方法](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#callbacks)。 – trashgod
@trashgod:我重試了,但仍然一樣。 – YumYumYum
檢查在這篇文章中接受的答案。我以前使用過相同鏈接的代碼,它的工作原理如下:http://stackoverflow.com/questions/4051408/jpanel-with-image-background –