Java2实用教程(第二版)程序代码——第十五章 建立窗口和菜单


1//例子1
2importjava.awt.*;importjava.awt.event.*;
3classMyFrameextendsFrameimplementsItemListener,ActionListener
4{Checkboxbox;TextAreatext;Buttonbutton;
5MyFrame(Strings)
6{super(s);
7box=newCheckbox("设置窗口是否可调整大小");
8text=newTextArea(12,12);
9button=newButton("关闭窗口");
10button.addActionListener(this);
11box.addItemListener(this);
12setBounds(100,100,200,300);
13setVisible(true);
14add(text,BorderLayout.CENTER);
15add(box,BorderLayout.SOUTH);
16add(button,BorderLayout.NORTH);
17setResizable(false);
18validate();
19}

20publicvoiditemStateChanged(ItemEvente)
21{if(box.getState()==true)
22{setResizable(true);
23}

24else
25{setResizable(false);
26}

27}

28publicvoidactionPerformed(ActionEvente)
29{dispose();
30}

31}

32classExample15_1
33{publicstaticvoidmain(Stringargs[])
34{newMyFrame("窗口");
35}

36}

37
38//例子2
39importjava.awt.*;importjava.awt.event.*;
40classMyFrameextendsFrameimplementsItemListener,ActionListener
41{Checkboxbox;Buttonbutton;
42Toolkittool;Dimensiondim;
43MyFrame(Strings)
44{super(s);
45box=newCheckbox("设置窗口和屏幕同样大小");
46add(box,BorderLayout.SOUTH);
47button=newButton("关闭窗口");button.addActionListener(this);
48box.addItemListener(this);
49setBounds(100,100,200,300);setVisible(true);
50add(box,BorderLayout.SOUTH);add(button,BorderLayout.NORTH);
51tool=getToolkit();
52validate();
53}

54publicvoiditemStateChanged(ItemEvente)
55{if(box.getState()==true)
56{dim=tool.getScreenSize();
57setBounds(0,0,dim.width,dim.height);
58validate();
59}

60else
61{setBounds(0,0,dim.width,80);
62validate();
63}

64}

65publicvoidactionPerformed(ActionEvente)
66{dispose();
67}

68}

69classExample15_2
70{publicstaticvoidmain(Stringargs[])
71{newMyFrame("窗口");
72}

73}

74
75//例子3
76importjava.awt.*;importjava.awt.event.*;
77class圆extendsPanelimplementsActionListener//负责计算圆面积的类。
78{doubler,area;
79TextField半径=null,
80结果=null;
81Buttonb=null;
82圆()
83{半径=newTextField(10);
84结果=newTextField(10);
85b=newButton("确定");
86add(newLabel("输入半径"));
87add(半径);
88add(newLabel("面积是:"));
89add(结果);add(b);
90b.addActionListener(this);
91}

92publicvoidactionPerformed(ActionEvente)
93{try
94{r=Double.parseDouble(半径.getText());
95area=Math.PI*r*r;
96结果.setText(""+area);
97}

98catch(Exceptionee)
99{半径.setText("请输入数字字符");
100}

101}

102}

103class三角形extendsPanelimplementsActionListener//负责计算三角形面积的类。
104{doublea=0,b=0,c=0,area;
105TextField边_a=newTextField(6),
106边_b=newTextField(6),
107边_c=newTextField(6),
108结果=newTextField(24);
109Buttonbutton=newButton("确定");
110三角形()
111{add(newLabel("输入三边的长度:"));
112add(边_a);add(边_b);add(边_c);
113add(newLabel("面积是:"));
114add(结果);add(button);
115button.addActionListener(this);
116}

117publicvoidactionPerformed(ActionEvente)//获取三边的长度。
118{try{a=Double.parseDouble(边_a.getText());
119b=Double.parseDouble(边_b.getText());
120c=Double.parseDouble(边_c.getText());
121if(a+b>c&&a+c>b&&c+b>a)
122{doublep=(a+b+c)/2;
123area=Math.sqrt(p*(p-a)*(p-b)*(p-c));//计算三角形的面积。
124结果.setText(""+area);
125}

126else
127{结果.setText("您输入的数字不能形成三角形");
128}

129}

130catch(Exceptionee)
131{结果.setText("请输入数字字符");
132}

133}

134}

135classWinextendsFrameimplementsActionListener
136{MenuBarbar=null;Menumenu=null;
137MenuItemitem1,item2;
138圆circle;
139三角形trangle;
140Win()
141{bar=newMenuBar();menu=newMenu("选择");
142item1=newMenuItem("圆面积计算");item2=newMenuItem("三角形面积计算");
143menu.add(item1);menu.add(item2);
144bar.add(menu);
145setMenuBar(bar);
146circle=new圆();
147trangle=new三角形();//创建一个圆和一个三角形。
148item1.addActionListener(this);item2.addActionListener(this);
149setVisible(true);setBounds(100,120,100,90);
150}

151publicvoidactionPerformed(ActionEvente)
152{if(e.getSource()==item1)
153{removeAll();
154add(circle,"Center");//添加圆面积计算的界面。
155validate();
156}

157elseif(e.getSource()==item2)
158{removeAll();
159add(trangle,"Center");//添加三角形面积计算的界面。
160validate();
161}

162}

163}

