Java程序设计基础教程第六章图形用户界面编辑

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

一、图形用户界面的核心概念(1)一个简单的GUI示例。例题:将二进制数据转换为十进制importjava.awt.*;importjava.awt.event.*;publicclassConvertToDecextendsFrameimplementsActionListener{Labeldec;TextFieldinput;publicConvertToDec(){super(binarytodecimal);dec=newLabel(......结果......);input=newTextField(15);Buttonconvert=newButton(转换);setLayout(newFlowLayout());add(input);add(convert);add(dec);convert.addActionListener(this);}publicvoidactionPerformed(ActionEvente){Strings=input.getText();intx=Integer.parseInt(s,2);dec.setText(result=+x);}publicstaticvoidmain(String[]args){Framex=newConvertToDec();x.setSize(400,100);x.setVisible(true);}}(2)创建窗体Frame的创建大致有两种方式:①通过继承Frame来创建窗体。②直接由Frame类创建。(3)创建GUI部件由add()方法把GUI布局加入到容器中。(4)事件处理①事件处理的流程★给事件源对象注册监听者★给监听者编写事件处理代码★发生事件时调用监听者的方法进行相关处理②事件监听者接口及其方法Java中的所有事件类都定义在java.awt.event包中,该包中还定义了11个监听者接口,每个接口内部包含了若干处理相关事件的抽象方法。见P158:表7-1(5)在事件处理代码中区分事件源一个事件源对象可以注册多个监听者,一个监听者也可以监视多个事件源。不同类型的事件提供了不同的方法来区分事件源对象。如ActionEvent类中提供了两个方法:①getSource():用来获取事件对象名。②getActionCommand():用来获取事件对象的命令名。例题:有两个按钮,点击按钮b1画圆,点击按钮b2画矩形。importjava.awt.*;importjava.awt.event.*;publicclassTwoButtonextendsPanelimplementsActionListener{Buttonb1,b2;Paneldraw;publicTwoButton(Paneldraw){this.draw=draw;b1=newButton(circle);b2=newButton(rectangle);add(b1);add(b2);b1.addActionListener(this);b2.addActionListener(this);}publicvoidactionPerformed(ActionEvente){Graphicsg=draw.getGraphics();g.setColor(draw.getBackground());g.fillRect(0,0,draw.getSize().width,draw.getSize().height);g.setColor(Color.blue);Stringlabel=e.getActionCommand();if(label.equals(circle))g.drawOval(20,20,50,50);elseg.drawRect(20,20,40,60);}publicstaticvoidmain(String[]args){Framef=newFrame(toButtoneventTest);Paneldraw=newPanel();TwoButtontwo=newTwoButton(draw);f.setLayout(newBorderLayout());f.add(North,two);f.add(Center,draw);f.setSize(300,300);f.setVisible(true);}}(6)关于事件适配器类例题:处理窗体关闭/*appletcode=TestFrame.classheight=500width=500/applet*/importjava.awt.*;importjava.awt.event.*;importjava.applet.*;publicclassTestFrameextendsApplet{publicvoidinit(){newMyFrame();}}classMyFrameextendsFrameimplementsActionListener{Buttonbtn;MyFrame(){super(MYWINDOWS);btn=newButton(关闭);setLayout(newFlowLayout());add(btn);btn.addActionListener(this);addWindowListener(newCloseWin());setSize(300,200);setVisible(true);}publicvoidactionPerformed(ActionEvente){if(e.getActionCommand()==关闭){dispose();}}};classCloseWinextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){Windoww=e.getWindow();w.dispose();}};二、容器与布局管理Java.awt包中定义了5种布局管理器:(1)FlowLayout:流式布局例题:importjava.awt.*;importjava.applet.Applet;publicclassFlowLayoutExampleextendsApplet{publicvoidinit(){setLayout(newFlowLayout(FlowLayout.LEFT,10,10));Stringspaces=;for(inti=1;i=9;i++){add(newButton(B#+i+spaces));spaces+=;}}publicstaticvoidmain(String[]args){Framex=newFrame(FlowLayout);FlowLayoutExampley=newFlowLayoutExample();x.add(y);y.init();x.setSize(200,100);x.setVisible(true);}}(2)BorderLayout:边缘布局例题:importjava.awt.*;importjava.applet.Applet;publicclassBorderLayoutExampleextendsApplet{String[]borders={North,East,South,West,Center};publicvoidinit(){setLayout(newBorderLayout(10,10));Stringspaces=;for(inti=0;i5;i++){add(borders,newButton(borders));}}publicstaticvoidmain(String[]args){Framex=newFrame(BorderLayout);BorderLayoutExampley=newBorderLayoutExample();x.add(y);y.init();x.setSize(300,200);x.setVisible(true);}}(3)GridLayout:网格布局例题:importjava.awt.*;importjava.applet.Applet;publicclassGridLayoutExampleextendsApplet{publicvoidinit(){setLayout(newGridLayout(3,3,10,10));for(inti=1;i=9;i++){add(newButton(Button#+i));}}publicstaticvoidmain(String[]args){Framex=newFrame(GridLayout);GridLayoutExampley=newGridLayoutExample();x.add(y);y.init();x.setSize(300,200);x.setVisible(true);}}(4)CardLayout:卡片布局例题:importjava.awt.*;importjava.applet.*;importjava.awt.event.*;publicclassCardLayoutExampleextendsApplet{publicvoidinit(){finalCardLayoutcardlayout=newCardLayout(10,10);setLayout(cardlayout);ActionListenerlistener=newActionListener(){publicvoidactionPerformed(ActionEvente){cardlayout.next(CardLayoutExample.this);}};for(inti=1;i=9;i++){Buttonb=newButton(Button#+i);b.addActionListener(listener);add(Button+i,b);}}publicstaticvoidmain(String[]args){Framex=newFrame(CardLayout);CardLayoutExampley=newCardLayoutExample();x.add(y);y.init();x.setSize(300,200);x.setVisible(true);}}(5)GridBagLayout:网格快布局例题:importjava.awt.*;publicclassTestextendsFrame{publicstaticvoidmain(String[]args){newTest();}publicTest(){LabelreceiveLabel=newLabel(收件人:);LabelccLabel=newLabel(抄送:);LabelsubjectLabel=newLabel(主题:);LabelaccessoryLabel=newLabel(附件:);TextFieldreceiveField=newTextField();TextFieldccField=newTextField();TextFieldsubjectField=newTextField();TextAreaaccessoryArea=newTextArea(1,40);TextAreamailArea=newTextArea(8,40);setLayout(newGridBagLayout());GridBagConstraintsgridBag=newGridBagConstraints();gridBag.fill=GridBagConstraints.HORIZONTAL;gridBag.weightx=0;gridBag.weighty=0;receiveLabel.setFont(newFont(Alias,Font.BOLD,16));addToBag(receiveLabel,gridBag,0,0,1,1);ccLabel.setFont(newFo

1 / 28
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功