JAVA GUI学习 - 窗体背景图片设置方法:重写paintComponent(Graphics g)方法

 1 public class BackgroundImage extends JFrame

 2 {

 3     public BackgroundImage()

 4     {

 5         this.setTitle("窗体背景图片设置方法");

 6         this.setSize(700, 471);

 7         

 8         JPanel jPanel = new JPanel()

 9         {

10 

11             @Override

12             protected void paintComponent(Graphics g)

13             {

14                 ImageIcon icon = new ImageIcon("images/2.jpg");

15                 g.drawImage(icon.getImage(), 0, 0, BackgroundImage.this.getWidth(), BackgroundImage.this.getHeight(), BackgroundImage.this);

16             }

17             

18         };

19         this.add(jPanel);

20         

21         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

22     }

23 

24     /**

25      * @param args

26      */

27     public static void main(String[] args)

28     {

29         BackgroundImage backgroundImage = new BackgroundImage();

30         backgroundImage.setVisible(true);

31     }

32 

33 }

 

你可能感兴趣的:(component)