164publicclassExample15_3
165{publicstaticvoidmain(Stringargs[])
166{Winwin=newWin();win.setBounds(100,100,200,100);win.setVisible(true);
167win.addWindowListener(newWindowAdapter()
168{publicvoidwindowClosing(WindowEvente)
169{System.exit(0);
170}

171}
);
172}

173}

174
175//例子4
176importjava.awt.*;importjava.awt.event.*;
177classHerwindowextendsFrameimplementsActionListener
178{MenuBarmenubar;Menumenu;MenuItemitem;
179MenuShortcutshortcut=newMenuShortcut(KeyEvent.VK_E);
180Herwindow(Strings)
181{super(s);
182setSize(160,170);setVisible(true);
183menubar=newMenuBar();menu=newMenu("文件");
184item=newMenuItem("退出");
185item.setShortcut(shortcut);//设置菜单选项的键盘快捷键。
186item.addActionListener(this);
187menu.add(item);
188menubar.add(menu);menubar.add(menu);
189setMenuBar(menubar);
190}

191publicvoidactionPerformed(ActionEvente)
192{if(e.getSource()==item)
193{System.exit(0);
194}

195}

196}

197publicclassExample15_4
198{publicstaticvoidmain(Stringargs[])
199{Herwindowwindow=newHerwindow("法制之窗");
200}

201}

202
203//例子5
204importjava.awt.*;importjava.awt.event.*;
205classMyFrameextendsFrameimplementsWindowListener
206{TextAreatext;
207MyFrame(Strings)
208{super(s);
209setBounds(100,100,200,300);
210setVisible(true);
211text=newTextArea();
212add(text,BorderLayout.CENTER);
213addWindowListener(this);
214validate();
215}

216publicvoidwindowActivated(WindowEvente)
217{text.append("\n我被激活");
218validate();
219}

220publicvoidwindowDeactivated(WindowEvente)
221{text.append("\n我不是激活状态了");
222setBounds(0,0,400,400);validate();
223}

224publicvoidwindowClosing(WindowEvente)
225{text.append("\n窗口正在关闭呢");dispose();
226}

227publicvoidwindowClosed(WindowEvente)
228{System.out.println("程序结束运行");
229System.exit(0);
230}

231publicvoidwindowIconified(WindowEvente)
232{text.append("\n我图标化了");
233}

234publicvoidwindowDeiconified(WindowEvente)
235{text.append("\n我撤消图标化");
236setBounds(0,0,400,400);validate();
237}

238publicvoidwindowOpened(WindowEvente){}
239}

240classExample15_5
241{ublicstaticvoidmain(Stringargs[])
242{newMyFrame("窗口");
243}

244}

245
246//例子6
247importjava.awt.*;importjava.awt.event.*;
248classMyFrameextendsFrame
249{TextAreatext;Boypolice;
250MyFrame(Strings)
251{super(s);
252police=newBoy(this);
253setBounds(100,100,200,300);setVisible(true);
254text=newTextArea();add(text,BorderLayout.CENTER);
255addWindowListener(police);validate();
256}

257}

258classBoyextendsWindowAdapter
259{MyFramef;
260publicBoy(MyFramef)
261{this.f=f;
262}

263publicvoidwindowActivated(WindowEvente)
264{f.text.append("\n我被激活");
265}

266publicvoidwindowClosing(WindowEvente)
267{System.exit(0);
268}

269}

270classExample15_6
271{publicstaticvoidmain(Stringargs[])
272{newMyFrame("窗口");
273}

274}

275
276//例子7
277importjava.awt.*;importjava.awt.event.*;
278classMyFrameextendsFrame
279{TextAreatext;
280MyFrame(Strings)
281{super(s);
282setBounds(100,100,200,300);setVisible(true);
283text=newTextArea();add(text,BorderLayout.CENTER);
284addWindowListener(newWindowAdapter()
285{publicvoidwindowActivated(WindowEvente)
286{text.append("\n我被激活");
287}

288publicvoidwindowClosing(WindowEvente)
289{System.exit(0);
290}

291}

292);
293validate();
294}

295}

296classExample15_7
297{publicstaticvoidmain(Stringargs[])
298{newMyFrame("窗口");
299}

300}

301
302//例子8
303importjava.applet.*;importjava.awt.*;importjava.awt.event.*;
304publicclassExample15_8extendsApplet
305{Framef,g;
306publicvoidinit()
307{f=newFrame("音乐之窗");g=newFrame("体育之窗");
308f.setSize(150,150);f.setVisible(true);
309g.setSize(200,200);g.setVisible(false);
310f.addWindowListener(newWindowAdapter()
311{publicvoidwindowClosing(WindowEvente)
312{f.setVisible(false);
313g.setVisible(true);
314}

315}
);//适配器
316g.addWindowListener(newWindowAdapter()
317{publicvoidwindowClosing(WindowEvente)
318{g.setVisible(false);
319f.setVisible(true);
320}

321}
);
322}

323}

