1JAVA语言程序设计周敏彤zhoumintong@suda.edu.cn2第十一讲JavaI/O:读写操作概念JavaI/O类JavaI/O操作标准输入/输出文件读写操作目录管理随机访问文件文件属性3概述发展AWT/Swing/JFC组件分类布局管理界面设计事件处理应用实例设计原则第十二讲Java图形用户界面4概述用户界面(UserInterface)用户与计算机系统(各种程序)交互的接口5GraphicalUserInterfaceNaturalUserInterface1990GUIMultipleWindowsMenus1995InternetHyperlinksSearchEnginesDigitalDecadeXMLWebServicesSmartdevicesNaturalLanguageMultimodal(speech,ink…)PersonalAssistantCommandline1985PCUserInterfaceEvolution-KaiFuLeein20036概述JavaGUI的发展1.AWT(Java1.0)AWT(AbstractWindowToolkit):抽象窗口工具包概念设计实现(about1month)字体设计(四种),界面显示(二流水准)2.Swing(LightweightComponents,Java1.1)SwingwasthecodenameoftheprojectthatdevelopedthenewcomponentsSwingAPI(附加包,Add-onpackage)3.JFC(Java2)JFC(JavaFoundationClasses):Java基础类JFCencompassagroupoffeaturestohelppeoplebuildgraphicaluserinterfaces(GUIs).JFC是指包含在Java2平台内的一整套图形和用户界面技术JFCwasfirstannouncedatthe1997JavaOnedeveloperconference7概述JFC(JavaFoundationClasses)1.AWT(AbstractWindowToolkit)一些用户界面组件(Component)事件响应模型(Event-handlingmodel)布局管理器(Layoutmanager)绘图和图形操作类,如Shape、Font、Color类等2.SwingComponents(Swing组件,JFC的核心)asetofGUIcomponentswithapluggablelookandfeel(包括已有的AWT组件(Button、Scrollbar、Label等)和更高层的组件(如treeview、listbox、tabbedpanes等)ThepluggablelookandfeelletsyoudesignasinglesetofGUIcomponentsthatcanautomaticallyhavethelookandfeelofanyOSplatform(MicrosoftWindows,Solaris,Macintosh).基于Java1.1LightweightUIFramework8概述JFC(JavaFoundationClasses)3.Java2D(advanced2Dgraphicsandimaging)Graphics?Imaging?4.PrintService打印文档、图形、图像设定打印属性和页面属性发现打印机(IPP,InternetPrintingProtocol)9概述JFC(JavaFoundationClasses)5.InputMethodFrameworktexteditingcomponentstocommunicatewithinputmethodsandimplementawell-integratedtextinputuserinterface用Java语言开发输入法6.Accessibility:辅助功能,帮助伤残人士screenreaders,speechrecognitionsystems,refreshablebrailledisplays7.Drag&DropDragandDropenablesdatatransferbothacrossJavaprogramminglanguageandnativeapplications,betweenJavaprogramminglanguageapplications,andwithinasingleJavaprogramminglanguageapplication.10概述发展AWT/Swing/JFC组件分类布局管理界面设计事件处理应用实例设计原则第十二讲Java图形用户界面11组件分类什么是组件?构成图形用户界面的元素,拿来即用用图形表示(能在屏幕上显示,能和用户进行交互)Button、Checkbox、Scrollbar、Choice、Frame12组件分类一些特定的Java类包含在java.awt包和javax.swing包中容器(Container)组件:可包含其他组件顶层容器:Applet,Dialog,Frame,Window一般用途容器:Panel,ScrollPane特定用途容器:InternalFrame非容器组件:必须要包含在容器中Button,Checkbox,Scrollbar,Choice,Canvas13组件分类AWT组件java.awt包ComponentButton,Canvas,Checkbox,Choice,Label,List,ScrollbarTextComponentTextAreaTextFieldContainerPanelWindowScrollPaneDialogFrameMenuComponentMenuBarMenuItem14组件分类Swing组件javax.swing包java.awt.Component|-java.awt.Container|-java.awt.Window|-java.awt.Frame|-javax.swing.JFramejava.awt.Component|-java.awt.Container|-javax.swing.JComponent|-JComboBox,JFileChooser,JInternalFrameJLabel,JList,JMenuBar,JOptionPane,JPanelJPopupMenu,JProgressBar,JScrollBarJScrollPane,JSeparator,JSlider,JSpinnerJSplitPane,JTabbedPane,JTableJTextComponent,JToolBar,JTree等15组件分类应用原则Swing比AWT提供更全面、更丰富的图形界面设计功能Java2平台支持AWT组件,但鼓励用Swing组件主要讲述Swing的图形界面设计16组件分类简单实例importjavax.swing.*;importjava.awt.event.*;publicclassHelloWorldSwing{publicstaticvoidmain(String[]args){JFramef=newJFrame(“Swing1);JLabellabel=newJLabel(Hello!);f.add(label);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setSize(200,200);f.setVisible(true);}}17概述发展AWT/Swing/JFC组件分类布局管理界面设计事件处理应用实例设计原则第十二讲Java图形用户界面18布局管理决定组件在界面中所处的位置和大小六种布局管理器(LayoutManager)两种简单布局java.awt.FlowLayout(JDK1.0)java.awt.GridLayout(JDK1.0)两种特定用途布局java.awt.BorderLayout(JDK1.0)java.awt.CardLayout(JDK1.0)两种灵活布局java.awt.GridBagLayout(JDK1.0)javax.swing.BoxLayout19布局管理FlowLayout(java.awt.FlowLayout)所有组件从左往右排成一行一行排满后转到下一行从左往右排居中、左对齐、右对齐importjava.awt.*;importjavax.swing.*;publicclassFlowWindowextendsJFrame{publicFlowWindow(){setLayout(newFlowLayout());add(newJButton(Button1));add(newJButton(2));add(newJButton(Button3));add(newJButton(Long-NamedButton4));add(newJButton(Button5));}publicstaticvoidmain(Stringargs[]){FlowWindowwin=newFlowWindow();win.setTitle(FlowLayout);win.pack();win.setVisible(true);}}publicvoidpack()可以使窗口的大小改变以适应里面的子组件的排版20布局管理GridLayout(java.awt.GridLayout)将空间划分为由行和列组成的网格单元,每个单元放一个组件,网格单元大小相同(宽度和高度)指定行数和列数importjava.awt.*;importjavax.swing.*;publicclassGridWindowextendsJFrame{publicGridWindow(){setLayout(newGridLayout(0,2));add(newJButton(Button1));add(newJButton(2));add(newJButton(Button3));add(newJButton(Long-NamedButton4));add(newJButton(Button5));}publicstaticvoidmain(Stringargs[]){GridWindowwin=newGridWindow();win.setTitle(FlowLayout);win.pack();win.setVisible(true);}}publicGridLayout(introws,intcols)rows和cols可以是0,这表示所有的组件对象可以在一行或是在一列中BorderLayout(java.awt.BorderLayout)BorderLayout对JFrame是默认的布局管理器上北、下南、左西、右东、中21布局管理ContainercontentPane=getContentPane();//contentPane.setLayout(newBorderLayout());contentPane.add(newJButton(Button1(NORTH)),BorderLayout.NORTH);contentPane.add(newJButton(2(CENTER)),BorderLayout.CENTER);contentPane.add(newJButton(Button3(WEST)),BorderLayout.WEST);contentPane.add(newJBut