高级程序设计java32讲稿_第1页
高级程序设计java32讲稿_第2页
高级程序设计java32讲稿_第3页
高级程序设计java32讲稿_第4页
高级程序设计java32讲稿_第5页
已阅读5页,还剩47页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

AppletAppletjview.exeJavaInternetApplet的名字和位置自动把字节码从WWW服务器上到本地并利用浏览器本身拥有的Java解释器直接执行该字节码AppletimportpublicclassMyAppletextends{}Applet是java.awt.Panel的子类,有如用响应它所包容范围之内的,或把向更次传AppletAppletinit(),start(),stop(),destroy()Applet从初始 启动化Applet转 重新激活程序 说importjava.applet.*;importjava.awt.*;publicclassLifeCycleextends{private private privateint privateint privateint public{t=t=t=t=t}publicvoid //初始化,Applet的字节码从WWW{t}publicvoid {}publicvoid {}publicvoid {}publicvoidpaint(Graphics //Applet{ ()", g.drawString("Destroy()",175,g.drawString("Paint()",235, t*30, // t*30, t*30,40, t*30,40, t*30,40, }}HTML嵌入Applet的标记:<APPLET> 至少需要三个参数:codeheightcodebase=//字节码所在位altThisis Align=CENTER //Applet界面在浏览器窗口中的对齐方式传递参数由<PARAM(p132),AppletgetParameter()HTML<APPLETcode=“MyApplet_param.class”height=200,width=<PARAMname=vstringvalue=<PARAMname=xvalue=50<PARAMname=yvalue=程序 说importjava.applet.Applet;importpublicclassMyApplet_paramextends{privateStrings="";privateintx;privateintpublicvoid{s= //HTMLx=Integer.parseInt(getParameter("x"));y=}publicvoidpaint(Graphics{if(s!=}}public {publicstaticvoidmain(String{inta1=Integer.parseInt(args[0]);a2=}} LParameter52Java是运行字节码的Java解释器 用于生成图形用户界面(graphicsuserinterface)java.awt包,awtwindowtoolkit的缩写。抽象窗口便开发人员所设计的界面独立于具体的界面实现。设计图形用户界面的工作主要有两个:java.awt包中的一个类GraphicsimportimportpublicclassDrawFiguresextends{publicvoidpaint(Graphics{}publicvoiddrawMyPolygon(Graphics{}} GraphicsdrawString(字符串,I,j)在指定位置显示字符串Font类获得字体显示效果FontMyFont=newFont(“TimesRoman”,Font.BOLD,12);GraphicsEnvironmentgl=GraphicsEnvironment.getLocalGraphicsEnvironment()StringFontNames[]=gl.getAvailableFontFamilyNames()OldFont=Current=newFont(FontNames[0],Font. IN,10);ColorPublicColor(intRed,intGreen,intBlue);PublicColor(floatRed,floatGreen,floatBlue);PublicColor(intRGB);ColorblueColor=newColor(0,0,255)g.setColor(blueColor)对象方式 publicvoidsetBackground(Colorc)publicColorgetBackground()GraphicsdrawImage(URL地址,文件名)方法可显示图像.bmpgifimportjava.applet.Appletimportjava.awt.*publicclassShowAnimatorextends{Image[]InttotalImages=IntcurrentImage=Publicvoid{ 下的images下的img001.gif至m_Images=newImage[totalImages];for(intI=0;I<totalImages;I++)m_Images[I]= Base(),}publicvoid currentImage0;}publicvoidpaint(Graphics }}Java的处触发(ActionEvent,ItemEvent等)者实现接口:publicvoidactionPerformed(ActionEventGUI

创建该组件对象的者,即实现以该组件为源的对应的者接(1)Labelprompt=newLabel(“ Prompt.setText(““)不是源按钮Buttonenter=newButton(“ //返回按钮字符串 //设置按钮的字符串 if( mand()==“ll“)if 动作PublicString mand()设置令名按钮的缺省命令名是按钮的publicint返回ActionEventALT_MASK,CTRL_MASK,SHIFT_MAST,文本 文本框与文本域(TextField与TextArea)TextAreatextArea1=newTextArea(10,45);TextFieldname=newTextField(““,8) textValueChanged(TextEvente)actionPerformed(ActionEvente)选择CheckboxbackgnewCheckboxitemStateChange(ItemEvente)importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassTextCheckboxextendsAppletimplements{CheckboxButtonpublicvoid{ckb=newCheckbox("BackgroundColor");btn=newButton("Effect");}publicvoiditemStateChanged(ItemEvent{Checkboxif(e.getItemSelectable()instanceof{temp=(Checkbox)(e.getItemSelectable());if(temp.getLabel()=="Background{}}}}style=newp=newCheckbox(“1”,true,style);b=newCheckbox(“2”,false,style);i=newCheckbox(“3”,false,style);ItemEvent类的ITEMSTATE_CHANGEDimportjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassTCheckboxGroupextendsAppletimplements{CheckboxGroupstyle;Checkboxp;Checkboxb,i;Buttonbtn;publicvoid{style=newp=newCheckbox("Normal",true,style);b=newCheckbox("Bold",false,style);i=newCheckbox("Itatic",false,style);Fontbtn=newButton("Effect");oldF=btn.setFont(newFont(oldF.getName(),oldF.getStyle(),18));}publicvoiditemStateChanged(ItemEvent{CheckboxFontoldF=if(e.getItemSelectable()instanceof{temp=(Checkbox)(e.getItemSelectable());if(temp.getLabel()=="Normal")btn.setFont(newFont(oldF.getName(),Font.IN,oldF.getSize()));if(temp.getLabel()=="Bold")btn.setFont(newFont(oldF.getName(),Font.BOLD,oldF.getSize()));if(temp.getLabel()=="Italic")btn.setFont(newFont(oldF.getName(),Font.ITALIC,}}}Choicesize=newChoice();select(intindex);select(Stringitem);add(Stringitem);insert(Stringitem,intindex);remove(intindex);remove(Stringitem);ItemEventimportjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassTChoiceextendsAppletimplements{ChoiceButtonpublicvoid{Fontsize=newChoice();btn=newButton("Effect");oldF=btn.setFont(newFont(oldF.getName(),oldF.getStyle(),}publicvoiditemStateChanged(ItemEvent{Choicetemp;FontoldF;Strings;intsi;if(e.getItemSelectable()instanceof{oldF=temp=(Choice)(e.getItemSelectable());s=temp.getSelectedItem();si=btn.setFont(newFont(oldF.getName(),oldF.getStyle(),}}}列表单击:ItemEvent类的选择双击:ActionEvent类的动作importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassTListextendsAppletimplements{ListMyList;Labelpublicvoid{result=newLabel("DoubleClicked MyList=newList(4,true);}publicvoidactionPerformed(ActionEvent{if(e.getSource()==result.setText("Youdoubleclicked }publicvoiditemStateChanged(ItemEvent{ListStringStringmgr=newString("");if(e.getItemSelectable()instanceof{temp=(List)(e.getItemSelectable());sList=temp.getSelectedItems();for(inti=0;i<sList.length;i++)mgr=mgr+sList[i]+"";}}}调整AdjustmentEvent类包含一个:ADJUSTMENT_VALUE_CHANGEDgetAdjustable()返回 AdjustmentEvent.BLOCK_DECREMENT:点击滚动条下方AdjustmentEvent.BLOCK_INCREMENT:点击滚动条上方 () publicvoidadjustmentValueChanged(AdjustmentEvente)→ importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassTSliderextendsAppletimplements{ScrollbarmySlider;publicvoid{setLayout(newBorderLayout());mySlider=add("South",mySlider);}{intif(e.getAdjustable()=={value=sliderValue.setBackground(newColor(value));}}}1.鼠标(MouseEvent)MouseEvent包含 移动intgetX()intgetY()Pointint importjava.applet.*;importjava.awt.*;importpublicclassResponseToMouseextendsAppletimplementsMouseListener,MouseMotionListener{publicvoid{}publicvoidmouseClicked(MouseEvent{if(e.getClickCount()==showStatus("SingleClickedat"+e.getX()+","+e.getY());elseif(e.getClickCount()==2)showStatus("DoubleClickedat"+e.getX()+","+}publicvoidmouseEntered(MouseEvent{}publicvoidmouseExited(MouseEvent{}publicvoidmousePressed(MouseEvent{}publicvoidmouseReleased(MouseEvent{}publicvoidmouseMoved(MouseEvent{showStatus("YouMovedMouse,Thenewlocationis"+e.getX()+","+}publicvoidmouseDragged(MouseEvent{}}2.键盘(KeyEvent)KeyEvent包含char返回的按键的Unicode字符,如无则返回KeyEvent.CHAR_UNDEFINEDStringgetKeyText()KeyEvent对应的者接口是KeyListener,该接口定义了以下方法分别与中的三个具体类型相对应:VoidkeyPressed(KeyEvente)VoidkeyRelease(KeyEvente)VoidkeyTyped(KeyEvente)PublicvoidkeyPress(KeyEvent{charch=e.getKeyChar();}CanvasmyCanvas=newpublicvoidpaint(Graphics与Applet相似,可以键盘和鼠importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassTCanvasextends{CanvasDrawpublicvoid{cd=newCanvasDraw(newDimension(200,180),this);}}classCanvasDrawextendsCanvasimplements{Appletm_Parent;booleanm_bFlag=false;intcurrentX=0,currentY=0,startX=0,startY=0;StringBuffersb=newStringBuffer();CanvasDraw(Dimensiond,Applet{m_Parent=p;addMouseListener(newMouseAdpt(this));}voidsetStart(intx,int{startX=x;startY=}voidsetCurrent(intx,int{currentX=x;currentY=}voidsetMouseDragged(boolean{m_bFlag=}voidshowMeg(String{}void{startX=startY=currentX=currentY=0;}publicvoidkeyTyped(KeyEvent{charch=e.getKeyChar();showMeg("Typed"+sb.toString());}publicvoidkeyPressed(KeyEvente){}publicvoidkeyReleased(KeyEvente){}publicvoidpaint(Graphics{g.drawString("("+currentX+","+currentY+")"+sb,10,20);}}classMouseAdptextends{CanvasDraw{m_Parent=}publicvoidmousePressed(MouseEvent{m_Parent.setStart(e.getX(),e.getY());m_Parent.showMeg("DrawLineStarted");}publicvoidmouseReleased(MouseEvent{m_Parent.showMeg("Oneline}publicvoidmouseEntered(MouseEvent{m_Parent.showMeg("MouseEnter}publicvoidmouseExited(MouseEvent{m_Parent.showMeg("Mouseexit}}classMouseMotionAdptextends{CanvasDraw{m_Parent=}publicvoidmouseMoved(MouseEvent{m_Parent.setCurrent(e.getX(),e.getY());}publicvoidmouseDragged(MouseEvent{m_Parent.setCurrent(e.getX(),e.getY());}}SetLayout(newFlowLayout(intalign,inthgap,intFlowLayout(intalign)BorderLayout(inthgap,int0。importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassTBorderLayoutextends{Buttonnorth,south,west,east,publicvoid{setLayout(newBorderLayout());north=newButton("North");south=newButton("South");west=newButton("West");east=newButton("East");center=newButton("Center");add("Center",center);add("North",north);add("South",}}CardLayout的一般步骤如下:CardLayoutMycard=newsetLayout()来设置布局编辑器:add()加入容器:add(“FirstPage”,组件)add(“SecondPage”,组件)importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassTCardLayoutextends{CardLayoutMyCard=newCardLayout();Buttonbtn1=newButton("FirstPage");Buttonbtn2=newButton("SecondPage");Buttonbtn3=newButton("ThirdPage");Buttonbtn4=newButton("Page");Buttonbtn5=newButton("FifthPage");publicvoid{add("FirstPage",btn1);add("SecondPage",btn2);add("ThirdPage",btn3); Page",btn4);add("FifthPage",btn5);btn1.addMouseListener(newMouseMoveCard(MyCard,this));btn2.addMouseListener(newMouseMoveCard(MyCard,this));btn3.addMouseListener(newMouseMoveCard(MyCard,this));btn4.addMouseListener(newMouseMoveCard(MyCard,this));btn5.addMouseListener(new}}classMouseMoveCardextends{CardLayoutcl;Appletm_Parent;{cl=c;m_Parent=a;}publicvoidmouseClicked(MouseEvent{if(e.getModifiers()==InputEvent.BUTTON1_MASK)}}

GridLayout的一般步骤如下:GridLayoutMycard=newsetLayout()来设置布局编辑器:importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassTGridLayoutextends{publicvoid{setLayout(newfor(inti=0;i<5;i++)for(intj=0;j<6;j++){add(new}}}

add(newPanelContainer类 ponet(int ponet(intx,int remove(intindex) Container可ContainerEvent类代表的容器publicvoidcomponetAdded(ContainerEvente)publicvoidcomponetRemoved(ContainerEvente)容器PublicContainer //返回容器的容器对PublicComponent 无边框容器:Container→PanelAppletPanel不能被移动,放大,缩小或关闭Panel不能作最外层图形界面的容器Panel的大小importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassTPanelextendsAppletimplementsActionListener,{Panelp1,p2,Labelprompt1,prompt2,prompt3;Buttonbtn;publicvoid{p1=newPanel();p2=newPanel();p3=newPanel();prompt1=newLabel("IntheFirstPanel");prompt2=newLabel("IntheSecondPanel");prompt3=newLabel("IntheThirdPanel");btn=newButton("in}publicvoidactionPerformed(ActionEvent{if(e.getSource()==btn)}publicvoidcomponentRemoved(ContainerEvent{showStatus("TheThirdPanel}publicvoidcomponentAdded(ContainerEvente)}AppletPanelJavaApplet程序最外层的容器,它可以依赖浏览器的窗口来完Frame与窗口有边框容器:Window→Frame,Dialog,FileDialogFrame作为最顶层容器存在,不能被其他容器所包含Frame(Stringtitle)Frame对WINDOWS_CLOSING做出响应,实现WindowListener接口窗口WindowEvent类包含如下几个具体窗口: 返回当前WindowEvent的具体窗口,Window对 importjava.awt.*;importjava.awt.event.*;publicclass{publicstaticvoidmain(String{new}}classMyFrameextendsFrameimplements{Button{super("Mybtn=newButton("Closed");setLayout(newFlowLayout());}publicvoidactionPerformed(ActionEvent{ mand()=={}}}classcloseWinextends{publicvoidwindowClosing(WindowEvent{Framefrm=(Frame)(e.getSource());}}Frame窗口实现 Container接菜单组件→菜单 Bar→菜单 →菜项菜单条→菜项m_Bar=newFile=new("File"); 创建 pi_New=newItem("New"); :mi_File_Open=new Item("Open",new →mi_Edit_Cut=newCheckboxItem("Cut");popM=newPopup();pi_New=newItem("New");ta.addMouseListener(newHandleMouse(this));MouseListener的mouseReleased(MouseEvente)show()importjava.awt.*;importjava.awt.event.*;publicclass{publicstaticvoidmain(String{MyFramemf=newMyFrame();mf.setSize(newDimension(300,200));}}classMy FrameextendsFrame{Barm_ Edit,Itemmi_File_Open,mi_File_Close,mi_File_Exit,mi_Edit_Copy;Itempi_New,pi_Del,pi_Pro,mi_Paste_All,mi_Paste_Part; Itemmi_Edit_Cut; {super("MyWindowhaving ta=newTextArea("\n\n\n\n\n\n\t\t\tNoChoice",5,20);ta.addMouseListener(newHandleMouse(this));popM=newPopup();pi_New=newItem("New");pi_Del=newItem("Delete");pi_Pro=newItem("Properties");m_Bar=new File=new("File");mi_File_Open=newItem("Open",newShortcut('o'));mi_File_Close=newItem("Close");mi_File_Exit=newItem("Exit");mi_File_Exit.setShortcut(newShortcut('x')); Edit=new mi_Edit_Copy=new mi_Edit_Cut=newCheckboxm_Edit_Paste=new mi_Paste_All=newItem("PasteAll");mi_Paste_Part=new Item("PastePart");}publicvoidactionPerformed(ActionEvent{ mand()=={}}

publicvoiditemStateChanged(ItemEvent{if(e.getSource()=={ta.setText("\n\n\n\n\n\n\t\t\t"+"haveChosen"+}}}

classHandleMouseextends{ Frame Frame{m_Parent=}publicvoidmouseReleased(MouseEvent{m_Parent.popM.show((Component)e.getSource(),e.getX(),e.getY());}}13.20框,组件与焦点组件 焦点(FocusEvent) Frame,并被它负责弹出Dialog(Frameparent,Stringtitile,booleanisModal)SetModel(booleanisModal) importjava.awt.*;importjava.awt.event.*;publicclass{publicstaticvoidmain(String{MyDialogFramedf=new}}classMyDialogFrameextends {DialogMegDlg,Buttonbtn1,btn2,btnY,btnN,TextFieldtf=newTextField("NoMessage",45);TextFieldgetMeg=newTextField("InputMessage",100);{super("MyWindowusingDialog");btn1=newButton("Hidden");btn2=newButton("Require");btnY=newButton("Yes");btnN=newButton("No");btnR=newButton("Return");setLayout(newFlowLayout());setSize(350,150);}publicvoidactionPerformed(ActionEvent{ mand()=={MegDlg=newDialog(this,"Hiddenrealy?",true);Panelp1=newPanel();p1.add(newLabel("Wanttohidethisbutton,continue?"));Panelp2=newPanel();}else mand()=={InOutDlg=newDialog(this,"Input");}else {}elseif( mand()=="No")else mand()=={}}publicvoidcomponentShown(ComponentEvente){}publicvoidcomponentResized(ComponentEvente){}publicvoidcomponentMoved(ComponentEvente){}publicvoidcomponentHidden(ComponentEvent{ ponent()).getLabel()+"\"is}publicvoidfocusGained(FocusEvent{ ponent()).getTitle()+"\"isfocused}publicvoidfocusLost(FocusEvente)}classWinAdptextends{publicvoidwindowClosing(WindowEvent{}}13.21SwingGUI(JFC,GUIjava.awtGUI组件增加了许多功能。javax.swing.JApplet是java.applet.AppletSwingGUI组件都包含在JApplet与AppletAppletFlowLayoutJApplet的缺省BorderLayout.JAppletSwingadd(),JApplet.getContentPane()获得一个ContainerContainer.add()将JComponent及其子类加入到JAppletimportjava.awt.*;importjavax.swing.*;publicclassMyFirstJAppletextendsJApplet{publicvoidpaint(Graphicsg){g.drawString("Iama}}importimportjavax.swing.*;importjava.awt.event.*;publicclassTestIconButtonextendsJAppletimplements{JButtonpublicvoid{Containerc=getContentPane();Iconicon=newImageIcon("bIcon.gif");jbtn=newJButton("JButton",icon);c.add(jbtn,BorderLayout.NORTH);}publicvoidactionPerformed(ActionEvent{}}JButtonSwingimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassTestIconButtonextendsJAppletimplements{JButtonpublicvoid{Containerc=getContentPane();Iconicon=newImageIcon("bIcon.gif");IconpressedIcon=newImageIcon(“pressed.gif”);IconrolloverIcon=newImageIcon(“rollover.gif”);jbtn=newJButton("JButton",icon); );//c.add(jbtn,BorderLayout.NORTH);}publicvoidactionPerformed(ActionEvent{}}importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassTTipButtonextendsJAppletimplements{JButtonjbtn;publicvoidinit(){Containerc=Iconicon=newImageIcon("bIcon.gif");jbtn=newJButton(icon); }publicvoidactionPerformed(ActionEvent{showStatus("JButton}}importimportjavax.swing.event.*;importjavax.swing.*;publicclassTJSliderextendsJAppletimplements{JSliderjslh,JLabeldoublehValue=0.0,vValue=//Initializetheappletpublicvoidinit(){Containerc=jslhnew jslv=newJSlider(JSlider.VERTICAL,200,1000,800); //从200至1000,初始在800 //横向每30显示 //200显示jl=newJLabel("0.0,800.0");}publicvoidstateChanged(ChangeEvent{if(e.getSource()==elseif(e.getSource()==jslv)}}JPpasswordField是TextFieldimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassTJPasswordFieldextendsJAppletimplements{JLabeljl1,jl2;JPasswordFieldjp1,jp2;JButtoncommit,cancel;publicvoid{Containerc=jl1=newJLabel("Pleaseinputyourpassword:");jl2=newJLabel("Inputagain:");jp1=newJPasswordField(10);jp2=newcommit= newJButton("Commit");cancel= newJButton("Cancel");c.setLayout(newGridLayout(3,2));}publicvoidactionPerformed(ActionEvent{if(e.getSource()==commit)showStatus("Twoinputsarenotsame");if(e.getSource()==cancel){}}}importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importpublicclass{publicstaticvoidmain(String{new}}{JButton[]intcurrentIndex={super("CardLayout");jtp=newJTabbedPane();jbtn=newJButton[5];for(inti=0;i<5;i++){jbtn[i]=newJButton("The"+jtp.addTab("The"+i,jbtn[i]);}}publicvoidstateChanged(ChangeEvent{{inti=((JTabbedPane)e.getSource()).getSelectedIndex();currentIndex=}}publicvoidactionPerformed(ActionEvent{}}classWindAdptextends{{}}14JavaJavaPCB是进程存在的标志新建一个Threa

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论