一、前言
1.GUI全稱是Graphical User Interface,就是圖形用戶界面。JAVA的GUI應用廣泛在我們生活中也很常見。很多應用使用該GUI編程設計,像點擊QQ圖標彈出對應的登錄窗體。
一般程序與用戶的交互都基于對應程序的運行界面。
2.JPanel面板是SWING下的一個面板容器類。該面板支持嵌套,可設置布局方式,設置不同的布局管理器可添加其他控件像JButton按鈕,JTextField文本框等。來設計完善一個程序界面窗體。
作為繪制面板支持setBackground()設置背景顏色的方法還遠遠不夠。這里實現自定義為JPanel設置圖片背景。
二、平臺工具
1.MyEclipse
此處演示使用myeclipse2014
其他支持java awt+swing平臺也可
三、圖文展示
1.同一窗體下做不同處理JPanel的效果
(1)首先創建一個不加修飾的窗體,一般的普通默認jpanel界面效果如下:
(2)簡單的設置背景顏色效果:
(3)自定義處理后的JPanel下的窗體效果:
2.代碼實現
自定義JPanel背景處理,該圖片為bg.PNG,與測試類在同一路徑下,使用圖片注意使用相對路徑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
import java.awt.Graphics; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; public class GUITest { private static JFrame jframe; //聲明一個窗體 private JPanel jpanel; //聲明一個畫板 public GUITest(){ //構造方法 jframe = new JFrame(); init(); } private void init(){ jframe.setTitle( "測試" ); jpanel = new JPanel(){ //關鍵代碼,就是重寫了paint的一個方法 @Override protected void paintComponent(Graphics g) { super .paintComponent(g); ImageIcon img = new ImageIcon(GUITest. class .getResource( "bg.png" )); /** * bg.PNG這個地方換成自己的圖片 * 此處使用的相對路徑,bg.png跟該測試類在同一路徑下 * 不過建議使用相對路徑避免使用絕對路徑 */ img.paintIcon( this , g, 0 , 0 ); } }; jpanel.setOpaque( true ); jframe.setBounds( 200 , 200 , 500 , 400 ); //設置顯示位置距離左邊200像素距離上邊200像素及屏幕大小500*400 jframe.add(jpanel); //添加畫板到窗體 jframe.setVisible( true ); //設置顯示界面 } public static void main(String[] args) { new GUITest(); // 實例化對象 } } |
四、拓展布局管理器
下面簡單寫個登錄窗體:
基于自定義的JPanel背景,設置GridBagLayout布局,添加按鈕文本框等基本控件實現的一個簡單登錄窗體。
(1)代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class GUIT { //聲明窗體,面板及控件 private static JFrame jframe; private JLabel jlabel,jlabel1; private GridBagLayout gridbag; private GridBagConstraints constraints; private JTextField jtfield1; private JPasswordField jpfield1; private JButton jbutton1,jbutton2,jbutton3; private JPanel jpanel; public GUIT(){ jframe = new JFrame(); jlabel = new JLabel(); jlabel1 = new JLabel(); jtfield1 = new JTextField(); jpfield1 = new JPasswordField(); gridbag = new GridBagLayout(); jbutton1 = new JButton(); jbutton2 = new JButton(); jbutton3 = new JButton(); init(); } /** * init()初始化并顯示界面 */ private void init(){ jframe.setTitle( "登錄" ); /** * 設置JPanel背景 */ jpanel = new JPanel(){ @Override protected void paintComponent(Graphics g) { super .paintComponent(g); ImageIcon img = new ImageIcon(GUITest. class .getResource( "ddmbg.jpg" )); img.paintIcon( this , g, 0 , 0 ); } }; //為JLabel,JButton初始化文本 jlabel.setText( "用戶名:" ); jlabel1.setText( "密 碼:" ); jbutton1.setText( "登錄" ); jbutton2.setText( "退出" ); jbutton3.setText( "注冊" ); //設置顯示位置及屏幕大小500*400 jframe.setBounds( 450 , 240 , 400 , 240 ); //jpanel采用GridBagLayout布局管理器 jpanel.setOpaque( false ); jpanel.setLayout(gridbag); //初始化用戶名label,并添加該控件到畫板 constraints = getGridBagConstraints( 0 , 0 , 1 , 1 , 0 , 0 ,GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets( 10 , 0 , 10 , 0 ), 0 , 0 ); gridbag.setConstraints(jlabel, constraints); jpanel.add(jlabel); //初始化用戶名文本框,并添加該組件到畫板 constraints = getGridBagConstraints( 1 , 0 , 1 , 1 , 0 , 0 ,GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets( 10 , 0 , 10 , 0 ), 100 , 0 ); gridbag.setConstraints(jtfield1, constraints); jpanel.add(jtfield1); //初始化密碼label constraints = getGridBagConstraints( 0 , 1 , 1 , 1 , 0 , 0 ,GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets( 10 , 0 , 10 , 0 ), 0 , 0 ); gridbag.setConstraints(jlabel1, constraints); jpanel.add(jlabel1); //初始化密碼文本框 constraints = getGridBagConstraints( 1 , 1 , 1 , 1 , 0 , 0 ,GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets( 10 , 0 , 10 , 0 ), 100 , 0 ); gridbag.setConstraints(jpfield1, constraints); jpanel.add(jpfield1); //初始化注冊按鈕,并添加該控件到畫板 constraints = getGridBagConstraints( 0 , 2 , 1 , 1 , 0 , 0 ,GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets( 10 , 0 , 10 , 0 ), 0 , 0 ); gridbag.setConstraints(jbutton3, constraints); jpanel.add(jbutton3); //初始化登錄按鈕 constraints = getGridBagConstraints( 1 , 2 , 1 , 1 , 0 , 0 ,GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets( 10 , 0 , 10 , 0 ), 0 , 0 ); gridbag.setConstraints(jbutton1, constraints); jpanel.add(jbutton1); //初始化退出按鈕 constraints = getGridBagConstraints( 2 , 2 , 1 , 1 , 0 , 0 ,GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets( 10 , 0 , 10 , 0 ), 0 , 0 ); gridbag.setConstraints(jbutton2, constraints); jpanel.add(jbutton2); //添加畫板到窗體 jframe.add(jpanel); //窗體初始化完成 } private static GridBagConstraints getGridBagConstraints( int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill,Insets insets, int ipadx, int ipady){ return new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insets, ipadx, ipady); } public static void main(String[] args) { new GUIT(); jframe.setVisible( true ); } } |
其中ddmbg為圖片名
(2)實現效果如圖所示:
GUI設計中布局是基礎也是十分重要的知識。
熟練使用掌握三大布局及其他布局管理器需要自己敲代碼練習了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。