Java2实用教程(第二版)程序代码——第十五章 建立窗口和菜单
1
//例子1
2
import java.awt.*;import java.awt.event.*;
3
class MyFrame extends Frame implements ItemListener,ActionListener
4
{ Checkbox box; TextArea text; Button button;
5
MyFrame(String s)
6
{ super(s);
7
box=new Checkbox("设置窗口是否可调整大小");
8
text=new TextArea(12,12);
9
button=new Button("关闭窗口");
10
button.addActionListener(this);
11
box.addItemListener(this);
12
setBounds(100,100,200,300);
13
setVisible(true);
14
add(text,BorderLayout.CENTER);
15
add(box,BorderLayout.SOUTH);
16
add(button,BorderLayout.NORTH);
17
setResizable(false);
18
validate();
19
}
20
public void itemStateChanged(ItemEvent e)
21
{ if(box.getState()==true)
22
{ setResizable(true);
23
}
24
else
25
{ setResizable(false);
26
}
27
}
28
public void actionPerformed(ActionEvent e)
29
{ dispose();
30
}
31
}
32
class Example15_1
33
{ public static void main(String args[])
34
{ new MyFrame("窗口");
35
}
36
}
37![]()
38
//例子2
39
import java.awt.*;import java.awt.event.*;
40
class MyFrame extends Frame implements ItemListener,ActionListener
41
{ Checkbox box; Button button;
42
Toolkit tool; Dimension dim;
43
MyFrame(String s)
44
{ super(s);
45
box=new Checkbox("设置窗口和屏幕同样大小");
46
add(box,BorderLayout.SOUTH);
47
button=new Button("关闭窗口"); button.addActionListener(this);
48
box.addItemListener(this);
49
setBounds(100,100,200,300); setVisible(true);
50
add(box,BorderLayout.SOUTH); add(button,BorderLayout.NORTH);
51
tool=getToolkit();
52
validate();
53
}
54
public void itemStateChanged(ItemEvent e)
55
{ if(box.getState()==true)
56
{ dim=tool.getScreenSize();
57
setBounds(0,0,dim.width,dim.height);
58
validate();
59
}
60
else
61
{ setBounds(0,0,dim.width,80);
62
validate();
63
}
64
}
65
public void actionPerformed(ActionEvent e)
66
{ dispose();
67
}
68
}
69
class Example15_2
70
{ public static void main(String args[])
71
{new MyFrame("窗口");
72
}
73
}
74![]()
75
//例子3
76
import java.awt.*;import java.awt.event.*;
77
class 圆 extends Panel implements ActionListener//负责计算圆面积的类。
78
{ double r,area;
79
TextField 半径=null,
80
结果=null;
81
Button b=null;
82
圆()
83
{ 半径=new TextField(10);
84
结果=new TextField(10);
85
b=new Button("确定");
86
add(new Label("输入半径"));
87
add(半径);
88
add(new Label("面积是:"));
89
add(结果); add(b);
90
b.addActionListener(this);
91
}
92
public void actionPerformed(ActionEvent e)
93
{ try
94
{ r=Double.parseDouble(半径.getText());
95
area=Math.PI*r*r;
96
结果.setText(""+area);
97
}
98
catch(Exception ee)
99
{ 半径.setText("请输入数字字符");
100
}
101
}
102
}
103
class 三角形 extends Panel implements ActionListener//负责计算三角形面积的类。
104
{ double a=0,b=0,c=0,area;
105
TextField 边_a=new TextField(6),
106
边_b=new TextField(6),
107
边_c=new TextField(6),
108
结果=new TextField(24);
109
Button button=new Button("确定");
110
三角形()
111
{ add(new Label("输入三边的长度:"));
112
add(边_a); add(边_b); add(边_c);
113
add(new Label("面积是:"));
114
add(结果); add(button);
115
button.addActionListener(this);
116
}
117
public void actionPerformed(ActionEvent e)//获取三边的长度。
118
{ try{ a=Double.parseDouble(边_a.getText());
119
b=Double.parseDouble(边_b.getText());
120
c=Double.parseDouble(边_c.getText());
121
if(a+b>c&&a+c>b&&c+b>a)
122
{ double p=(a+b+c)/2;
123
area=Math.sqrt(p*(p-a)*(p-b)*(p-c));//计算三角形的面积。
124
结果.setText(""+area);
125
}
126
else
127
{ 结果.setText("您输入的数字不能形成三角形");
128
}
129
}
130
catch(Exception ee)
131
{ 结果.setText("请输入数字字符");
132
}
133
}
134
}
135
class Win extends Frame implements ActionListener
136
{ MenuBar bar=null; Menu menu=null;
137
MenuItem item1, item2;
138
圆 circle ;
139
三角形 trangle;
140
Win()
141
{ bar=new MenuBar(); menu=new Menu("选择");
142
item1=new MenuItem("圆面积计算"); item2=new MenuItem("三角形面积计算");
143
menu.add(item1); menu.add(item2);
144
bar.add(menu);
145
setMenuBar(bar);
146
circle=new 圆();
147
trangle=new 三角形(); //创建一个圆和一个三角形。
148
item1.addActionListener(this); item2.addActionListener(this);
149
setVisible(true); setBounds(100,120,100,90);
150
}
151
public void actionPerformed(ActionEvent e)
152
{ if(e.getSource()==item1)
153
{ removeAll();
154
add(circle,"Center");//添加圆面积计算的界面。
155
validate();
156
}
157
else if(e.getSource()==item2)
158
{ removeAll();
159
add(trangle,"Center");//添加三角形面积计算的界面。
160
validate();
161
}
162
}
163
}
164
public class Example15_3
165
{ public static void main(String args[])
166
{ Win win=new Win();win.setBounds(100,100,200,100);win.setVisible(true);
167
win.addWindowListener(new WindowAdapter()
168
{ public void windowClosing(WindowEvent e)
169
{ System.exit(0);
170
}
171
});
172
}
173
}
174![]()
175
//例子4
176
import java.awt.*;import java.awt.event.*;
177
class Herwindow extends Frame implements ActionListener
178
{ MenuBar menubar;Menu menu;MenuItem item;
179
MenuShortcut shortcut=new MenuShortcut(KeyEvent.VK_E);
180
Herwindow(String s)
181
{ super(s);
182
setSize(160,170);setVisible(true);
183
menubar=new MenuBar(); menu=new Menu("文件");
184
item=new MenuItem("退出");
185
item.setShortcut(shortcut); //设置菜单选项的键盘快捷键。
186
item.addActionListener(this);
187
menu.add(item);
188
menubar.add(menu);menubar.add(menu);
189
setMenuBar(menubar);
190
}
191
public void actionPerformed(ActionEvent e)
192
{ if(e.getSource()==item)
193
{ System.exit(0);
194
}
195
}
196
}
197
public class Example15_4
198
{ public static void main(String args[])
199
{ Herwindow window=new Herwindow("法制之窗");
200
}
201
}
202![]()
203
//例子5
204
import java.awt.*;import java.awt.event.*;
205
class MyFrame extends Frame implements WindowListener
206
{ TextArea text;
207
MyFrame(String s)
208
{ super(s);
209
setBounds(100,100,200,300);
210
setVisible(true);
211
text=new TextArea();
212
add(text,BorderLayout.CENTER);
213
addWindowListener(this);
214
validate();
215
}
216
public void windowActivated(WindowEvent e)
217
{ text.append("\n我被激活");
218
validate();
219
}
220
public void windowDeactivated(WindowEvent e)
221
{ text.append("\n我不是激活状态了");
222
setBounds(0,0,400,400); validate();
223
}
224
public void windowClosing(WindowEvent e)
225
{ text.append("\n窗口正在关闭呢"); dispose();
226
}
227
public void windowClosed(WindowEvent e)
228
{ System.out.println("程序结束运行");
229
System.exit(0);
230
}
231
public void windowIconified(WindowEvent e)
232
{ text.append("\n我图标化了");
233
}
234
public void windowDeiconified(WindowEvent e)
235
{ text.append("\n我撤消图标化");
236
setBounds(0,0,400,400);validate();
237
}
238
public void windowOpened(WindowEvent e){ }
239
}
240
class Example15_5
241
{ ublic static void main(String args[])
242
{ new MyFrame("窗口");
243
}
244
}
245![]()
246
//例子6
247
import java.awt.*;import java.awt.event.*;
248
class MyFrame extends Frame
249
{ TextArea text; Boy police;
250
MyFrame(String s)
251
{ super(s);
252
police=new Boy(this);
253
setBounds(100,100,200,300); setVisible(true);
254
text=new TextArea(); add(text,BorderLayout.CENTER);
255
addWindowListener(police); validate();
256
}
257
}
258
class Boy extends WindowAdapter
259
{ MyFrame f;
260
public Boy(MyFrame f)
261
{ this.f=f;
262
}
263
public void windowActivated(WindowEvent e)
264
{ f.text.append("\n我被激活");
265
}
266
public void windowClosing(WindowEvent e)
267
{ System.exit(0);
268
}
269
}
270
class Example15_6
271
{ public static void main(String args[])
272
{ new MyFrame("窗口");
273
}
274
}
275![]()
276
//例子7
277
import java.awt.*;import java.awt.event.*;
278
class MyFrame extends Frame
279
{ TextArea text;
280
MyFrame(String s)
281
{ super(s);
282
setBounds(100,100,200,300);setVisible(true);
283
text=new TextArea(); add(text,BorderLayout.CENTER);
284
addWindowListener(new WindowAdapter()
285
{ public void windowActivated(WindowEvent e)
286
{ text.append("\n我被激活");
287
}
288
public void windowClosing(WindowEvent e)
289
{ System.exit(0);
290
}
291
}
292
);
293
validate();
294
}
295
}
296
class Example15_7
297
{ public static void main(String args[])
298
{ new MyFrame("窗口");
299
}
300
}
301![]()
302
//例子8
303
import java.applet.*;import java.awt.*;import java.awt.event.*;
304
public class Example15_8 extends Applet
305
{ Frame f,g;
306
public void init()
307
{ f=new Frame("音乐之窗");g=new Frame("体育之窗");
308
f.setSize(150,150); f.setVisible(true);
309
g.setSize(200,200); g.setVisible(false);
310
f.addWindowListener(new WindowAdapter()
311
{ public void windowClosing(WindowEvent e)
312
{ f.setVisible(false);
313
g.setVisible(true);
314
}
315
} ); //适配器
316
g.addWindowListener(new WindowAdapter()
317
{public void windowClosing(WindowEvent e)
318
{ g.setVisible(false);
319
f.setVisible(true);
320
}
321
} );
322
}
323
}
324![]()
325
//例子9
326
import java.awt.*;import java.awt.event.*;
327
public class Example15_9
328
{ public static void main(String args[])
329
{ MyFrame f=new MyFrame();
330
f.setBounds(70,70,70,89);f.setVisible(true);f.pack();
331
}
332
}
333
class MyFrame extends Frame implements ActionListener
334
{ PrintJob p=null; //声明一个PrintJob对象。
335
Graphics g=null;
336
TextArea text=new TextArea(10,10);
337
Button 打印文本框=new Button("打印文本框"),
338
打印窗口=new Button("打印窗口"),
339
打印按扭=new Button("打印按扭");
340
MyFrame()
341
{ super("在应用程序中打印");
342
打印文本框.addActionListener(this);
343
打印窗口.addActionListener(this);
344
打印按扭.addActionListener(this);
345
add(text,"Center");
346
Panel panel=new Panel();
347
panel.add(打印文本框); panel.add(打印窗口); panel.add(打印按扭);
348
add(panel,"South");
349
addWindowListener(new WindowAdapter()
350
{public void windowClosing(WindowEvent e)
351
{System.exit(0); }
352
});
353
}
354
public void actionPerformed(ActionEvent e)
355
{ if(e.getSource()==打印文本框)
356
{ p=getToolkit().getPrintJob(this,"ok",null);
357
//创建一个PrintJob对象p 。
358
g=p.getGraphics(); //p获取一个用于打印的 Graphics对象。
359
g.translate(120,200);
360
text.printAll(g);
361
g.dispose(); //释放对象 g。
362
p.end();
363
}
364
else if(e.getSource()==打印窗口)
365
{ p=getToolkit().getPrintJob(this,"ok",null);
366
g=p.getGraphics(); //p获取一个用于打印的 Graphics对象。
367
g.translate(120,200);
368
this.printAll(g); //打印当前窗口及其子组件。
369
g.dispose(); //释放对象 g。
370
p.end();
371
}
372
else if(e.getSource()==打印按扭)
373
{ p=getToolkit().getPrintJob(this,"ok",null);
374
g=p.getGraphics();
375
g.translate(120,200); //在打印页的坐标(120,200)处打印第一个"按扭"。
376
打印文本框.printAll(g);
377
g.translate(78,0); //在打印页的坐标(198,200)处打印第二个"按扭"。
378
打印窗口.printAll(g);
379
g.translate(66,0); //在打印页的坐标(264,200)处打印第三个"按扭"。
380
打印按扭.printAll(g);
381
g.dispose();
382
p.end();
383
}
384
}
385
}
386![]()
387
//例子10
388
import java.awt.*;import java.awt.event.*;
389
import java.awt.datatransfer.*;
390
public class Example15_10 extends Frame implements ActionListener
391
{ MenuBar menubar; Menu menu;
392
MenuItem copy,cut,paste;
393
TextArea text1,text2;
394
Clipboard clipboard=null;
395
Example15_10()
396
{ clipboard=getToolkit().getSystemClipboard();//获取系统剪贴板。
397
menubar=new MenuBar();
398
menu=new Menu("Edit"); copy=new MenuItem("copy");
399
cut=new MenuItem ("cut"); paste=new MenuItem ("paste");
400
text1=new TextArea(20,20); text2=new TextArea(20,20);
401
copy.addActionListener(this); cut.addActionListener(this);
402
paste.addActionListener(this);
403
setLayout(new FlowLayout());
404
menubar.add(menu);
405
menu.add(copy); menu.add(cut); menu.add(paste);
406
setMenuBar(menubar);
407
add(text1);add(text2);
408
setBounds(100,100,200,250); setVisible(true);pack();
409
addWindowListener(new WindowAdapter()
410
{public void windowClosing(WindowEvent e)
411
{System.exit(0);
412
}
413
}) ;
414
}
415
public void actionPerformed(ActionEvent e)
416
{ if(e.getSource()==copy) //拷贝到剪贴板。
417
{ String temp=text1.getSelectedText(); //拖动鼠标选取文本。
418
StringSelection text=new StringSelection(temp);
419
clipboard.setContents(text,null);
420
}
421
else if(e.getSource()==cut) //剪贴到剪贴板。
422
{ String temp=text1.getSelectedText(); //拖动鼠标选取文本。
423
StringSelection text=new StringSelection(temp);
424
clipboard.setContents(text,null);
425
int start=text1.getSelectionStart();
426
int end =text1.getSelectionEnd();
427
text1.replaceRange("",start,end) ; //从Text1中删除被选取的文本。
428
}
429
else if(e.getSource()==paste) //从剪贴板粘贴数据。
430
{ Transferable contents=clipboard.getContents(this);
431
DataFlavor flavor= DataFlavor.stringFlavor;
432
if( contents.isDataFlavorSupported(flavor))
433
try{ String str;
434
str=(String)contents.getTransferData(flavor);
435
text2.append(str);
436
}
437
catch(Exception ee){}
438
}
439
}
440
public static void main(String args[])
441
{ Example15_10 win=new Example15_10 ();
442
}
443
}
444![]()
//例子12
import java.awt.*;import java.awt.event.*;3
class MyFrame extends Frame implements ItemListener,ActionListener4
{ Checkbox box; TextArea text; Button button;5
MyFrame(String s)6
{ super(s);7
box=new Checkbox("设置窗口是否可调整大小");8
text=new TextArea(12,12);9
button=new Button("关闭窗口");10
button.addActionListener(this); 11
box.addItemListener(this);12
setBounds(100,100,200,300);13
setVisible(true);14
add(text,BorderLayout.CENTER);15
add(box,BorderLayout.SOUTH);16
add(button,BorderLayout.NORTH);17
setResizable(false);18
validate();19
} 20
public void itemStateChanged(ItemEvent e)21
{ if(box.getState()==true)22
{ setResizable(true);23
}24
else25
{ setResizable(false);26
}27
}28
public void actionPerformed(ActionEvent e)29
{ dispose();30
}31
}32
class Example15_133
{ public static void main(String args[])34
{ new MyFrame("窗口");35
} 36
}37

