




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
PAGEPAGE2授课内容学时分配教学方法与手段进度计划(周次)课堂讲授(学时)上机实践(学时)Java语言入门标识符、关键字和数据类型运算符、表达式和语句6421~2类与对象141042~5子类与继承6425-7接口与实现4227-8内部类与异常类2208常用实用类6429-10组件及事件处理64210-11输入、输出流22012JDBC与MySQL数据库42212-13Java多线程机制22013Java网络的基本知识22014总结22014总学时=SUM(ABOVE)56=SUM(ABOVE)40=SUM(ABOVE)16
各章教学实施计划章节题目第9章组件及事件处理9.1节-9.4.2节学时2教学目的、要求(分了解、理解、掌握三个层次):1、了解JavaSwing概述。2、掌握常用组(JFrame类、菜单条、菜单、菜单项)件与布局。3、掌握事件处理的模式。4、掌握ActionEvent事件、ItemEvent事件的处理。教学内容(包括基本内容、重点、难点):基本内容:JavaSwing概述。常用组(JFrame类、菜单条、菜单、菜单项)件与布局。事件处理的模式。ActionEvent事件、ItemEvent事件的处理。重点:窗口JFrame常用方法、常用组件与布局、事件处理模式。难点:事件处理模式(9.4.2)。讨论、思考题、作业:习题92(1),(3)预习第9章9.4.3-9.12节参考书目(含参考书、文献等)具体内容:参考书对应章节
课堂教学实施计划第15课教学过程设计:复习分钟;授新课95分钟讨论5分钟;其它分钟授课类型(请打√):理论课√讨论课□实验课□习题课□其它□教学方式(请打√):讲授√讨论□示教□指导□其它□教学手段(请打√):多媒体模型□实物□挂图□音像□其它√主要内容基本内容:JavaSwing概述。常用组(JFrame类、菜单条、菜单、菜单项)件与布局。事件处理的模式。ActionEvent事件、ItemEvent事件的处理。重点:窗口JFrame常用方法、常用组件与布局、事件处理模式。难点:事件处理模式(9.4.2)。第9章组件及事件处理§9.1JavaSwing概述通过图形用户界面(GUI:GraphicsUserInterface),用户和程序之间可以方便地进行交互。Java的Swing工具包中包含了许多类来支持GUI设计。如:按钮、菜单、列表、文本框等组件类,同时它还包含窗口、面板等容器类。javax.swing包提供了功能更为强大的用来设计GUI的类。在学习GUI编程时,必须要很好的掌握两个概念:容器类和组件类。javax.swing包中JComponent类是java.awt包中Container类的一个直接子类、是java.awt包中Component类的一个间接子类,学习GUI编程主要是学习掌握使用Component类的一些重要的子类及其使用方法。以下是GUI编程经常提到的基本知识点。Java把Component类的子类或间接子类创建的对象称为一个组件.Java把Container的子类或间接子类创建的对象称为一个容器.可以向容器添加组件。Container类提供了一个public方法:add(),一个容器可以调用这个方法将组件添加到该容器中。容器调用removeAll()方法可以移掉容器中的全部组件;调用remove(Componentc)方法可以移掉容器中参数c指定的组件。注意到容器本身也是一个组件,因此可以把一个容器添加到另一个容器中实现容器的嵌套。每当容器添加新的组件或移掉组件时,应当让容器调用validate()方法,以保证容器中的组件能正确显示出来如:JFramef=newJFrame();JButtonbutton=newJButton("确定")f.add(button);…….f.remove(button);§9.2窗口Java提供的JFrame类的实例是一个底层容器,即通常所称的窗口。其他组件必须被添加到底层容器中,以便借助这个地层容器和操作系统进行信息交互。JFrame类是Container类的间接子类。当需要一个窗口时,可使用JFrame或其子类创建一个对象。窗口也是一个容器,可以向窗口添加组件。需要注意的是,窗口默认地被系统添加到显示器屏幕上,因此不允许将一个窗口添加到另一个容器中。§9.2.1JFrame常用方法JFrame构造方法:JFrame()创建一个无标题的窗口。JFrame(Strings)创建标题为s的窗口。常用方法:publicvoidsetBounds(inta,intb,intwidth,intheight设置窗口的初始位置是(a,b),即距屏幕左面a个像素、距屏幕上方b个像素;窗口的宽是width,高是height。publicvoidsetSize(intwidth,intheight)设置窗口的大小。publicvoidsetLocation(intx,inty)设置窗口的位置,默认位置是(0,0)。publicvoidsetVisible(booleanb)设置窗口是否可见,窗口默认是不可见的。publicvoidsetResizable(booleanb)设置窗口是否可调整大小,默认可调整大小。publicvoiddispose()撤消当前窗口,并释放当前窗口所使用的资源。等。例子1Example9_1.javaimportjavax.swing.*;importjava.awt.*;publicclassExample9_1{publicstaticvoidmain(Stringargs[]){JFramewindow1=newJFrame("第一个窗口");JFramewindow2=newJFrame("第二个窗口");Containercon=window1.getContentPane();con.setBackground(Color.yellow); //设置窗口的背景色window1.setBounds(60,100,188,108);//设置窗口在屏幕上的位置及大小window2.setBounds(260,100,188,108);window1.setVisible(true);window1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //释放当前窗口window2.setVisible(true);window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //退出程序}}§9.2.2菜单条、菜单、菜单项菜单条、菜单、菜单项是窗口常用的组件,菜单放在菜单条里,菜单项放在菜单里。1.菜单条2.菜单3.菜单项4.嵌入子菜单5.菜单上的图标例子2在主类的main方法中用JFrame的子类创建一个含有菜单的窗口,效果如图9.3所示。Example9_2.javapublicclassExample9_2{publicstaticvoidmain(Stringargs[]){WindowMenuwin=newWindowMenu("带菜单的窗口",20,30,200,190);}}WindowMenu.javaimportjavax.swing.*;importjava.awt.event.InputEvent;importjava.awt.event.KeyEvent;importstaticjavax.swing.JFrame.*;publicclassWindowMenuextendsJFrame{//JFrame的子类JMenuBarmenubar;JMenumenu,subMenu;JMenuItemitem1,item2;publicWindowMenu(){}publicWindowMenu(Strings,intx,inty,intw,inth){init(s);setLocation(x,y);setSize(w,h);setVisible(true);setDefaultCloseOperation(DISPOSE_ON_CLOSE);}voidinit(Strings){setTitle(s);menubar=newJMenuBar();menu=newJMenu("菜单");subMenu=newJMenu("软件项目");item1=newJMenuItem("Java话题",newImageIcon("a.gif"));item2=newJMenuItem("动画话题",newImageIcon("b.gif"));item1.setAccelerator(KeyStroke.getKeyStroke('A'));item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));menu.add(item1);menu.addSeparator();menu.add(item2);menu.add(subMenu);subMenu.add(newJMenuItem("汽车销售系统",newImageIcon("c.gif")));subMenu.add(newJMenuItem("农场信息系统",newImageIcon("d.gif")));menubar.add(menu);setJMenuBar(menubar);}}§9.3常用组件与布局可以使用JComponent的子类JTextField创建各种组件。利用组件可以完成应用程序与用户的交互及事件处理等。也可以在命令行窗口反编译组件即时查看组件所具有的属性及常用方法,例如:C:\>javapjavax.swing.JComponent也可以查看类库帮助文档.例如下载Java类库帮助文档:jdk-6-doc.zip。§9.3.1常用组件1.文本框:由JComponent的子类JTextField创建文本框。2.文本区:由JComponent的子类JTexArea创建文本区。3.按钮:由JComponent的子类JButton类用来创建按钮。4.标签:由JComponent的子类JLabel类用来创建标签。5.选择框:由JComponent的子类JCheckBox类用来创建选择框6.单选按钮:由JComponent的子类JRadioButton类用来创建单项选择框。7.下拉列表:由JComponent的子类JComboBox类用来创建下拉列表。8.密码框:由JComponent的子类JPasswordField创建密码框.例子3Example9_3.javapublicclassExample9_3{publicstaticvoidmain(Stringargs[]){ComponentInWindowwin=newComponentInWindow();win.setBounds(100,100,310,260);win.setTitle("常用组件");}}ComponentInWindow.javaimportjava.awt.*;importjavax.swing.*;publicclassComponentInWindowextendsJFrame{JCheckBoxcheckBox1,checkBox2;//选择框JRadioButtonradioM,radioF;//单选框ButtonGroupgroup;JComboBox<String>comBox;//下拉列表publicComponentInWindow(){init();setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}voidinit(){setLayout(newFlowLayout());comBox=newJComboBox<String>();checkBox1=newJCheckBox("喜欢音乐");checkBox2=newJCheckBox("喜欢旅游");group=newButtonGroup();radioM=newJRadioButton("男");radioF=newJRadioButton("女");group.add(radioM);group.add(radioF);//归组才能实现单选add(checkBox1);add(checkBox2);add(radioM);add(radioF);comBox.addItem("音乐天地");comBox.addItem("武术天地");add(comBox);}}§9.3.2常用容器JComponent是Container的子类,因此JComponent子类创建的组件也都是容器。容器经常用来添加组件。JFrame是底层容器,本节提到的容器被习惯地称做中间容器,中间容器必须被添加到底层容器中才能发挥作用。1.JPanel面板2.JTabbedPane选项卡窗格3.滚动窗格JScrollPane4.拆分窗格JSplitPane5.JLayeredPane分层窗格§9.3.3常用布局当把组件添加到容器中时,希望控制组件在容器中的位置,这就需要学习布局设计的知识。我们将分别介绍java.awt包中的FlowLayout、BorderLayout、CardLayout、GridLayout布局类和java.swing.border包中的BoxLayout布局类。容器可以使用方法:setLayout(布局对象);来设置自己的布局,控制组件在容器中的位置FlowLayout布局,是JPanel型容器的默认布局。(重点介绍)BorderLayout布局(简单介绍)CardLayout布局(简单介绍)GridLayout布局(建议学生自主学习)null布局(简单介绍)BoxLayout布局(建议学生自主学习)例子4Example9_4.javapublicclassExample9_4{publicstaticvoidmain(Stringargs[]){newShowLayout();}}ShowLayout.javaimportjava.awt.*;importjavax.swing.*;publicclassShowLayoutextendsJFrame{PanelGridLayoutpannelGrid;//网格布局的面板PanelNullLayoutpanelNull;//空布局的面板JTabbedPanep;//选项卡窗格ShowLayout(){pannelGrid=newPanelGridLayout();panelNull=newPanelNullLayout();p=newJTabbedPane();p.add("网格布局的面板",pannelGrid);p.add("空布局的面板",panelNull);add(p,BorderLayout.CENTER);add(newJButton("窗体是BorderLayout布局"),BorderLayout.NORTH);add(newJButton("南"),BorderLayout.SOUTH);add(newJButton("西"),BorderLayout.WEST);add(newJButton("东"),BorderLayout.EAST);setBounds(10,10,570,390);setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);validate();}}PanelGridLayout.javaimportjava.awt.*;importjavax.swing.*;publicclassPanelGridLayoutextendsJPanel{PanelGridLayout(){GridLayoutgrid=newGridLayout(12,12);//网格布局setLayout(grid);Labellabel[][]=newLabel[12][12];for(inti=0;i<12;i++){for(intj=0;j<12;j++){label[i][j]=newLabel();if((i+j)%2==0)label[i][j].setBackground(Color.black);elselabel[i][j].setBackground(Color.white);add(label[i][j]);}}}}PanelNullLayout.javaimportjavax.swing.*;publicclassPanelNullLayoutextendsJPanel{JButtonbutton;JTextFieldtext;PanelNullLayout(){setLayout(null);//空布局button=newJButton("确定");text=newJTextField();add(text);add(button);text.setBounds(100,30,90,30);button.setBounds(190,30,66,30);}}下面的例子5中,有两个列型盒式容器boxVOne、boxVTwo和一个行型盒式容器boxH。将boxVOne、boxVTwo添加到boxH中,并在它们之间添加了水平支撑。例子5Example9_5.javapublicclassExample9_5{publicstaticvoidmain(Stringargs[]){WindowBoxLayoutwin=newWindowBoxLayout();win.setBounds(100,100,310,260);win.setTitle("嵌套盒式布局容器");}}WindowBoxLayout.javaimportjavax.swing.*;publicclassWindowBoxLayoutextendsJFrame{BoxboxH;//行式盒BoxboxVOne,boxVTwo;//列式盒publicWindowBoxLayout(){setLayout(newjava.awt.FlowLayout());init();setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}voidinit(){boxH=Box.createHorizontalBox();boxVOne=Box.createVerticalBox();boxVTwo=Box.createVerticalBox();boxVOne.add(newJLabel("姓名:"));boxVOne.add(newJLabel("职业:"));boxVTwo.add(newJTextField(10));boxVTwo.add(newJTextField(10));boxH.add(boxVOne);boxH.add(Box.createHorizontalStrut(10));boxH.add(boxVTwo);add(boxH);}}§9.4处理事件(重点)学习组件除了要熟悉组件的属性和功能外,一个更重要的方面是学习怎样处理组件上发生的界面事件。当用户在文本框中键入文本后按回车键、单击按钮、在一个下拉式列表中选择一个条目等操作时,都发生界面事件。程序有时需对发生的事件作出反应,来实现特定的任务,例如,用户单击一个名字叫“确定”或名字叫“取消”的按钮,程序可能需要作出不同的处理。§9.4.1事件处理模式在学习处理事件时,必须很好地掌握事件源、监视器、处理事件的接口这三个概念。1.事件源2.监视器3.处理事件的接口§9.4.2ActionEvent事件(重点,难点)1.ActionEvent事件源文本框、按钮、菜单项、密码框和单选按钮都可以触发ActionEvent事件,即都可以成为ActionEvent事件的事件源。2.注册监视器Java规定能触发ActionEvent事件的组件使用方法addActionListener(ActionListenerlisten)将实现ActionListener接口的类的实例注册为事件源的监视器。3.ActionListener接口方法publicvoidactionPerformed(ActionEvente)。4.ActionEvent类中的方法ActionEvent类有如下常用的方法:publicObjectgetSource()publicStringgetActionCommand()ActionEvent下面的例子6处理文本框上触发的ActionEvent事件。在文本框text中输入字符串回车,监视器负责计算字符串的长度,并在命令行窗口显示字符串的长度。例子6Example9_6.javapublicclassExample9_6{publicstaticvoidmain(Stringargs[]){WindowActionEventwin=newWindowActionEvent();win.setTitle("处理ActionEvent事件");win.setBounds(100,100,310,260);}}WindowActionEvent.javaimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassWindowActionEventextendsJFrame{JTextFieldtext;ActionListenerlistener;//listener是监视器publicWindowActionEvent(){setLayout(newFlowLayout());text=newJTextField(10);add(text);listener=newReaderListen();//创建监视器text.addActionListener(listener);//text将listener注册为自己的监视器setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}ReaderListen.javaimportjava.awt.event.*;publicclassReaderListenimplementsActionListener{//负责创建监视器的类publicvoidactionPerformed(ActionEvente){Stringstr=e.getActionCommand();//获取封装在事件中的“命令”字符串System.out.println(str+":"+str.length());}}下面例子7中的监视器PoliceListen与例子6中的ReaderListen略有不同,PoliceListen类实现了ActionListerner接口的子接口MyCommand-Listener(我们自己写的一个接口)。例子7Example9_7.javapublicclassExample9_7{publicstaticvoidmain(Stringargs[]){WindowActionEventwin=newWindowActionEvent();PoliceListenpolice=newPoliceListen(); //创建监视器win.setMyCommandListener(police); //窗口组合监视器win.setBounds(100,100,460,360);win.setTitle("处理ActionEvent事件");}}WindowActionEvent.javaimportjava.awt.*;importjavax.swing.*;publicclassWindowActionEventextendsJFrame{JTextFieldinputText;JTextAreatextShow;JButtonbutton;MyCommandListenerlistener;publicWindowActionEvent(){init();setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}voidinit(){setLayout(newFlowLayout());inputText=newJTextField(10);button=newJButton("确定");textShow=newJTextArea(9,30);add(inputText);add(button);add(newJScrollPane(textShow));}voidsetMyCommandListener(MyCommandListenerlistener){this.listener=listener;listener.setJTextField(inputText);listener.setJTextArea(textShow);inputText.addActionListener(listener); //inputText是事件源,listener是监视器button.addActionListener(listener); //button是事件源,listener是监视器}}MyCommandListener.javaimportjavax.swing.*;importjava.awt.event.*;publicvoidsetJTextField(JTextFieldtext);publicvoidsetJTextArea(JTextAreaarea);}PoliceListen.javaimportjava.awt.event.*;importjavax.swing.*;JTextFieldtextInput;JTextAreatextShow;publicvoidsetJTextField(JTextFieldtext){textInput=text;}publicvoidsetJTextArea(JTextAreaarea){textShow=area;}publicvoidactionPerformed(ActionEvente){Stringstr=textInput.getText();textShow.append(str+"的长度:"+str.length()+"\n");}}本次课总结(1)掌握怎样将其他组件嵌套到JFrame窗体中。(2)掌握各种组件的特点和使用方法。(3)本章重点掌握组件上的事件处理,Java处理事件的模式是:事件源、监视器、处理事件的接口。作业习题92(1),(3)
预习第9章9.4.3-9.4.10节本次课结束
各章教学实施计划章节题目第9章组件及事件处理9.4.3节-9.12节学时2教学目的、要求(分了解、理解、掌握三个层次):1、掌握事件处理的模式。2、使用MVC结构。3、掌握对话框。4、了解树组件与表格组件、按钮绑定到键盘。5、打印组件、发布GUI程序。教学内容(包括基本内容、重点、难点):基本内容:处理事件使用MVC结构对话框树组件与表格组件按钮绑定到键盘发布GUI程序重点:事件处理模式。难点:匿名类实例或窗口做监视器(9.4.9)。讨论、思考题、作业:习题91(1)-(6)2(2)3(1)-(3)课后调试例子19--例子25代码预习第10章参考书目(含参考书、文献等)具体内容:参考书对应章节
课堂教学实施计划第16课教学过程设计:复习5分钟;授新课90分钟讨论5分钟;其它分钟授课类型(请打√):理论课√讨论课□实验课□习题课□其它□教学方式(请打√):讲授√讨论□示教□指导□其它□教学手段(请打√):多媒体模型□实物□挂图□音像□其它√主要内容基本内容:处理事件使用MVC结构对话框树组件与表格组件按钮绑定到键盘发布GUI程序重点:事件处理模式。难点:匿名类实例或窗口做监视器(9.4.9)。§9.4.3ItemEvent事件(重点讲解,使学生进一步理解事件处理)1.ItemEvent事件源选择框、下拉列表都可以触发ItemEvent事件。2.注册监视器3.ItemListener接口方法publicvoiditemStateChanged(ItemEvente)。例子8Example9_8.javapublicclassExample9_8{publicstaticvoidmain(Stringargs[]){WindowOperationwin=newWindowOperation();win.setBounds(100,100,390,360);win.setTitle("简单计算器");}}WindowOperation.javaimportjava.awt.*;importjavax.swing.*;importjava.io.*;publicclassWindowOperationextendsJFrame{JTextFieldinputNumberOne,inputNumberTwo;JComboBoxchoiceFuhao;JTextAreatextShow;JButtonbutton;OperatorListeneroperator; //监视ItemEvent事件的监视器ComputerListenercomputer; //监视ActionEvent事件的监视器publicWindowOperation(){init();setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}voidinit(){setLayout(newFlowLayout());inputNumberOne=newJTextField(5);inputNumberTwo=newJTextField(5);choiceFuhao=newJComboBox();button=newJButton("计算");choiceFuhao.addItem("选择运算符号:");String[]a={"+","-","*","/"};for(inti=0;i<a.length;i++){choiceFuhao.addItem(a[i]);}textShow=newJTextArea(9,30);operator=newOperatorListener();computer=newComputerListener();operator.setJComboBox(choiceFuhao);operator.setWorkTogether(computer);computer.setJTextFieldOne(inputNumberOne);computer.setJTextFieldTwo(inputNumberTwo);computer.setJTextArea(textShow);choiceFuhao.addItemListener(operator); //choiceFuhao是事件源,operator是监视器button.addActionListener(computer);//button是事件源,computer是监视器add(inputNumberOne);add(choiceFuhao);add(inputNumberTwo);add(button);add(newJScrollPane(textShow));}}OperatorListener.javaimportjava.awt.event.*;importjavax.swing.*;publicclassOperatorListenerimplementsItemListener{JComboBoxchoice;ComputerListenerworkTogether;publicvoidsetJComboBox(JComboBoxbox){choice=box;}publicvoidsetWorkTogether(ComputerListenercomputer){workTogether=computer;}publicvoiditemStateChanged(ItemEvente){Stringfuhao=choice.getSelectedItem().toString();workTogether.setFuhao(fuhao);}}ComputerListener.javaimportjava.awt.event.*;importjavax.swing.*;publicclassComputerListenerimplementsActionListener{JTextFieldinputNumberOne,inputNumberTwo;JTextAreatextShow;Stringfuhao;publicvoidsetJTextFieldOne(JTextFieldt){inputNumberOne=t;}publicvoidsetJTextFieldTwo(JTextFieldt){inputNumberTwo=t;}publicvoidsetJTextArea(JTextAreaarea){textShow=area;}publicvoidsetFuhao(Strings){fuhao=s;}publicvoidactionPerformed(ActionEvente){try{doublenumber1=Double.parseDouble(inputNumberOne.getText());doublenumber2=Double.parseDouble(inputNumberTwo.getText());doubleresult=0;if(fuhao.equals("+")){result=number1+number2;}elseif(fuhao.equals("-")){result=number1-number2;}elseif(fuhao.equals("*")){result=number1*number2;}elseif(fuhao.equals("/")){result=number1/number2;}textShow.append(number1+""+fuhao+""+number2+"="+result+"\n");}catch(Exceptionexp){textShow.append("\n请输入数字字符\n");}}}§9.4.4DocumentEvent事件(在9.4.3的基础上简单讲解)1.DocumentEvent事件源2.注册监视器3.DocumentListener接口DocumentListener接口在javax.swing.event包中,该接口中有三个方法:publicvoidchangedUpdate(DocumentEvente)publicvoidremoveUpdate(DocumentEvente)publicvoidinsertUpdate(DocumentEvente)例子9Example9_9.javapublicclassExample9_9{publicstaticvoidmain(Stringargs[]){WindowDocumentwin=newWindowDocument();win.setBounds(100,100,590,500);win.setTitle("排序单词");}}WindowDocument.javaimportjava.awt.*;importjavax.swing.event.*;importjavax.swing.*;importjava.awt.event.*;publicclassWindowDocumentextendsJFrame{JTextAreainputText,showText;JMenuBarmenubar;JMenumenu;JMenuItemitemCopy,itemCut,itemPaste;TextListenertextChangeListener;//inputText的监视器HandleListenerhandleListener;//itemCopy,itemCut,itemPaste的监视器WindowDocument(){init();setLayout(newFlowLayout());setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}voidinit(){inputText=newJTextArea(15,20);showText=newJTextArea(15,20);showText.setLineWrap(true);//文本自动回行showText.setWrapStyleWord(true);//文本区以单词为界自动换行menubar=newJMenuBar();menu=newJMenu("编辑");itemCopy=newJMenuItem("复制(C)");itemCut=newJMenuItem("剪切(T)");itemPaste=newJMenuItem("粘贴(P)");itemCopy.setAccelerator(KeyStroke.getKeyStroke('c'));//设置快捷方式itemCut.setAccelerator(KeyStroke.getKeyStroke('t'));itemPaste.setAccelerator(KeyStroke.getKeyStroke('p'));//设置快捷方式itemCopy.setActionCommand("copy");itemCut.setActionCommand("cut");itemPaste.setActionCommand("paste");menu.add(itemCopy);menu.add(itemCut);menu.add(itemPaste);menubar.add(menu);setJMenuBar(menubar);add(newJScrollPane(inputText));add(newJScrollPane(showText));textChangeListener=newTextListener();handleListener=newHandleListener();textChangeListener.setInputText(inputText);textChangeListener.setShowText(showText);handleListener.setInputText(inputText);handleListener.setShowText(showText);(inputText.getDocument()).addDocumentListener(textChangeListener); //向文档注册监视器itemCopy.addActionListener(handleListener);//向菜单项注册监视器itemCut.addActionListener(handleListener);itemPaste.addActionListener(handleListener);}}TextListener.javaimportjava.awt.event.*;importjava.io.*;importjavax.swing.event.*;importjavax.swing.*;importjava.util.*;publicclassTextListenerimplementsDocumentListener{JTextAreainputText,showText;publicvoidsetInputText(JTextAreatext){inputText=text;}publicvoidsetShowText(JTextAreatext){showText=text;}publicvoidchangedUpdate(DocumentEvente){Stringstr=inputText.getText();//空格、数字和符号(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)组成的正则表达式Stringregex="[\\s\\d\\p{Punct}]+";Stringwords[]=str.split(regex);Arrays.sort(words);//按字典序从小到大排序showText.setText(null);for(inti=0;i<words.length;i++)showText.append(words[i]+",");}publicvoidremoveUpdate(DocumentEvente){changedUpdate(e);}publicvoidinsertUpdate(DocumentEvente){changedUpdate(e);}}HandleListener.javaimportjava.awt.event.*;importjavax.swing.*;publicclassHandleListenerimplementsActionListener{JTextAreainputText,showText;publicvoidsetInputText(JTextAreatext){inputText=text;}publicvoidsetShowText(JTextAreatext){showText=text;}publicvoidactionPerformed(ActionEvente){Stringstr=e.getActionCommand();if(str.equals("copy"))showText.copy();elsei
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 医疗器械召回程序考核试卷
- 矿山开采环境影响评价技术指南应用考核试卷
- 政府空调管理办法
- 振兴计划管理办法
- 林场消杀管理办法
- 无线门磁管理办法
- 德州草房管理办法
- 数据退役管理办法
- 建设资质管理办法
- 担保信用管理办法
- 选修一人教版单词表
- 预制板装配式道路施工方案
- DeepSeek介绍及其典型使用案例
- 《民用建筑设计》课件
- 配套课件-EDA技术及应用(第四版)
- 手机壳采购销售合同
- 新生儿呼吸窘迫综合征护理查房课件
- CNAS-CL62-2016 检测和校准实验室能力认可准则在基因扩增检测领域的应用说明
- 重氮化工艺操作证考试题库(含答案)
- 对公客户经理培训
- 《服务机器人用锂离子电池和电池组技术规范》(T-CIAPS0021-2023)编制说明
评论
0/150
提交评论