实验指导方案第9章gui补充案例_第1页
实验指导方案第9章gui补充案例_第2页
实验指导方案第9章gui补充案例_第3页
实验指导方案第9章gui补充案例_第4页
实验指导方案第9章gui补充案例_第5页
已阅读5页,还剩40页未读 继续免费阅读

下载本文档

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

文档简介

第九章 2、练习目掌握如何使用AWT处理机3、需求分4、设计思路(实现原理定义一个类MyWindowListener实现器的接口WindowListener。4)通过addWindowListener()方法为源器对象。importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample01publicstaticvoidmain(String[]args)FramefnewFrame("设置窗体的宽和f.setSize(300,设置窗体的出现的位f.setLocation(300,设置窗体可//为窗口组 MyWindowListenermw=newMyWindowListener();}}创建MyWindowListener类实现WindowListenerclassMyWindowListenerimplementsWindowListener publicvoidwindowClosing(WindowEvente){Windowwindow=e.getWindow();}publicvoidwindowActivated(WindowEvente)}publicvoidwindowClosed(WindowEvente)}publicvoidwindowDeactivated(WindowEvente)}publicvoidwindowDeiconified(WindowEvente)}publicvoidwindowIconified(WindowEvente)}publicvoidwindowOpened(WindowEvente)}}9-1

图9-1 (Event : (Listener:对象(对象中包含处理器)。 2、对象、源、器、处理器在整个处理机制中都起着非常重要的9-2源源图9-2处理流程图9-2中源是一个组件当用户进行一些操作时如按下鼠标或者释放键盘等,这些动作会触发相应的如果源了器则触发的相应将会被处理。 2、练习目掌握如何使用AWT适配器3、需求分JDK提供了一些适配器类,它们是器接口的默认实现类,这些实现类中实实 器接口的目的。为了让初学者熟悉AWT适配器,本4、设计思路(实现原理Example02MyWindowListener类继承适配器类WindowAdapter。4)windowClosing()方法importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample02publicstaticvoidmain(String[]args)FramefnewFrame("设置窗体的宽和f.setSize(300,设置窗体的出现的位f.setLocation(300,设置窗体可//为窗口组 f.addWindowListener(new}}//继承WindowAdapter类,重写windowClosing()方法classMyWindowListenerextendsWindowAdapterpublicvoidwindowClosing(WindowEvente){Windowwindow=(Window) }}9-3

图9-3 窗2、练习目掌握窗体的处3、需求分4、设计思路(实现原理1)Example03,创建一个窗体。2)WindowListener实例对象。importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample03publicstaticvoidmain(String[]args){finalFramef=newFrame("窗体 //使用内部类创建WindowListener实例对象 f.addWindowListener(newWindowListener(){publicf.addWindowListener(newWindowListener(){publicvoidwindowOpened(WindowEvente){}publicvoidwindowActivated(WindowEvente}publicvoidwindowIconified(WindowEvente}publicvoidwindowDeiconified(WindowEvente}publicvoidwindowDeactivated(WindowEvente}publicvoidwindowClosing(WindowEvente){ }publicvoidwindowClosed(WindowEvente)}}}9-4图9-4运行结果从上图可以看出,当对窗口进行操作时,程序到了这些窗体。在本案例中,通过WindowListener对操作窗口的动作进行,当接收到特定的际需求,在器中自定义窗体的处理器。9-42、练习目掌握鼠标的处3、需求分操作被定义为鼠标,JDK中提供了一个MouseEvent类用于表示鼠标。为4、设计思路(实现原理编写一个类Example04importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample04publicstaticvoidmain(String[]args){finalFramef=newFrame("鼠 f.setLayout(newFlowLayout());f.setSize(300,200);f.setLocation(300,200);ButtonbutnewButton("按钮f.add(but//为按钮添加鼠 but.addMouseListener(newMouseListener(){publicvoidmouseReleased(MouseEvente){}publicvoidmousePressed(MouseEvente)}}publicvoidmouseExited(MouseEvente){}publicvoidmouseEntered(MouseEvente}publicvoidmouseClicked(MouseEvente}}}图9-5鼠标窗图9-6publicvoidmouseClicked(MouseEvente){publicvoidmouseClicked(MouseEvente){ } } }}MouseEvent类中针对鼠标的按键都定义了对应的常量,可以通过MouseEvent点击次数也可以通过MouseEvent对象的getClickCount()方法获取到。 键2、练习目掌握键盘的处3、需求分键盘,JDK中提供了一个KeyEvent类表示键盘。为了让初学者掌握键盘4、设计思路(实现原理importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample05publicstaticvoidmain(String[]args)Framef=newFrame("键盘 f.setLayout(newFlowLayout());f.setSize(400,f.setSize(400,f.setLocation(300,TextFieldtfnewTextField(30创建文本框对象f.add(tf);//在窗口中添加文本框组件//为文本框添加键 tf.addKeyListener(newKeyAdapter(){publicvoidkeyPressed(KeyEvente){intKeyCodee.getKeyCodeStringsKeyEvent.getKeyText(KeyCodeSystem.out.print("输入内容为:"+s+",");System.out.println("KeyCode为:KeyCode);}}}9-7图9-7键盘 ,图9-8还有一个静态方法getKeyText(intkeyCode)String形式返回。 2、练习目3、需求分加顺序从左向右放置。为了让初学者熟悉流式布局管理器,本案例将演示FlowLayout布局管理器的用法。4、设计思路(实现原理编写类Example06,创建一个名为“流式布局”的窗体importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample06publicstaticvoidmain(String[]args)finalFramefnewFrame("流式布局创建一个名为流式布局的窗距为f.setLayout(newFlowLayout(FlowLayout.LEFT,25,f.setSize(200 f.setLocation(300,f.setLocation(300,Buttonbut1newButton("11 把"1个按钮"添加到fbut1.addActionListener(newActionListener(){//动 为第1个按privateintnum=定义变量numpublicvoidactionPerformed(ActionEvente){f.add(newButton(++num向窗体中添加新按钮// 钮}f.setVisible(true}}3-8图9-91、FlowLayout布局管理器的特点就是可以将所有组件像流水一样依次进行排列,不需5FlowLayout(intalign,int 2、练习目3、需求分为了让初学者熟悉BorderLayout布局,本案例演示一下4、设计思路(实现原理5BorderLayout5importjava.awt.*;publicimportjava.awt.*;publicclassExample07{publicstaticvoidmain(String[]args){finalFramefnewFrame("边界布局");f.setLayout(new 设置窗体中的布局管理f.setLocation(300,200);Buttonbut1newButton("东Buttonbut2newButton("西");Buttonbut3newButton("南");Buttonbut4newButton("北");Buttonbut5newButton("中 设置按钮所在区域}}9-10图9-101BorderLayout布局管理器中,组件组件可以被放置在这五个区域的中任意一个。BorderLayout9-11所示。图9-11BorderLayout定添加到哪个区域,则默认添加到CENTER区域,并且每个区域只能放置一个组件,如果向9-8GridLayout2、练习目3、需求分4、设计思路(实现原理4*312个按钮到GridLayoutimportimportpublicclassExample08publicpublicstaticvoidmain(StringargsFramef=newFrame("网格布局");f.setSize(300 f.setLocation(400,12个按钮到GridLayoutfor(inti=1;i<=12;i++)ButtonbtnnewButton("btn"i);f.add(btn);//向窗体中添加按钮}}}9-12图9-12GridLayout(introws,intcols,int2、GridLayout布局管理器的特点是组件的相对位置不随区域的缩放而改变,但组件的9-9GridBagLayout2、练习目3、需求分允许一个组件一个或者多个网格为了让初学者熟悉GridBagLayout的布局,GridBagLayout的用法。4、设计思路(实现原理LayoutFrameExample09LayoutimportimportclassLayoutextendsFrame{publicLayout(Stringtitle){GridBagLayoutlayout=newGridBagLayout();GridBagConstraintsc=newGridBagConstraints();c.fillGridBagConstraints.BOTH;设置组件横c.weightx1设置横向权重为c.weighty1设置纵向权重为ponent("btn1",layout,c);ponent("btn2",layout,c);ponent("btn3",layout,c.gridwidthGridBagConstraints.REMAINDERponent("btn4",layout,c.weightxc.weightx0c.weighty=0;//0ponent("btn5layoutc);c.gridwidth1设置组件跨一个网格(默认值)ponent("btn6",layout,c);c.gridwidthGridBagConstraints.REMAINDERponent("btn7",layout,c);c.gridheight=2;//设置组件纵向跨两个网格c.gridwidth=1;//设置组件横向跨一个网格c.weightx=2; //设置横向权重为2c.weighty ponent("btn8",layout,c);c.gridwidth=GridBagConstraints.REMAINDER;c.gridheight=1;ponent("btn9",layout,c);ponent("btn10",layout,c);}privatevoid ponent(Stringname,GridBagLayoutlayout,GridBagConstraintsc){ButtonbtnewButton(name创建一个名为namelayout.setConstraints(bt,c设置GridBagConstraintsthis.add(bt}}publicclassExample09publicstaticvoidmain(String[]args){newLayout("GridBagLayout");}}9-13

图9-13GridBagLayoutGridBagLayoutlayout=newGridBagConstraintsGridBagConstraintsconstraints=newconstraints.gridx=constraints.gridy=constraints.gridwidth=设置网格的左上角横向索设置网格的左上角纵向索设置组件横constraints.gridheight1;//3GridBagLayoutsetConstraints()GridBagConstraints对象和受控组GridBagConstraints2、3、4步骤。 2、练习目3、需求分相当于一卡片,而管理这些卡片的布局管理器就是卡片布局管理(CardLayout)CardLayout4、设计思路(实现原理定义卡片布局对象,在卡片面板中添加2个文本分别为“传智播客”和“importjava.awt.*;importjavax.swing.*;importimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;//定义Cardlayout继承Frame类,实现ActionListener接classCardlayoutextendsFrameimplementsActionListenerPanelcardPanel=newPanel(); 定义Panel面板放置卡片PanelcontrolpaPanelnewPanel();//定义Panel面板放置按钮Buttonnextbutton,preButton;CardLayoutcardLayoutnewCardLayout定义卡片布局对publicCardlayout 器的属setSize(300,200);//为窗口添加关 this.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){} 设置cardPanel面板对象为卡布在cardPanel2cardPanel.add(newLabel(" 播客",Label.CENTER));cardPanel.add(newLabel(" 程序员",Label.CENTER));nextbuttonnewButton("下一张卡片");preButton=newButton("上一张卡片");//为按钮对象 //将按钮添加到controlpaPanel中将cardPanelthis.add(cardPanel,将controlpaPanelthis.add(controlpaPanel,}//下面的代码实现了按钮 publicvoidactionPerformed(ActionEvente)//如果用户单击nextbutton,执行的语句ife.getSourcenextbutton}if(e.getSource()==preButton)}}}publicclassExample10publicstaticvoidmain(String[]args){Cardlayoutcardlayout=newCardlayout();}}9-14图9-14Stringparentname 名称:Graphics2、练习目掌握使用Graphics3、需求分很多GUI程序都需要在组件上绘制图形,在java.awt包中专门提供了一个Graphics类中的方法。4、设计思路(实现原理 importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;importjava.util.Random;publicclassExample11publicstaticvoidmain(String[]args)finalFrameframe=newFrame(" ");//创建Frame对象finalPanelpanel=newMyPanel(); frame.setSize(300,//将Frame窗口居中}}classMyPanelextendsPanel{publicvoidpaint(Graphicsg){intwidth=200;//定 intheight=60;//定 g.fillRect(0,0,width,填g.drawRect(0,0width1,height1绘制边Randomr=newfor(inti=0;i<100;i++){intx=r.nextInt(width)-2;inty=r.nextInt(height)-2;g.drawOval(x,y,2,}}g.setFont(newFont("黑体Font.BOLD30char[]chars=+StringBuildersb=newStringBuilder();for(inti=0;i<4;i++){intpos=r.nextInt(chars.length);charc=chars[pos];sb.append(c+"}g.drawString(sb.toString20,30}}9-15图9-15voiddrawLine(intx1,intintx2,int drawRect(intx, intwidth,intheight)于x和x+width。上边缘和下边缘分别位于y和y+ drawOval(intx, intwidth,intheight)x、y、width和height参数指定的矩形中。椭圆覆盖区域的宽度为width+1像素,高度为height1voidfillRect(intx,inty,intintxxwidth1yyheightvoidfillOval(intx,inty,intintvoiddrawString(Stringstr,intintstr(xy) 名称:Jdialog2、练习目Jdialog3、需求分JDialog框可分为两种:模态框和非模态框。所谓模态框是Jdialog类的使用,本案例将演示JDialog框中的模态框的使用。4、设计思路(实现原理importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassExample12{publicstaticvoidmain(String[]args){JButtonbtn=newJButton("模式 JFramef=newJFrame("模式 f.setSize(300,250);f.setLocation(300,f.setLayout(newFlowLayout());//为内容面板设置布局管理在Container对象上添加按设置点击关闭按钮默认关闭窗finalJLabellabel=newfinalJDialogdialog=new框dialog.setSize(220,dialog.setLocation(350,250);dialog.setLayout(newFlowLayout());finalJButtonbtn2newJButton("确定");"框");//dialog.add(btn2);// //为"模 btn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//设 框为模//如果JDialog窗口中没有添加了 ,就把 if( ponents().length==1){}//否则修 //显 }}}9-16图9- 运行结DialogJDialogsetModal()方法来进行设置,JDialog构造方法,用来创建一个非模态的框,ownerJDialog(Frameowner, JFrame组名称:JFrame2、练习目3、需求分不能放置在其它容器之中,JFrame4、设计思路(实现原理Example13JFrame创建一个title为“JFrameimportimportjava.awt.FlowLayout;importjava.awt.event.*;importjavax.swing.*;publicclassExample13extendsJFrame{publicExample15(){this.setSize(300,this.setSize(300,JButtonbtnewJButton("关闭窗口this.setLayout(new//设置点击关闭按钮时的默认操作}publicstaticvoidmain(String[]args){newExample13();}}9-17图9-17JFrame类创建了一个窗体,并向该窗体中添加了一个按钮。JFrameFrame类最大的区别在于,JFrame类提供了关闭窗口的功能,在程序中不需要添加窗 2、练习目3、需求分JScrollPane是一个带有滚动条的面板容器,而且这个面板只能添加一个组件,JScrollPaneJPanel中,然后JPanelJScrollPaneJScrollPane面板容器的使用,4、设计思路(实现原理importjava.awt.*;importimportjava.awt.*;importjavax.swing.*;publicclassExample14extendsJFrame{publicExample14(){创建滚动面JScrollPanescrollPane=new//设置水平滚动条策略--滚动条一直显示(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);//设置垂直滚动条策略--滚动条需要时显示(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);定义一个JPanel面JPanelpanel=new设置JPanel//将滚动面板添加到内容面板的CENTER区域this.add(scrollPane,BorderLayout.CENTER);this.setSize(400,250);}publicstaticvoidmain(String[]args){newExample14();}}}9-18图9-181、SwingJFrameJDialog这样的顶级窗口,还提供了一些中间容器,这些容器不能单独存在,只能放置在顶级窗口中。其中最常见的中间容器有两种:JPanel和intvsb,inthsb)创建一个显示指定容器、并具有指定滚动条策略的ScrollPaneConstants的静态常量,如下所示: ) )void voidComponent 名称 2、练习目 ponent的使3、需求分文本组件ponent用于接收用户输入的信息或向用户展示信息。为了JTextField和JTextArea4、设计思路(实现原理importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassExample15extendsJFrame{JButtonsendBt;JTextFieldinputField;JTextAreachatContent;publicExample15(){this.setLayout(newchatContentnewJTextArea(12,34创建一个文本//JScrollPanesho nel=newJScrollPane(chatContent); //设置文本域不可编辑JPanelinputPanel=newJPanel();//创建一个JPanel面板inputField=newJTextField(20);//创建一个文本框sendBt=newJButton("发送"); //创建一个发送按钮sendBt.addActionListener(newsendBt.addActionListener(newActionListener(){//听 重写StringcontentinputField.getText();//if(content!=null&&!content.trim().equals(""))//chatContent.append(content+}else//如果为空,提 内容不能为空} }Labellabel=newLabel("编辑添加到JPanel将文本框添加到JPanel//将按钮添加到JPanel//将滚动面板和JPanel面板添加到JFrame nel,BorderLayout.CENTER);this.add(inputPanelBorderLayout.SOUTH); this.setSize(400,300);}publicstaticvoidmain(String[]args){newExample15();}}9-19图9-191、文本组件包括文本框(JTextField)、文本域(JTextArea)等,它们都有一个共同父类JTextArea常用的构造方法,9-11所示。表9- 名称:JCheckBox2、练习目JCheckBox组件3、需求分JCheckBox组件被称为复选框,它有选中(是)/未选中(非)两种状态,如果用户4、设计思路(实现原理importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassExample16extendsJFrame{privateJCheckBoxitalic;privateJCheckBoxbold;privateJLabellabel;publicExample16(){//创建一个 labelnewJLabel("请选择字体样式//设 label.setFont(newFont("宋体",Font. IN,20)); //在CENTER域添加JPanelpanelnewJPanel创建一个JPanel面//创建两个JCheckBox复选框italicnewJCheckBox("斜体");bold=newJCheckBox("黑体");//为复选框定义 ActionListenerlistener=newActionListener(){publicvoidactionPerformed(ActionEvente)intmode=if(bold.isSelected())mode+=if(italic.isSelected())mode+=label.setFont(newFont("宋体mode,}//为两个复选框添加 在JPanel面板面板添加复选框在SOUTH域添加JPanel面this.add(panel,BorderLayout.SOUTH);this.add(panel,BorderLayout.SOUTH);this.setSize(300,300);}publicstaticvoidmain(String[]args){newExample16();}}9-20图9-20钮,通过判断这两个按钮是否选中来改变界面字体的样式。图9-20中,四个图分别表示勾选零个、一个和两个复选框时“请选择字体样式”所显示的字体样式。 2、练习目3、需求分JRadioButton组件的使用,本案例将JRadioButton组4、设计思路(实现原理将按钮添加到panel面板和ButtonGroup按钮组中并添加器,选择三importjava.awt.*;importjavax.swing.*;importimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassExample17extendsJFrame{privateButtonGroupgroup单选按钮组对象privateJPanel JPanel面板放置三个JRadioButton按privateJPanel JPanel面板作为调色publicExample17(){pallet=newJPanel();this.add(palletBorderLayout.CENTER将调色板面板放置了CENTER域panel=newJPanel();group=newButtonGroup();调用addJRadioButton()方this.add(panel,BorderLayout.NORTH);this.setSize(300,300);}@paramJRadioButtionJRadioButton将按钮添加到panel面板和ButtonGroup按钮组中并添 privatevoidaddJRadioButton(finalStringtext){JRadioButtonradioButton=newJRadioButton(text);radioButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Colorcolor=null;if红".equals(textcolor=elseif黄".equals(textcolor=Color.YELLOW;elseif蓝".equals(textcolor=Color.BLUE;}elsecolor=}}}publicstaticvoidmain(String[]args){newExample17();}}9-21图9-211JPanel面板中放置了“红”、“黄”、“蓝”三个单选按钮,并为这JRadioButton(String 创建一个具有文本信息,并指定初始状态(选中 2、练习目3、需求分JComboBox下拉列表框,会将所有选项折叠在一起,当用户点击组合框时,4、设计思路(实现原理importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassExample18extendsJFrameprivateJComboBox定义一个JComboBox组框privateJTextField定义一个JTextField文框publicExample18()JPanelpanelnewJPanel创建JPanel面comboBox=new//为组合框添加选项 comboBox.addItem("");comboBox.addItem("");为组合框添加comboBox.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){ if请选择城市".equals(item}elsefield.setText("您选择的城市是:}}field=newJTextField(20);panel.add(comboBox);//在面板中添加组合框 //在面板中添加文本//在内容面板中添加JPanel面板this.add(panel,BorderLayout.NORTH);this.setSize(350,100);}publicpublicstaticvoidmain(String[]args){newExample18();}}图9-22 boBox组件被称为组合框,它将所有选项折叠收藏在一起,默认显示的是第一 boBox组合框组件分为可编辑和不可编辑两种形式,对于不可编辑的组合框,项中选择,也可以自己输入新的内容。需要注意的是,自己输入的内容只能作为当前 如何创建下拉式菜2、练习目3、需求分建下拉式菜单需要使用三个组件:JBar(菜单栏)、J(菜单)和JItem(菜4、设计思路(实现原理创建JBar菜单栏对象,将其放置在JFrame窗口的顶创建J菜单对象,将其添加到 Bar菜单栏创建JItem菜单项,为菜单项添加 的JDialog窗口,将菜单项添加到J菜单中。importimportjava.awt.event.*;importjavax.swing.*;publicclassExample19extendsJFrame{publicExample19(){ Bar=newJ ne

温馨提示

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

最新文档

评论

0/150

提交评论