38
//例子239
import java.awt.*;import java.awt.event.*;40
class MyFrame extends Frame implements ItemListener,ActionListener41
{ Checkbox box; Button button;42
Toolkit tool; Dimension dim;43
MyFrame(String s)44
{ super(s);45
box=new Checkbox("设置窗口和屏幕同样大小");46
add(box,BorderLayout.SOUTH);47
button=new Button("关闭窗口"); button.addActionListener(this);48
box.addItemListener(this);49
setBounds(100,100,200,300); setVisible(true);50
add(box,BorderLayout.SOUTH); add(button,BorderLayout.NORTH);51
tool=getToolkit();52
validate();53
} 54
public void itemStateChanged(ItemEvent e)55
{ if(box.getState()==true)56
{ dim=tool.getScreenSize();57
setBounds(0,0,dim.width,dim.height);58
validate(); 59
}60
else61
{ setBounds(0,0,dim.width,80);62
validate();63
}64
}65
public void actionPerformed(ActionEvent e)66
{ dispose();67
}68
}69
class Example15_2 70
{ public static void main(String args[])71
{new MyFrame("窗口");72
} 73
}74

75
//例子376
import java.awt.*;import java.awt.event.*;77
class 圆 extends Panel implements ActionListener//负责计算圆面积的类。78
{ double r,area;79
TextField 半径=null,80
结果=null;81
Button b=null;82
圆()83
{ 半径=new TextField(10);84
结果=new TextField(10);85
b=new Button("确定");86
add(new Label("输入半径"));87
add(半径);88
add(new Label("面积是:"));89
add(结果); add(b);90
b.addActionListener(this);91
} 92
public void actionPerformed(ActionEvent e)93
{ try 94
{ r=Double.parseDouble(半径.getText());95
area=Math.PI*r*r; 96
结果.setText(""+area);97
}98
catch(Exception ee)99
{ 半径.setText("请输入数字字符");100
}101
}102
}103
class 三角形 extends Panel implements ActionListener//负责计算三角形面积的类。104
{ double a=0,b=0,c=0,area;105
TextField 边_a=new TextField(6),106
边_b=new TextField(6),107
边_c=new TextField(6),108
结果=new TextField(24);109
Button button=new Button("确定");110
三角形()111
{ add(new Label("输入三边的长度:"));112
add(边_a); add(边_b); add(边_c);113
add(new Label("面积是:"));114
add(结果); add(button);115
button.addActionListener(this);116
} 117
public void actionPerformed(ActionEvent e)//获取三边的长度。118
{ try{ a=Double.parseDouble(边_a.getText());119
b=Double.parseDouble(边_b.getText());120
c=Double.parseDouble(边_c.getText());121
if(a+b>c&&a+c>b&&c+b>a)122
{ double p=(a+b+c)/2;123
area=Math.sqrt(p*(p-a)*(p-b)*(p-c));//计算三角形的面积。124
结果.setText(""+area);125
}126
else127
{ 结果.setText("您输入的数字不能形成三角形");128
}129
}130
catch(Exception ee)131
{ 结果.setText("请输入数字字符");132
}133
}134
}135
class Win extends Frame implements ActionListener136
{ MenuBar bar=null; Menu menu=null;137
MenuItem item1, item2;138
圆 circle ;139
三角形 trangle;140
Win() 141
{ bar=new MenuBar(); menu=new Menu("选择"); 142
item1=new MenuItem("圆面积计算"); item2=new MenuItem("三角形面积计算");143
menu.add(item1); menu.add(item2);144
bar.add(menu);145
setMenuBar(bar);146
circle=new 圆();147
trangle=new 三角形(); //创建一个圆和一个三角形。148
item1.addActionListener(this); item2.addActionListener(this);149
setVisible(true); setBounds(100,120,100,90);150
}151
public void actionPerformed(ActionEvent e)152
{ if(e.getSource()==item1) 153
{ removeAll();154
add(circle,"Center");//添加圆面积计算的界面。155
validate();156
}157
else if(e.getSource()==item2) 158
{ removeAll();159
add(trangle,"Center");//添加三角形面积计算的界面。160
validate();161
}162
}163
}164
public class Example15_3165
{ public static void main(String args[]) 166
{ Win win=new Win();win.setBounds(100,100,200,100);win.setVisible(true); 167
win.addWindowListener(new WindowAdapter() 168
{ public void windowClosing(WindowEvent e)169
{ System.exit(0); 170
}171
});172
}173
}174

