把前兩天做的實驗用GUI實現...
主要的接口都沒有變,所以實現比較容易...
代碼如下:
主要的接口都沒有變,所以實現比較容易...
代碼如下:
??1
/**?*//**
??2
*title?模擬銀行存儲實驗——GUI實現
??3
*@author?realsmy
??4
*date?2006-10-26?8:10
??5
*/
??6
??7
import?java.io.*;
??8
import?java.awt.*;
??9
import?javax.swing.*;
?10
import?java.awt.event.*;
?11
import?javax.swing.JPanel.*;
?12
?13
//login?applicatiion
?14
class?Login?extends?JFrame
{
?15
????Container?c;
?16
????JTextField?text;
?17
????JPasswordField?password;
?18
????MyPanel?panel;
?19
????JLabel?label_name,label_password;
?20
????JButton?button1,button2;
?21
????String?lg_name,lg_password;
?22
????File?fl;//帳戶資料文件聲明
?23
????Login()
{
?24
????????super("模擬銀行存儲系統");
?25
????????c?=?getContentPane();
?26
????????c.setLayout(new?BorderLayout());
?27
????????//初始化組件
?28
????????text?=?new?JTextField();
?29
????????password?=?new?JPasswordField();
?30
????????label_name?=?new?JLabel("教工號?:");
?31
????????label_password?=?new?JLabel("密????碼?:");
?32
????????panel?=?new?MyPanel();
?33
????????button1?=?new?JButton("LOGIN");
?34
????????button2?=?new?JButton("RESET");
?35
????????//添加監聽
?36
????????button1.addActionListener(new?ActionListener()
?37
????????
{
?38
????????????public?void?actionPerformed(ActionEvent?e)
?39
????????????
{
?40
????????????????lg_name?=?text.getText();
?41
????????????????lg_password??=?password.getText();
?42
????????????????if?(?lg_name.equals("admin")?&&?lg_password.equals("admin"))
{
?43
????????????????????setVisible(false);
?44
????????????????????GuanLi?gl?=?new?GuanLi();?
?45
????????????????????gl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
?46
????????????????}
?47
????????????????else
{
?48
????????????????????try
{
?49
????????????????????????fl?=?new?File("frozen",lg_name+".txt");
?50
????????????????????????if(fl.exists())
{
?51
????????????????????????????JOptionPane.showMessageDialog(null,"對不起,您的帳戶已被凍結!");
?52
????????????????????????}
?53
????????????????????????else
{
?54
????????????????????????????fl?=?new?File(lg_name+".txt");
?55
????????????????????????????//判斷帳戶是否存在
?56
????????????????????????????if(!fl.exists())
{
?57
????????????????????????????????JOptionPane.showMessageDialog(null,"對不起,您輸入的帳戶并不存在,請重新輸入:");
?58
????????????????????????????}
?59
????????????????????????????else
{
?60
????????????????????????????//帳戶存在,開始判斷密碼
?61
????????????????????????????????BufferedReader?reader?=?new?BufferedReader(new?FileReader(?lg_name?+?".txt"));
?62
????????????????????????????????String?pw?=?reader.readLine();
?63
????????????????????????????????int?money?=?Integer.parseInt(reader.readLine());
?64
????????????????????????????????//判斷密碼
?65
????????????????????????????????if(lg_password.equals(pw))
{
?66
????????????????????????????????????JOptionPane.showMessageDialog(null,"登陸成功\n"+"您的用戶尚有余額"+money+"元");
?67
????????????????????????????????????ZhangHu?zh?=?new?ZhangHu(lg_name,lg_password,money);
?68
????????????????????????????????????setVisible(false);
?69
????????????????????????????????????YongHu?yh?=?new?YongHu(zh);
?70
????????????????????????????????????yh.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
?71
????????????????????????????????}
?72
????????????????????????????????else
{
?73
????????????????????????????????????JOptionPane.showMessageDialog(null,"對不起,您輸入的密碼不正確,請重新輸入:");
?74
????????????????????????????????}
?75
????????????????????????????}
?76
????????????????????????}????????
?77
????????????????????}catch(HeadlessException?a)
{}catch(IOException?b)
{}
?78
????????????????}
?79
????????????}
?80
????????});
?81
????????button2.addActionListener(new?ActionListener()
?82
????????
{
?83
????????????public?void?actionPerformed(ActionEvent?e)
?84
????????????
{
?85
????????????????text.setText("");
?86
????????????????password.setText("");
?87
????????????}
?88
????????});
?89
?90
????????//設置容器
?91
????????panel.setLayout(new?GridLayout(3,2,10,15));
?92
????????panel.add(label_name);
?93
????????panel.add(text);
?94
????????panel.add(label_password);
?95
????????panel.add(password);
?96
????????panel.add(button1);
?97
????????panel.add(button2);
?98
????????
?99
????????c.add(panel,BorderLayout.CENTER);
100
????????setSize(300,200);
101
????????setLocation(300,200);
102
????????setVisible(true);
103
????}
104
}
105
106
//定義管理界面
107
class?GuanLi?extends?JFrame
{
108
????private?Container?c;
109
????JButton?button1,button2,button3,button4;
110
????MyPanel?panel;
111
????JLabel?label1,label2;
112
????Manager?manager;
113
????//String?name,password,password2;
114
????//int?money;
115
????GuanLi()
{
116
????????super("模擬銀行存儲系統");
117
????????c?=?getContentPane();
118
????????c.setLayout(new?BorderLayout());
119
????????//數據初始化
120
????????//button?=?new?JButton("");
121
????????panel?=?new?MyPanel();
122
????????label1?=?new?JLabel("歡迎光臨趙家銀行!");
123
????????label2?=?new?JLabel("請選擇你要進行的操作:");
124
????????button1?=?new?JButton("1.添加帳戶");
125
????????button2?=?new?JButton("2.刪除用戶");
126
????????button3?=?new?JButton("3.凍結用戶");
127
????????button4?=?new?JButton("4.退出");
128
????????
129
????????//按鈕監聽
130
????????button1.addActionListener(new?ActionListener()
{
131
????????????public?void?actionPerformed(ActionEvent?e)
{
132
????????????????manager?=?new?Manager();
133
????????????????manager.add();
134
????????????}
135
????????});
136
????????button2.addActionListener(new?ActionListener()
{
137
????????????public?void?actionPerformed(ActionEvent?e)
{
138
????????????????manager?=?new?Manager();
139
????????????????manager.del();
140
????????????}
141
????????});
142
????????button3.addActionListener(new?ActionListener()
143
????????
{
144
????????????public?void?actionPerformed(ActionEvent?e)
{
145
????????????????manager?=?new?Manager();
146
????????????????manager.froze();
147
????????????}
148
????????});
149
????????button4.addActionListener(new?ActionListener()
150
????????
{
151
????????????public?void?actionPerformed(ActionEvent?e)
152
????????????
{
153
????????????????System.exit(0);
154
????????????}
155
????????});
156
????????panel.setLayout(new?GridLayout(6,1,50,5));
157
????????//c.setLayout(new?GridLayout(4,2));
158
????????panel.add(label1);
159
????????panel.add(label2);
160
????????panel.add(button1);
161
????????panel.add(button2);
162
????????panel.add(button3);
163
????????panel.add(button4);
164
165
????????c.add(panel);
166
????????setSize(400,300);
167
????????setLocation(300,200);
168
????????setVisible(true);
169
????}
170
}
171
172
//定義用戶界面
173
class?YongHu?extends?JFrame
{
174
????private?Container?c;
175
????JButton?button1,button2,button3,button4;
176
????MyPanel?panel;
177
????JLabel?label1,label2;
178
????ZhangHu?zhanghu,zhanghu2;
179
????//String?name,password,password2;
180
????String?toname,password;
181
????int?money,money2,money3;
182
????File?fl;
183
????YongHu(ZhangHu?zh)
{
184
????????super("模擬銀行存儲系統");
185
????????zhanghu?=?zh;
186
????????c?=?getContentPane();
187
????????c.setLayout(new?BorderLayout());
188
????????//數據初始化
189
????????panel?=?new?MyPanel();
190
????????label1?=?new?JLabel("歡迎光臨趙家銀行!");
191
????????label2?=?new?JLabel("請選擇你要進行的操作:");
192
????????button1?=?new?JButton("1.存錢");
193
????????button2?=?new?JButton("2.取錢");
194
????????button3?=?new?JButton("3.轉帳");
195
????????button4?=?new?JButton("4.退出");
196
????????
197
????????//按鈕監聽
198
????????button1.addActionListener(new?ActionListener()
{
199
????????????public?void?actionPerformed(ActionEvent?e)
{
200
????????????????try
{
201
????????????????????money?=?Integer.parseInt(JOptionPane.showInputDialog(null,"請輸入您要存入的金額:"));
202
????????????????}catch(HeadlessException?o)
{}
203
????????????????zhanghu.setM(money);
204
????????????????infile(zhanghu);
205
????????????}
206
????????});
207
????????button2.addActionListener(new?ActionListener()
{
208
????????????public?void?actionPerformed(ActionEvent?e)
{
209
????????????????try
{
210
????????????????????money?=?Integer.parseInt(JOptionPane.showInputDialog(null,"請輸入您要取得的金額:"));
211
????????????????}catch(HeadlessException?o)
{}
212
????????????????zhanghu.getM(money);
213
????????????????infile(zhanghu);
214
????????????}
215
????????});
216
????????button3.addActionListener(new?ActionListener()
217
????????
{
218
????????????public?void?actionPerformed(ActionEvent?e)
{
219
????????????????try
{
220
????????????????????while(true)
{
221
????????????????????????toname?=?JOptionPane.showInputDialog(null,"請輸入你要轉入的帳戶:");
222
????????????????????????fl?=?new?File(toname+".txt");
223
????????????????????????//判斷帳戶是否存在
224
????????????????????????if(!fl.exists())
{
225
????????????????????????????System.out.println("對不起,您輸入的帳戶并不存在,請重新輸入:");
226
????????????????????????????continue;
227
????????????????????????}
228
????????????????????????else
{
229
????????????????????????????break;
230
????????????????????????}
231
????????????????????}
232
????????????????????money2?=?Integer.parseInt(JOptionPane.showInputDialog(null,"請輸入你要轉入的金額:"));
233
????????????????????zhanghu.getM(money2);
234
????????????????????infile(zhanghu);
235
????????????????????BufferedReader?reader?=?new?BufferedReader(new?FileReader(?toname?+?".txt"));
236
????????????????????password?=?reader.readLine();
237
????????????????????money3?=?Integer.parseInt(reader.readLine());
238
????????????????????zhanghu2?=?new?ZhangHu(toname,password,money3);
239
????????????????????zhanghu2.setM(money2);
240
????????????????????infile(zhanghu2);
241
????????????????}catch(HeadlessException?o)
{}catch(IOException?p)
{}
242
????????????}
243
????????});
244
????????button4.addActionListener(new?ActionListener()
245
????????
{
246
????????????public?void?actionPerformed(ActionEvent?e)
247
????????????
{
248
????????????????System.exit(0);
249
????????????}
250
????????});
251
????????panel.setLayout(new?GridLayout(6,1,50,5));
252
????????panel.add(label1);
253
????????panel.add(label2);
254
????????panel.add(button1);
255
????????panel.add(button2);
256
????????panel.add(button3);
257
????????panel.add(button4);
258
259
????????c.add(panel);
260
????????setSize(400,300);
261
????????setLocation(300,200);
262
????????setVisible(true);
263
????}
264
????public?void?infile(ZhangHu?p)
{
265
????????try
{
266
????????????PrintWriter?writer?=?new?PrintWriter(new?BufferedWriter(new?FileWriter(p.getName()+".txt")));
267
????????????writer.println(p.getPassword());
268
????????????writer.println(p.getMoney());
269
????????????writer.flush()?;
270
????????}catch(IOException?e)
{}
271
????}
272
}
273
274
//定義帳戶類
275
class?ZhangHu?
{
276
????private?String?name;
277
????private?String?password;
278
????private?int?money;
279
????ZhangHu()
{
280
????????
281
????}
282
????ZhangHu(String?name,String?password,?int?money)
{
283
????????this.name?=?name;
284
????????this.password?=?password;
285
????????this.money?=?money;
286
????}
287
????public?void?setM(int?a)
{
288
????????money?=?money?+?a;
289
????????try
{
290
????????????JOptionPane.showMessageDialog(null,"存儲了"+a+"元,帳戶"+name+"尚有余額"+money+"元");
291
????????}catch(HeadlessException?E)
{}
292
????}
293
????public?void?getM(int?a)
{
294
????????if(a?>?money)
{
295
????????????try
{
296
????????????????JOptionPane.showMessageDialog(null,"對不起,您的金額不足"+a+"元");
297
????????????}catch(HeadlessException?E)
{}
298
????????}
299
????????else
{
300
????????????money?=?money?-?a;
301
????????????try
{
302
????????????????JOptionPane.showMessageDialog(null,"取得了"+a+"元,帳戶"+name+"尚有余額"+money+"元");
303
????????????}catch(HeadlessException?E)
{}
304
????????}
305
????}
306
????public?String?getName()
{
307
????????return?name;
308
????}
309
????public?String?getPassword()
{
310
????????return?password;
311
????}
312
????public?int?getMoney()
{
313
????????return?money;
314
????}
315
????public?void?setName(String?name)
{
316
????????this.name?=?name;
317
????}
318
????public?void?setPassword(String?password)
{
319
????????this.password?=?password;
320
????}
321
????public?void?setMoney(int?money)
{
322
????????this.money?=?money;
323
????}
324
}
325
326
//定義管理員類
327
class?Manager
328

