




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
就需要使用AWT的处理机制。为了让初学者熟悉AWT的处理机制,本案例将实现关4)通过addWindowListener()方法为源器对象。importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample01publicstaticvoidmain(String[]args)FramefnewFrame("f.setSize(300, MyWindowListenermw=newMyWindowListener();}}classMyWindowListenerimplementsWindowListener publicvoidwindowClosing(WindowEvent{Windowwindow=e.getWindow();}publicvoidwindowActivated(WindowEvente)}publicvoidwindowClosed(WindowEvente)}publicvoidwindowDeactivated(WindowEvente)}publicvoidwindowDeiconified(WindowEvente)}publicvoidwindowIconified(WindowEvente)}publicvoidwindowOpened(WindowEvente)}}图9-1 (Event (Listener 此之间有着非常紧密的联系,接下来用一个图例来描述处理的工作流程,如图9-2所示。 了让初学者熟悉AWT适配器,本案例将使用AWT适配器优化窗体关闭代码。4)重写适配器类中的windowClosing()方法importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample02publicstaticvoidmain(String[]args)FramefnewFrame("f.setSize(300,//为窗口组 }}classMyWindowListenerextendsWindowAdapterpublicvoidwindowClosing(WindowEvent{Windowwindow=(Window) }}图9-3 MyWindowListenerWindowAdapter,同样实现了关闭窗口的功不必要的代码,所以在方法数不超过3个时候,不需要继承适配器类。 窗2)创建WindowListener实例对象。importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample03publicstaticvoidmain(String[]finalFramefnewFrame("窗体");f.setSize(400,300); f.addWindowListener(newpublicvoidwindowOpened(WindowEvente}publicvoidwindowActivated(WindowEvent}publicvoidwindowIconified(WindowEvent}publicvoidwindowDeiconified(WindowEvente)}publicvoidwindowDeactivated(WindowEvent}publicvoidwindowClosing(WindowEvent{System.out.println("窗体正在关闭"); }publicvoidwindowClosed(WindowEvent}}} 在本案例中,通过WindowListener对操作窗口的动作进行,当接收到特定的动作后,就将所触发的名称打印出来。了解了窗体,在以后的编程中,可以根据实际需求,在器中自定义窗importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample04publicstaticvoidmain(String[]finalFramefnewFrame("鼠标f.setSize(300,200);ButtonbutnewButton("按钮 but.addMouseListener(newpublicvoidmouseReleased(MouseEvente}publicvoidmousePressed(MouseEvent}publicvoidmouseExited(MouseEvent}publicvoidmouseEntered(MouseEvent}publicvoidmouseClicked(MouseEvent}图9-5 用鼠标点击按钮,运行结果如图9-6所示。 publicvoidmouseClicked(MouseEvent{if(e.getButton()==e.BUTTON1){ } } }}MouseEventMouseEventgetButton()方法对象的getClickCount()方法获取到。 键importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample05publicstaticvoidmain(String[]{Framef=newFrame("键盘 f.setLayout(newFlowLayout());f.setSize(400,300);TextFieldtfnewTextField(30创建文本框对象f.add(tf);//在窗口中添加文本框组件 tf.addKeyListener(new{publicvoidkeyPressed(KeyEvent{intKeyCodee.getKeyCodeStringsKeyEvent.getKeyText(KeyCode返回按键的字符串描述System.out.print("输入内容为:"+s+",");System.out.println("KeyCode为:"+KeyCode);}} 本案例中,当在图9-7中的文件框中键入字符时,便触发了键盘。这时,KeyEvent类通过调用getKeyCode()方法将输入内容对应的整数值返回,即keyCode。在KeyEvent类中还有一个静态方法getKeyText(intkeyCode),它可以将按键内容以String形式返回。 右放置。为了让初学者熟悉流式布局管理器,本案例将演示FlowLayout布局管理器的用法。2)设置窗体中的布局管理器为FlowLayout。importjava.awt.*;importimportjava.awt.*;importjava.awt.event.*;publicclassExample06publicstaticvoidmain(String[]args)finalFramefnewFrame("流式布局f.setLayout(newFlowLayout(FlowLayout.LEFT,25,25));f.setSize(200 , Buttonbut1newButton("11 but1.addActionListener(newActionListener(){//动作 为第1个按钮添加点击privateintnum=1; //定义变量num,记录按钮的个数publicvoidactionPerformed(ActionEvente){ }}} 1、FlowLayout布局管理器的特点就是可以将所有组件像流水一样依次进行排列,不需要用户明确的位案例9-7 BorderLayout布局管理器分别是东(EAST)、南(SOUTH)、西(WEST)、北(NORTH)、中(CENTER)。为了让初学者熟悉BorderLayout布局,本案例演示一下BorderLayout布局管理器对组件布局的效果。2)设置窗体中的布局管理器为BorderLayoutimportjava.awt.*;publicimportjava.awt.*;publicclassExample07{publicstaticvoidmain(String[]finalFramefnewFrame("边界布局f.setLayout(new Buttonbut2newButton("西");Buttonbut3newButton("南");Buttonbut4newButton("北");Buttonbut5newButton("中"); }} 布局的效果如图9-11所示。 BorderLayout的好处就是可以限定各区域的边界,当用户改变容器窗口大小时,各个组件的相对位置importjava.awt.*;publicimportjava.awt.*;publicclassExample08{publicstaticvoidmain(String[]FramefnewFrame("网格布局f.setSize(300,300); //设置窗体大小f.setLocation(400,300);for(inti=1;i<=12;i++)ButtonbtnnewButton("btni);f.add(btn);//向窗体中添加按钮}}} 2、GridLayout布局管理器的特点是组件的相对位置不随区域的缩放而改变,但组件的大小会随之改GridBagLayout(网格包布局管理器)GridLayout布局个网格。为了让初学者熟悉GridBagLayout的布局,本案例将来演示GridBagLayout的用法。importclassLayoutextends{publicLayout(String{GridBagLayoutlayout=newGridBagLayout();GridBagConstraintsc=newGridBagConstraints();c.fillGridBagConstraints.BOTHc.weightx1c.weighty1ponent("btn1",layout,c);ponent("btn2",layout,c);ponent("btn3",layout,ponent("btn4",layout,c.weightx0c.weighty=0;//0ponent("btn5layoutc);c.gridwidth1;设置组件跨一个网格(默认值)ponent("btn6",layout,c);ponent("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(namename}}publicclassExample09publicstaticvoidmain(String[]{new}}
GridBagLayoutlayout=GridBagLayoutlayout=newGridBagConstraintsGridBagConstraintsconstraints=newGridBagConstraints();constraints.gridx=1; //设置网格的左上角横向索引constraints.gridy=1; //设置网格的左上角纵向索引constraints.gridwidth=1;//设置组件横向 constraints.gridheight=1;//设置组件纵 重复2、3、4步骤。 卡片,而管理这些卡片的布局管理器就是卡片布局管理器(CardLayout)。为了让初学者熟悉CardLayout布局管理器,本案例将演示CardLayout中常用方法的使用。Example10Cardlayoutimportjava.awt.*;importjavax.swing.*;importjava.awt.*;importjavax.swing.*;classCardlayoutextendsFrameimplementsActionListenerPanelcardPanel=newPanel(); PanelcontrolpaPanel=newPanel();//定义Panel面板放置按钮Buttonnextbutton,preButton;CardLayoutcardLayoutnewCardLayoutpublicCardlayout //setSize(300,200);器this.addWindowListener(new{publicvoidwindowClosing(WindowEvente){} ////在cardPanel面板对象中添加2个文本cardPanel.add(newLabel(" 播客",Label.CENTER));cardPanel.add(newLabel(" 程序员",Label.CENTER));nextbuttonnewButton("下一张卡片");preButton=newButton("上一张卡片//为按钮对象 controlpaPanel中} publicvoidactionPerformed(ActionEvente)nextbutton,执行的语句ife.getSourcenextbutton//}if(e.getSource()==preButton)//}}}publicclassExample10publicstaticvoidmain(String[]{Cardlayoutcardlayout=new}} 的整个区域。在CardLayout布局管理中经常会用到下面几个方法,如下表所示。String案例9-11 本案例将演示如何使用Graphics类中的方法。设计类MyPanel继承类Panel,用于画。3) importjava.awt.*;importjava.awt.event.*;importjava.util.Random;publicclassExample11publicstaticvoidmain(String[]args)finalFrameframe=newFrame(" ");//创建Frame对象finalPanelpanel=newMyPanel(); //创建Canvas对象//将Frame窗口居中}}classMyPanelextends{publicvoidpaint(Graphics{intwidth=200;//定 intheight=60;//定义 //设置上下文颜色g.fillRect(0,0,width,height); //填充 //设置上下文颜色g.drawRect(0,0,width-1,height-1);//绘制边框Randomr=newfor(inti=0;i<100;{intx=r.nextInt(width)-2;inty=r.nextInt(height)-2;g.drawOval(x,y,2,2);}g.setFont(newFont("黑体",Font.BOLD,30));//设 //设 char[]chars= +StringBuildersb=newStringBuilder();for(inti=0;i<4;i++){intpos=r.nextInt(chars.length);charc=chars[pos];sb.append(c+"}g.drawString(sb.toString2030}}} 表列出了Graphic类中常用的方法。voiddrawLine(intx1,intintx2,intvoiddrawRect(intx,inty,intintxxwidthyyvoiddrawOval(intx,inty,intwidth,intheight)width和height参数指定的矩形中。椭圆覆盖区域的宽度为width1height1voidfillRect(intx,inty,intintxxwidth1yyheightintvoiddrawString(Stringstr,intx,int(x,y)案例9-12 JDialog框可分为两种:模态框和非模态框。所谓模态框是指用户需要等到处理完框后才能继续与其它窗互,为了方便初学者掌握Jdialog类的使用,本案例将演示JDialog框中的模态框的使用。importjava.awt.*;importjava.awt.*;importjavax.swing.*;publicclassExample12{publicstaticvoidmain(String[]{JButtonbtn=newJButton("模式 JFramef=newJFrame("模式 f.setSize(300,250);f.setLayout(newFlowLayout());//设置点击关闭按钮默认关闭窗口finalJLabellabel=newfinalJDialogdialognewJDialog(fdialog.setLocation(350,250);dialog.setLayout(newFlowLayout());finalJButtonbtn2newJButton("确定");dialog.add(btn2);// 框 btn.addActionListener(new{publicvoidactionPerformed(ActionEvent{//设 if ponents().length=={} ////显 }}} Dialog案例9-13 让初学者掌握JFrame组件的使用,本案例将演示JFrame的效果。importjava.awt.event.*;importjavax.swing.*;publicimportjava.awt.event.*;importjavax.swing.*;publicclassExample13extendsJFrame{publicthis.setSize(300,200);JButtonbtnewJButton("关闭窗口//设置点击关闭按钮时的默认操作}publicstaticvoidmain(String[]{newExample13}} JFrame类创建了一个窗体,并向该窗体中添加了一个按钮。JFrameFrame类最大的区别在于,JFrame类提供了关闭窗口的功能,在程序中不需要添加窗体器,只需调用setDefaultCloseOperation()JFrame.EIXT_ON_CLOSE作为参数传入即可,该参数表示点 JScrollPane是一个带有滚动条的面板容器,而且这个面板只能添加一个组件,如果想往中。为了让初学者掌握JScrollPane面板容器的使用,本案例将演示一下向中间容器添加按钮。importjava.awt.*;importjava.awt.*;publicclassExample14extends{publicExample14(){JScrollPanescrollPane=new//设置水平滚动条策略--滚动条一直显示JPanelpanel=new//将滚动面板添加到内容面板的CENTER区域this.add(scrollPane,BorderLayout.CENTER);this.setSize(400,250);}publicstaticvoidmain(String[]{newExample14}} 单独存在,只能放置在顶级窗口中。其中最常见的中间容器有两种:JPanel和JScrollPvsb,inthsb)参数vsb和hsb分别表示垂直滚动条策略和水平滚动ScrollPaneConstants的静态常量,如下所示:void 本组件的使用,本案例将演示一下文本组件JTextField和JTextArea组件的使用。importjava.awt.*;importjava.awt.*;importjavax.swing.*;publicclassExample15extends{JButtonsendBt;JTextFieldinputField;JTextAreachatContent;publicExample15(){chatContentnewJTextArea(1234JScrollPanesho nelJScrollPanesho nel=newJScrollPane(chatContent); //设置文本域不可编辑JPanelinputPanel=newJPanel();//创建一个JPanel面板inputField=newJTextField(20);//创建一个文本框sendBt=newJButton("发送"); //创建一个发送按钮StringcontentinputField.getTextif(content!=null&&!content.trim().equals(""))chatContent.append(content+}else 内容不能为空} }Labellabel=newLabel("编辑//JPanel将文本框添加到JPanelJPanelJPanelJFrame this.setSize(400,300);}publicstaticvoidmain(String[]{newExample15}} 象时可以设定区域的行数、列数,接下来介绍一下JTextArea常用的构造方法,如表9-11所示。 案例9-16 JCheckBox组件被称为复选框,它有选中(是)/未选中(非)两种状态,如果用户想接收的输入importjava.awt.*;importjava.awt.*;importjavax.swing.*;publicclassExample16extends{privateJCheckBoxitalic;privateJCheckBoxbold;privateJLabellabel;publicExample16(){ labelnewJLabel("请选择字体样式//设 label.setFont(newFont("宋体",Font. IN,20)); //在CENTER域添加JPanelpanelnewJPanel创建一个JPanel//创建两个JCheckBox复选框italicnewJCheckBox("斜体");bold=newJCheckBox("黑体"); ActionListenerlistener=new{publicvoidactionPerformed(ActionEvente){intmode=0;mode+=mode+=label.setFont(newFont("宋体mode}//为两个复选框添加 this.add(panel,BorderLayout.SOUTH);this.setSize(300,300);}publicstaticvoidmain(String[]{newExample16}} 1、本案例中,在JPanel面板中放置了“黑体”和“斜体”两个复选框,并为这两个按钮事两个复选框时“请选择字体样式”所显示的字体样式。案例9-17 JRadioButtonJRadioButton按钮来说,件的使用,本案例将演示一下JRadioButton组件的用法。importjava.awt.*;importjavax.swing.*;importjava.awt.*;importjavax.swing.*;publicclassExample17extendsprivateButtonGroupgroup单选按钮组对privateJPanel privateJPanel public{pallet=newpanel=newJPanel();group=newButtonGroup();//调用addJRadioButton()方法this.setSize(300,300);}@param privatevoidaddJRadioButton(finalString{JRadioButtonradioButton=newpublicpublicvoidactionPerformed(ActionEvent{Colorcolor=null;if红".equals(textcolor=elseif黄{color=elseif蓝{color=}elsecolor=}}}publicstaticvoidmain(String[]{newExample17}} 1JPanel面板中放置了“红”、“黄”、“蓝”三个单选按钮,并为这三个按钮 JComboBox下拉列表框,本案例将演示JComboBox下拉列表框的用法。importjava.awt.*;importjavax.swing.*;publicclassExample18extendsJFrameprivateJComboBoxcomboBox;privateJTextFieldfield;publicExample18(){
JPanelpanelnewJPanelJPanelcomboBox=new {publicvoidactionPerformed(ActionEvente)StringitemStringcomboBox.getSelectedItem();if("请选择城市".equals(item)){}}elsefield.setText("您选择的城市是:}}field=newJTextField(20);panel.add(comboBox);//在面板中添加组合框 //在面板中添加文本//在内容面板中添加JPanel面板this.add(panel,BorderLayout.NORTH);this.setSize(350,100);}publicstaticvoidmain(String[]{newExample18}} 、、boBox组合框组件分为可编辑和不可编辑两种形式,对于不可编辑的组合框,用户只能在现 要使用三个组件:JBar(菜单栏)、J(菜单)和JItem(菜单项),为了让初学者熟悉下拉菜 创建JItem菜单项
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度房地产项目税收筹划合同范本
- 《小数除法-解决问题》(教学设计)-2024-2025学年五年级上册数学北京版
- 电动自行车共享项目可行性分析报告
- Unit 1 You and me. SectionB1a-2b读写课教学设计 2024-2025学年人教版英语七年级上册
- 2025年度二手车交易售后服务跟踪合同
- 2025年哑光漆项目可行性研究报告
- 2025年镜下分析岩样盘行业深度研究分析报告
- 2025年建筑安装服务合作协议书
- 中国天然香料行业市场发展监测及投资前景展望报告
- 2025年度物流行业短信跟踪服务合同
- 变频器技术(全套课件)
- 公路工程工程量清单(全)
- 旧金山china town中文介绍课件
- (中职)表面组装技术(SMT工艺)教学课件
- 初中英语 沪教牛津版 9A U7-1 Reading Tom Sawyer paints the fence 课件
- 骗提个人住房公积金检讨书
- 监控系统维保方案计划及报价
- ABCD2评分量表(TIA早期卒中风险预测工具)
- E-learning平台使用手册(培训管理员版)
- 自动化物料编码规则
- 小学音乐教材分析
评论
0/150
提交评论