175
//例子4176
import java.awt.*;import java.awt.event.*;177
class Herwindow extends Frame implements ActionListener 178
{ MenuBar menubar;Menu menu;MenuItem item; 179
MenuShortcut shortcut=new MenuShortcut(KeyEvent.VK_E);180
Herwindow(String s) 181
{ super(s); 182
setSize(160,170);setVisible(true); 183
menubar=new MenuBar(); menu=new Menu("文件"); 184
item=new MenuItem("退出");185
item.setShortcut(shortcut); //设置菜单选项的键盘快捷键。 186
item.addActionListener(this);187
menu.add(item);188
menubar.add(menu);menubar.add(menu); 189
setMenuBar(menubar); 190
} 191
public void actionPerformed(ActionEvent e) 192
{ if(e.getSource()==item) 193
{ System.exit(0); 194
}195
}196
}197
public class Example15_4 198
{ public static void main(String args[])199
{ Herwindow window=new Herwindow("法制之窗");200
}201
} 202

203
//例子5204
import java.awt.*;import java.awt.event.*;205
class MyFrame extends Frame implements WindowListener 206
{ TextArea text;207
MyFrame(String s)208
{ super(s);209
setBounds(100,100,200,300);210
setVisible(true);211
text=new TextArea();212
add(text,BorderLayout.CENTER); 213
addWindowListener(this);214
validate();215
} 216
public void windowActivated(WindowEvent e)217
{ text.append("\n我被激活");218
validate();219
}220
public void windowDeactivated(WindowEvent e)221
{ text.append("\n我不是激活状态了");222
setBounds(0,0,400,400); validate();223
}224
public void windowClosing(WindowEvent e)225
{ text.append("\n窗口正在关闭呢"); dispose();226
}227
public void windowClosed(WindowEvent e)228
{ System.out.println("程序结束运行"); 229
System.exit(0);230
}231
public void windowIconified(WindowEvent e)232
{ text.append("\n我图标化了");233
}234
public void windowDeiconified(WindowEvent e)235
{ text.append("\n我撤消图标化"); 236
setBounds(0,0,400,400);validate();237
}238
public void windowOpened(WindowEvent e){ }239
}240
class Example15_5241
{ ublic static void main(String args[])242
{ new MyFrame("窗口");243
}244
}245