{
329
????public?void?add()
{
330
????????String?pw;
331
????????ZhangHu?zh?=?new?ZhangHu();
332
????????try
{
333
????????????zh.setName(JOptionPane.showInputDialog(null,"請輸入您要添加的帳戶名:"));
334
????????????zh.setPassword(JOptionPane.showInputDialog(null,"請輸入您要設置的密碼:"));
335
????????????while(true)
{
336
????????????????pw?=?JOptionPane.showInputDialog(null,"請再次輸入密碼:");
337
????????????????if?(?zh.getPassword().equals(pw))
{
338
????????????????????break;
339
????????????????}
340
????????????????else
{
341
????????????????????zh.setPassword(JOptionPane.showInputDialog(null,"兩次輸入的密碼不一致,請重新輸入密碼:"));
342
????????????????????continue;
343
????????????????}
344
????????????}
345
????????????zh.setMoney(Integer.parseInt(JOptionPane.showInputDialog(null,"請輸入您的金額:")));
346
????????????PrintWriter?writer?=?new?PrintWriter(new?BufferedWriter(new?FileWriter(zh.getName()+".txt")));//創建文件
347
????????????writer.println(zh.getPassword());
348
????????????writer.println(zh.getMoney());
349
????????????writer.flush()?;
350
????????????JOptionPane.showMessageDialog(null,"帳戶"+zh.getName()+"已經創建成功");
351
????????}catch(HeadlessException?e)
{}catch(IOException?c)
{}
352
????}
353
????public?void?del()
{
354
????????String?name?=?null;
355
????????try
{
356
????????????name?=?(JOptionPane.showInputDialog(null,"請輸入您要刪除的帳戶名:"));
357
????????????while(true)
{
358
????????????????File?fl?=?new?File(name+".txt");
359
????????????????if(fl.exists())
{
360
????????????????????fl.delete();
361
????????????????????JOptionPane.showMessageDialog(null,"帳戶"+name+"刪除成功");
362
????????????????????break;
363
????????????????}
364
????????????????else
{
365
????????????????????name?=?JOptionPane.showInputDialog(null,"您所輸入的帳戶并不存在,請重新輸入:");
366
????????????????}
367
????????????}????
368
????????}catch(HeadlessException?e)
{}
369
????}
370
????public?void?froze()
{
371
????????ZhangHu?zh?=?new?ZhangHu();
372
????????String?name?=?null;????????
373
????????name?=?(JOptionPane.showInputDialog(null,"請輸入您要凍結的帳戶:"));
374
????????try
{
375
????????????while(true)
{
376
????????????????File?fl?=?new?File(name+".txt");
377
????????????????if?(fl.exists())
{//如果該帳戶存在,則讀取該帳戶的數據
378
????????????????????BufferedReader?reader?=?new?BufferedReader(new?FileReader(?name?+?".txt"));
379
????????????????????zh.setPassword(reader.readLine());
380
????????????????????zh.setMoney(Integer.parseInt(reader.readLine()));
381
????????????????????File?fr?=?new?File("frozen");
382
????????????????????if(!fr.exists())
{
383
????????????????????????fr.mkdir();//創建一個此目錄的文件夾
384
????????????????????}
385
????????????????????File?frfl?=?new?File(fr,name+".txt");
386
????????????????????PrintWriter?writer?=?new?PrintWriter(new?BufferedWriter(new?FileWriter(frfl)));
387
????????????????????writer.println(zh.getPassword());
388
????????????????????writer.println(zh.getMoney());
389
????????????????????writer.flush()?;
390
????????????????????fl.delete();
391
????????????????????JOptionPane.showMessageDialog(null,"帳戶"+name+"凍結成功!");
392
????????????????????break;
393
????????????????}
394
????????????????else
{
395
????????????????????name?=?JOptionPane.showInputDialog(null,"您所輸入的帳戶并不存在,請重新輸入:");
396
????????????????}????
397
????????????}
398
????????}catch(SecurityException?e)
{}catch(HeadlessException?e)
{}catch(IOException?e)
{}
399
????}
400
}
401
402
//自定義面扳類
403
class?MyPanel?extends?JPanel
404

