我做的是登錄界面。用了JButton(按鈕),JComboBox(組合框),JOptionPane(提示面板),JPasswordField(密碼框),JTextField(文本框),用監聽器實現了事件相應。當輸入帳號和密碼都是123時候,提示登錄成功,否則登錄失敗!
還添加了java中的顏色和字體類。字體為“CENTER_BASELINE”,顏色為黑色。而且在確定和取消按鈕上加入了快捷鍵。
但是ImageIcon(圖片)沒有實現。
下面是源代碼:
1 package wole;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.KeyEvent;
8 import javax.swing.ImageIcon;
9 import javax.swing.JButton;
10 import javax.swing.JComboBox;
11 import javax.swing.JFrame;
12 import javax.swing.JLabel;
13 import javax.swing.JOptionPane;
14 import javax.swing.JPanel;
15 import javax.swing.JPasswordField;
16 import javax.swing.JTextField;
17
18 public class showLoginFrame extends JFrame implements ActionListener {
19
20 private static final long serialVersionUID = 1L;
21
22 JPanel p;
23 JButton a;
24 JButton b;
25 JLabel u;
26 JLabel m;
27 JLabel se;
28 JLabel t;
29 JLabel shfen;
30 JComboBox kuang3;
31 ImageIcon tu;
32 JTextField kuang1;
33 JPasswordField kuang2;
34 String role[] = { "管 理 員", "其 他" };
35
36 public showLoginFrame() {
37
38 se = new JLabel("網 站 內 容 管 理 系 統");
39 se.setBounds(115, 20, 420, 50);
40 se.setFont(new Font("楷體", Font.CENTER_BASELINE, 20));
41 se.setForeground(Color.black);
42
43 u = new JLabel("用戶名:");
44 u.setFont(new Font("楷體", Font.CENTER_BASELINE, 15));
45 u.setForeground(Color.black);
46
47 m = new JLabel("密碼:");
48 m.setFont(new Font("楷體", Font.CENTER_BASELINE, 15));
49 m.setForeground(Color.black);
50
51 shfen = new JLabel("身份:");
52 shfen.setFont(new Font("楷體", Font.CENTER_BASELINE, 15));//設置字體
53 shfen.setForeground(Color.black);
54 shfen.setBounds(130, 200, 100, 50);
55
56 kuang3 = new JComboBox(role);
57 kuang3.setForeground(Color.black);
58 kuang3.setBounds(210, 210, 100, 25);
59
60 kuang1 = new JTextField();
61 kuang1.setBounds(210, 100, 180, 25);
62 kuang1.setForeground(Color.black);
63
64 kuang2 = new JPasswordField();
65 kuang2.setBounds(210, 160, 180, 25);
66 kuang2.setEchoChar('*');//設置密碼框出現的符號
67 kuang2.setForeground(Color.black);
68
69 a = new JButton("確定(ALT+Y)");
70 a.setMnemonic(KeyEvent.VK_Y);//給確定按鈕增加快捷鍵,ALT+G
71 a.setForeground(Color.black);//所設置字體顏色為藍色
72 a.addActionListener(this);//對這個按鈕進行監聽
73
74 b = new JButton("取消(ALT+N)");
75 b.setMnemonic(KeyEvent.VK_N);
76 b.setForeground(Color.black);
77 b.addActionListener(this);
78
79 p = new JPanel();
80 tu = new ImageIcon("/img/22.jpg");//這是圖片
81 t = new JLabel(tu);//將圖片添加到JLabel當中
82 p.add(t);//將添加到JPanel當中
83
84 p.setBounds(0, 0, 560, 400);//圖片560*400(寬*高)
85 b.setBounds(320, 270, 120, 35);
86 a.setBounds(120, 270, 120, 35);
87 u.setBounds(130, 90, 100, 50);
88 m.setBounds(130, 150, 100, 50);
89
90 add(se);
91 add(u);
92 add(m);
93 add(shfen);
94 add(kuang3);
95 add(kuang1);
96 add(kuang2);
97 add(a);
98 add(b);
99 add(p);
100
101 setTitle("歡 迎 登 陸 網 站 內 容 管 理 系 統");
102 setSize(560,400);
103 setResizable(false);//不能改變界面大小
104 setLayout(null);//設置界面的布局管理器為空
105 setVisible(true);
106 setLocationRelativeTo(null);//讓窗口位於屏幕中央
107
108 }
109
110 public static void main(String[] args) {
111
112 new showLoginFrame();
113
114 }
115
116 public void actionPerformed(ActionEvent e) {
117 if (e.getSource() == a)
118
119 {
120 if (kuang1.getText().trim().equals("")) {
121
122 JOptionPane.showMessageDialog(null,"請輸入用戶名!", "用戶名空提示",JOptionPane.OK_OPTION);
123
124 }
125
126 else {
127
128 if (new String(kuang2.getPassword()).equals("")) {
129
130 JOptionPane.showMessageDialog(null,"請輸入密碼!", "密碼空提示",JOptionPane.OK_OPTION);
131
132 }
133
134 else {
135
136 if (kuang1.getText().trim().equals("123")&& (new String(kuang2.getPassword()).equals("123"))) {
137
138 this.dispose();//使當前界面退出
139
140 JOptionPane.showMessageDialog(null,"恭喜你,登陸成功!");
141 }
142
143 else
144
145 JOptionPane.showMessageDialog(null,"您輸入的用戶名或密碼錯誤!");
146
147 kuang2.setText(null);//設置密碼框為空
148
149 }
150 }
151 }
152 if (e.getSource() == b)
153
154 System.exit(0);//關閉當前界面
155
156 }
157
158 }