246
//例子6247
import java.awt.*;import java.awt.event.*;248
class MyFrame extends Frame 249
{ TextArea text; Boy police;250
MyFrame(String s)251
{ super(s);252
police=new Boy(this);253
setBounds(100,100,200,300); setVisible(true);254
text=new TextArea(); add(text,BorderLayout.CENTER);255
addWindowListener(police); validate();256
} 257
}258
class Boy extends WindowAdapter 259
{ MyFrame f;260
public Boy(MyFrame f)261
{ this.f=f;262
}263
public void windowActivated(WindowEvent e) 264
{ f.text.append("\n我被激活");265
}266
public void windowClosing(WindowEvent e)267
{ System.exit(0);268
}269
}270
class Example15_6271
{ public static void main(String args[])272
{ new MyFrame("窗口");273
} 274
}275

276
//例子7277
import java.awt.*;import java.awt.event.*;278
class MyFrame extends Frame 279
{ TextArea text;280
MyFrame(String s) 281
{ super(s);282
setBounds(100,100,200,300);setVisible(true);283
text=new TextArea(); add(text,BorderLayout.CENTER);284
addWindowListener(new WindowAdapter()285
{ public void windowActivated(WindowEvent e)286
{ text.append("\n我被激活");287
}288
public void windowClosing(WindowEvent e)289
{ System.exit(0);290
}291
}292
);293
validate();294
}295
}296
class Example15_7297
{ public static void main(String args[]) 298
{ new MyFrame("窗口");299
}300
}301