324
325//例子9
326importjava.awt.*;importjava.awt.event.*;
327publicclassExample15_9
328{publicstaticvoidmain(Stringargs[])
329{MyFramef=newMyFrame();
330f.setBounds(70,70,70,89);f.setVisible(true);f.pack();
331}

332}

333classMyFrameextendsFrameimplementsActionListener
334{PrintJobp=null;//声明一个PrintJob对象。
335Graphicsg=null;
336TextAreatext=newTextArea(10,10);
337Button打印文本框=newButton("打印文本框"),
338打印窗口=newButton("打印窗口"),
339打印按扭=newButton("打印按扭");
340MyFrame()
341{super("在应用程序中打印");
342打印文本框.addActionListener(this);
343打印窗口.addActionListener(this);
344打印按扭.addActionListener(this);
345add(text,"Center");
346Panelpanel=newPanel();
347panel.add(打印文本框);panel.add(打印窗口);panel.add(打印按扭);
348add(panel,"South");
349addWindowListener(newWindowAdapter()
350{publicvoidwindowClosing(WindowEvente)
351{System.exit(0);}
352}
);
353}

354publicvoidactionPerformed(ActionEvente)
355{if(e.getSource()==打印文本框)
356{p=getToolkit().getPrintJob(this,"ok",null);
357//创建一个PrintJob对象p。
358g=p.getGraphics();//p获取一个用于打印的Graphics对象。
359g.translate(120,200);
360text.printAll(g);
361g.dispose();//释放对象g。
362p.end();
363}

364elseif(e.getSource()==打印窗口)
365{p=getToolkit().getPrintJob(this,"ok",null);
366g=p.getGraphics();//p获取一个用于打印的Graphics对象。
367g.translate(120,200);
368this.printAll(g);//打印当前窗口及其子组件。
369g.dispose();//释放对象g。
370p.end();
371}

372elseif(e.getSource()==打印按扭)
373{p=getToolkit().getPrintJob(this,"ok",null);
374g=p.getGraphics();
375g.translate(120,200);//在打印页的坐标(120,200)处打印第一个"按扭"。
376打印文本框.printAll(g);
377g.translate(78,0);//在打印页的坐标(198,200)处打印第二个"按扭"。
378打印窗口.printAll(g);
379g.translate(66,0);//在打印页的坐标(264,200)处打印第三个"按扭"。
380打印按扭.printAll(g);
381g.dispose();
382p.end();
383}

384}

385}

386
387//例子10
388importjava.awt.*;importjava.awt.event.*;
389importjava.awt.datatransfer.*;
390publicclassExample15_10extendsFrameimplementsActionListener
391{MenuBarmenubar;Menumenu;
392MenuItemcopy,cut,paste;
393TextAreatext1,text2;
394Clipboardclipboard=null;
395Example15_10()
396{clipboard=getToolkit().getSystemClipboard();//获取系统剪贴板。
397menubar=newMenuBar();
398menu=newMenu("Edit");copy=newMenuItem("copy");
399cut=newMenuItem("cut");paste=newMenuItem("paste");
400text1=newTextArea(20,20);text2=newTextArea(20,20);
401copy.addActionListener(this);cut.addActionListener(this);
402paste.addActionListener(this);
403setLayout(newFlowLayout());
404menubar.add(menu);
405menu.add(copy);menu.add(cut);menu.add(paste);
406setMenuBar(menubar);
407add(text1);add(text2);
408setBounds(100,100,200,250);setVisible(true);pack();
409addWindowListener(newWindowAdapter()
410{publicvoidwindowClosing(WindowEvente)
411{System.exit(0);
412}

413}
);
414}

415publicvoidactionPerformed(ActionEvente)
416{if(e.getSource()==copy)//拷贝到剪贴板。
417{Stringtemp=text1.getSelectedText();//拖动鼠标选取文本。
418StringSelectiontext=newStringSelection(temp);
419clipboard.setContents(text,null);
420}

421elseif(e.getSource()==cut)//剪贴到剪贴板。
422{Stringtemp=text1.getSelectedText();//拖动鼠标选取文本。
423StringSelectiontext=newStringSelection(temp);
424clipboard.setContents(text,null);
425intstart=text1.getSelectionStart();
426intend=text1.getSelectionEnd();
427text1.replaceRange("",start,end);//从Text1中删除被选取的文本。
428}

429elseif(e.getSource()==paste)//从剪贴板粘贴数据。
430{Transferablecontents=clipboard.getContents(this);
431DataFlavorflavor=DataFlavor.stringFlavor;
432if(contents.isDataFlavorSupported(flavor))
433try{Stringstr;
434str=(String)contents.getTransferData(flavor);
435text2.append(str);
436}

437catch(Exceptionee){}
438}

439}

440publicstaticvoidmain(Stringargs[])
441{Example15_10win=newExample15_10();
442}

443}

444
优质内容筛选与推荐>>
1、数据可视化matplotlib、seaborn、pydotplus
2、跟着b站up学习如何搜索管理文献
3、jQuery和Ajax的使用(杂记)
4、asp.net mvc控制器激活全分析
5、idea(2018.3.5)破解


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号