Thứ Ba, 14 tháng 6, 2011

[JAVA]Sử Dụng ảnh Làm Background Cho JFrame

- Chắc hẳn khi học Java,các bạn sẽ thấy bực mình vì không chỉnh ,tạo giao diện cho JFrame 1 cách dễ dàng như trong C#.Hôm nay mình sẽ hướng dẫn các bạn các cách mà mình biết để tạo background cho JFrame từ 1 tấm ảnh.

-Cách thứ nhất: Sử dụng 1 Component có thể hiển thị ảnh để làm background và sau này tất cả những Component khác muốn add vô JFrame thì ta add lên component chứa background.Ở đây mình sử dụng JLabel để làm demo.

view plaincopy to clipboardprint?

1. package sprite;
2.
3. import java.awt.Component;
4. import javax.swing.ImageIcon;
5. import javax.swing.JButton;
6. import javax.swing.JFrame;
7. import javax.swing.JLabel;
8.
9. /**
10. *
11. * @author tauit_dnmd
12. */
13. public class testFrame extends JFrame
14. {
15. public static final String strImagePath="BoundBall.png";
16. public JLabel contentBackground;
17. public testFrame() {
18. this.contentBackground=new JLabel(new ImageIcon(getClass().getResource(strImagePath)));
19. this.contentBackground.setBounds(0,0,960,960);
20. this.setSize(960,960);
21. this.add(contentBackground);
22.
23. }
24.
25. public static void main(String[] str) {
26.
27. testFrame frame=new testFrame();
28. frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
29. frame.setVisible(true);
30. for(int i=0;i<10;i++) 31. { 32. JButton but=new JButton("A"+i); 33. but.setBounds(60*i+2,10,60,30); 34. but.setVisible(true); 35. frame.contentBackground.add(but); 36. } 37. 38. } 39. 40. } package sprite; import java.awt.Component; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; /** * * @author tauit_dnmd */ public class testFrame extends JFrame { public static final String strImagePath="BoundBall.png"; public JLabel contentBackground; public testFrame() { this.contentBackground=new JLabel(new ImageIcon(getClass().getResource(strImagePath))); this.contentBackground.setBounds(0,0,960,960); this.setSize(960,960); this.add(contentBackground); } public static void main(String[] str) { testFrame frame=new testFrame(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); for(int i=0;i<10;i++) { JButton but=new JButton("A"+i); but.setBounds(60*i+2,10,60,30); but.setVisible(true); frame.contentBackground.add(but); } } }





--> tuy vậy cách này chỉ là tạm thời vì khi add thêm control thì ta fai add vô JLabel chứ không fai vào JFrame,làm cho thao tác bị rối ,và dễ gây nhầm lẫn.Và khi Jframe thay đổi kích thước thì background bị mất 1 phần.



+Cách 2: Khi 1 Control được add vô JFrame thì không fai add lên JFrame mà là add vô ContentPane của JFrame.Vậy muốn set background cho JFrame ta chỉ cần tìm cách vẽ lại ContentPane theo ý muốn của chúng ta ( vẽ tấm ảnh dùng làm background lên ContentPane).

+ Sử dụng JPanel làm content mới .Override phương thức PaintConponent(Graphics g) của JPanel. Sau đó set JPanel này là ContentPanel mới của JFrame bằng phương thức setContentPane(...);

view plaincopy to clipboardprint?

1. /*
2. * To change this template, choose Tools | Templates
3. * and open the template in the editor.
4. */
5.
6. package sprite;
7.
8. import java.awt.Component;
9. import java.awt.Graphics;
10. import javax.swing.ImageIcon;
11. import javax.swing.JButton;
12. import javax.swing.JFrame;
13. import javax.swing.JLabel;
14. import javax.swing.JPanel;
15.
16. /**
17. *
18. * @author tauit_dnmd
19. */
20. public class testFrame extends JFrame
21. {
22. public static final String strImagePath="BoundBall.png";
23. ImageIcon background;
24. JPanel jpanel;
25. public testFrame() {
26.
27. background=null;
28. this.setSize(300,200);
29. jpanel=new JPanel(){
30. @Override
31. protected void paintComponent(Graphics g) {
32. super.paintComponent(g);
33. if(background!=null)
34. {
35. g.drawImage(background.getImage(),
36. 0,0,getWidth(),getHeight(),null);
37. }
38. }
39. };
40. setContentPane(jpanel);
41.
42.
43. }
44. public void setBackground(ImageIcon img)
45. {
46. this.background=img;
47. }
48. public static void main(String[] str) {
49.
50. testFrame frame=new testFrame();
51. frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
52. frame.setBackground(new ImageIcon("BoundBall.png"));
53. frame.setVisible(true);
54. for(int i=0;i<10;i++) 55. { 56. JButton but=new JButton("A"+i); 57. but.setBounds(60*i+2,10,60,30); 58. but.setVisible(true); 59. frame.add(but); 60. } 61. 62. } 63. 64. } 65. 66. /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package sprite; import java.awt.Component; import java.awt.Graphics; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * * @author tauit_dnmd */ public class testFrame extends JFrame { public static final String strImagePath="BoundBall.png"; ImageIcon background; JPanel jpanel; public testFrame() { background=null; this.setSize(300,200); jpanel=new JPanel(){ @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if(background!=null) { g.drawImage(background.getImage(), 0,0,getWidth(),getHeight(),null); } } }; setContentPane(jpanel); } public void setBackground(ImageIcon img) { this.background=img; } public static void main(String[] str) { testFrame frame=new testFrame(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setBackground(new ImageIcon("BoundBall.png")); frame.setVisible(true); for(int i=0;i<10;i++) { JButton but=new JButton("A"+i); but.setBounds(60*i+2,10,60,30); but.setVisible(true); frame.add(but); } } }




Ta thấy hiện tượng mất 1 phần background khi co giãn JFrame không còn nữa .



Trên đây chỉ là 2 cách mình biết khi mò Java ở học kì vừa rồi.Nếu bạn nào có cách khác hay hơn,chia sẻ anh em biết với nha.