{
405
????public?Insets?insets()
406
????
{
407
????????return?new?Insets(40,40,40,40);
408
????}
409
}
410
411
//主類
412
public?class?Bank_Test?
{
413
????public?static?void?main(String[]?args)
{
414
????????Login?lg?=?new?Login();
415
????????lg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
416
????}????
417
}


??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

115



116

117

118

119

120

121

122

123

124

125

126

127

128

129

130



131



132

133

134

135

136



137



138

139

140

141

142

143



144



145

146

147

148

149

150



151

152



153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173



174

175

176

177

178

179

180

181

182

183



184

185

186

187

188

189

190

191

192

193

194

195

196

197

198



199



200



201

202



203

204

205

206

207



208



209



210

211



212

213

214

215

216

217



218



219



220



221

222

223

224



225

226

227

228



229

230

231

232

233

234

235

236

237

238

239

240

241




242

243

244

245



246

247



248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264



265



266

267

268

269

270



271

272

273

274

275



276

277

278

279



280

281

282



283

284

285

286

287



288

289



290

291



292

293



294



295



296

297



298

299



300

301



302

303



304

305

306



307

308

309



310

311

312



313

314

315



316

317

318



319

320

321



322

323

324

325

326

327

328



329



330

331

332



333

334

335



336

337



338

339

340



341

342

343

344

345

346

347

348

349

350

351




352

353



354

355



356

357



358

359



360

361

362

363

364



365

366

367

368



369

370



371

372

373

374



375



376

377



378

379

380

381

382



383

384

385

386

387

388

389

390

391

392

393

394



395

396

397

398





399

400

401

402

403

404



405

406



407

408

409

410

411

412



413



414

415

416

417

歡迎來訪!^.^!
本BLOG僅用于個人學習交流!
目的在于記錄個人成長.
所有文字均屬于個人理解.
如有錯誤,望多多指教!不勝感激!