302
//例子8303
import java.applet.*;import java.awt.*;import java.awt.event.*;304
public class Example15_8 extends Applet305
{ Frame f,g;306
public void init()307
{ f=new Frame("音乐之窗");g=new Frame("体育之窗");308
f.setSize(150,150); f.setVisible(true);309
g.setSize(200,200); g.setVisible(false);310
f.addWindowListener(new WindowAdapter()311
{ public void windowClosing(WindowEvent e)312
{ f.setVisible(false);313
g.setVisible(true);314
} 315
} ); //适配器316
g.addWindowListener(new WindowAdapter()317
{public void windowClosing(WindowEvent e)318
{ g.setVisible(false);319
f.setVisible(true);320
}321
} );322
}323
}324

325
//例子9 326
import java.awt.*;import java.awt.event.*;327
public class Example15_9328
{ public static void main(String args[])329
{ MyFrame f=new MyFrame();330
f.setBounds(70,70,70,89);f.setVisible(true);f.pack();331
}332
}333
class MyFrame extends Frame implements ActionListener334
{ PrintJob p=null; //声明一个PrintJob对象。335
Graphics g=null;336
TextArea text=new TextArea(10,10);337
Button 打印文本框=new Button("打印文本框"),338
打印窗口=new Button("打印窗口"),339
打印按扭=new Button("打印按扭");340
MyFrame()341
{ super("在应用程序中打印"); 342
打印文本框.addActionListener(this);343
打印窗口.addActionListener(this);344
打印按扭.addActionListener(this);345
add(text,"Center");346
Panel panel=new Panel();347
panel.add(打印文本框); panel.add(打印窗口); panel.add(打印按扭);348
add(panel,"South");349
addWindowListener(new WindowAdapter()350
{public void windowClosing(WindowEvent e)351
{System.exit(0); }352
});353
}354
public void actionPerformed(ActionEvent e)355
{ if(e.getSource()==打印文本框)356
{ p=getToolkit().getPrintJob(this,"ok",null);357
//创建一个PrintJob对象p 。358
g=p.getGraphics(); //p获取一个用于打印的 Graphics对象。359
g.translate(120,200);360
text.printAll(g);361
g.dispose(); //释放对象 g。 362
p.end();363
} 364
else if(e.getSource()==打印窗口)365
{ p=getToolkit().getPrintJob(this,"ok",null);366
g=p.getGraphics(); //p获取一个用于打印的 Graphics对象。367
g.translate(120,200);368
this.printAll(g); //打印当前窗口及其子组件。369
g.dispose(); //释放对象 g。 370
p.end();371
}372
else if(e.getSource()==打印按扭)373
{ p=getToolkit().getPrintJob(this,"ok",null);374
g=p.getGraphics();375
g.translate(120,200); //在打印页的坐标(120,200)处打印第一个"按扭"。376
打印文本框.printAll(g);377
g.translate(78,0); //在打印页的坐标(198,200)处打印第二个"按扭"。378
打印窗口.printAll(g);379
g.translate(66,0); //在打印页的坐标(264,200)处打印第三个"按扭"。380
打印按扭.printAll(g); 381
g.dispose(); 382
p.end();383
}384
}385
}386

387
//例子10388
import java.awt.*;import java.awt.event.*;389
import java.awt.datatransfer.*;390
public class Example15_10 extends Frame implements ActionListener391
{ MenuBar menubar; Menu menu; 392
MenuItem copy,cut,paste;393
TextArea text1,text2;394
Clipboard clipboard=null; 395
Example15_10()396
{ clipboard=getToolkit().getSystemClipboard();//获取系统剪贴板。397
menubar=new MenuBar(); 398
menu=new Menu("Edit"); copy=new MenuItem("copy");399
cut=new MenuItem ("cut"); paste=new MenuItem ("paste");400
text1=new TextArea(20,20); text2=new TextArea(20,20);401
copy.addActionListener(this); cut.addActionListener(this);402
paste.addActionListener(this);403
setLayout(new FlowLayout());404
menubar.add(menu);405
menu.add(copy); menu.add(cut); menu.add(paste); 406
setMenuBar(menubar); 407
add(text1);add(text2);408
setBounds(100,100,200,250); setVisible(true);pack();409
addWindowListener(new WindowAdapter()410
{public void windowClosing(WindowEvent e)411
{System.exit(0);412
}413
}) ;414
}415
public void actionPerformed(ActionEvent e)416
{ if(e.getSource()==copy) //拷贝到剪贴板。417
{ String temp=text1.getSelectedText(); //拖动鼠标选取文本。418
StringSelection text=new StringSelection(temp);419
clipboard.setContents(text,null);420
}421
else if(e.getSource()==cut) //剪贴到剪贴板。422
{ String temp=text1.getSelectedText(); //拖动鼠标选取文本。423
StringSelection text=new StringSelection(temp);424
clipboard.setContents(text,null);425
int start=text1.getSelectionStart();426
int end =text1.getSelectionEnd(); 427
text1.replaceRange("",start,end) ; //从Text1中删除被选取的文本。 428
}429
else if(e.getSource()==paste) //从剪贴板粘贴数据。430
{ Transferable contents=clipboard.getContents(this);431
DataFlavor flavor= DataFlavor.stringFlavor;432
if( contents.isDataFlavorSupported(flavor))433
try{ String str;434
str=(String)contents.getTransferData(flavor);435
text2.append(str);436
}437
catch(Exception ee){}438
}439
}440
public static void main(String args[])441
{ Example15_10 win=new Example15_10 ();442
}443
}444




浙公网安备 33010602011771号