编写图形界面应用程序.ppt
《编写图形界面应用程序.ppt》由会员分享,可在线阅读,更多相关《编写图形界面应用程序.ppt(73页珍藏版)》请在三一办公上搜索。
1、1,第九章 编写图形界面应用程序,9.1关于Swing9.2Swing组件和容器9.3布局管理器9.4 事件处理 9.5 绘画与动画显示,2,9.1 关于Swing,1.Swing:是第二代GUI开发工具 它建立在AWT之上,但用新版本的组件替代了旧版本的组件。它提供了许多新的组件和相关的API,第九章 编写图形界面应用程序,3,9.1 关于Swing,2.The Swing API 发布在:JDK1.2 JFC1.1(for use with JDK1.1)3.The Swing API 功能是强大的,灵活的和广泛的。例如 JFC 1.1 有 15 public packages。我们经常用
2、的有两个包:javax.swing javax.swing.event注:AWT components 在 java.awt package 中,第九章 编写图形界面应用程序,4,9.2 Swing组件和容器,9.2.1 样题分析:9.2.2 Swing Components 分类9.2.3 顶层容器9.2.4 Frame9.2.6 Dialog9.2.7 panel9.2.8 创建菜单:9.2.9 JComponent Class,第九章 编写图形界面应用程序,5,9.2 Swing组件和容器,SwingApplication 创建了四个常用的 Swing components:a fram
3、e,or main window(JFrame)a panel(JPanel)a button(JButton)a label(JLabel),第九章 编写图形界面应用程序,9.2.1.样题分析:,6,9.2 Swing组件和容器,容器与组件包含继承关系图表:,第九章 编写图形界面应用程序,7,9.2 Swing组件和容器,9.2.2 Swing Components 分类:顶层容器:中间容器:原子组件见显示样本,第九章 编写图形界面应用程序,8,9.2 Swing组件和容器,9.2.3 顶层容器java 提供了三个顶层容器:JFrame,JDialog,和 JApplet。,第九章 编写图形
4、界面应用程序,9,9.2 Swing组件和容器,9.2.3 顶层容器特点:显示在屏幕上的每个组件都必须在一个包含继承中。每一个包含继承都有一个顶层容器作为它的根。每一个一个顶层容器都有一个content pane,它包含了顶层容器中的所有组件菜单在顶层容器中,但在content pane 之外。,第九章 编写图形界面应用程序,10,9.2 Swing组件和容器,9.2.3 顶层容器包含继承图如下:,第九章 编写图形界面应用程序,11,9.2 Swing组件和容器,9.2.3 顶层容器在Content Pane 中添加组件 有两种方式:topLevelContainer.getContentPa
5、ne().add(yellowLabel,BorderLayout.CENTER);或:JPanel contentPane=new JPanel();topLevelContainer.setContentPane(contentPane);,第九章 编写图形界面应用程序,12,9.2 Swing组件和容器,9.2.3 顶层容器2.添加菜单条 JMenuBar cyanMenuBar=new JMenuBar();frame.setJMenuBar(cyanMenuBar);,第九章 编写图形界面应用程序,13,9.2 Swing组件和容器,9.2.4 Frame是一个带有标题和可变大小边框
6、的窗口。任何一个图形界面应用程序都必须至少有一个JFrame.见例题:FrameDemo.java TopLevelDemo.java.,第九章 编写图形界面应用程序,14,9.2 Swing组件和容器,9.2.7 panel为其它组件提供一个容器。便于组织和显示组件。还可以包含子panel见例题:MyPanel.java,第九章 编写图形界面应用程序,15,9.2 Swing组件和容器,9.2.8 创建菜单:步骤:1.创建一个 MenuBar 对象,将其加入一个菜单容器,例如:Frame.2.创建 一个或多个Menu 对象,将其加入menu bar 对象.3.创建 一个或多个MenuItem
7、 objects,对象,将其加入相应的menu object.,第九章 编写图形界面应用程序,16,9.2 Swing组件和容器,9.2.8 创建菜单:JFrame f=new JFrame(Menu);JMenuBar mb=new JMenuBar();JMenu m1=new JMenu(File);JMenu m2=new JMenu(Edit);JMenu m3=new JMenu(Help);mb.add(m1);mb.add(m2);f.setMenuBar(mb);JMenuItem mi1=new JMenuItem(New);JMenuItem mi2=new JMenuI
8、tem(Save);JMenuItem mi3=new JMenuItem(Load);JMenuItem mi4=new JMenuItem(Quit);m1.add(mi1);m1.add(mi2);m1.add(mi3);m1.addSeparator();m1.add(mi4);,第九章 编写图形界面应用程序,17,9.2 Swing组件和容器,9.2.9 JComponent Class 除了顶层容器,Swing中所有组件(中间容器和原子组件)都从Jcomponent类中继承。例如:JPanel,JScrollPane,JButton,JTable(以J开头的组件类)。Compone
9、ntContainerJComponent,第九章 编写图形界面应用程序,18,9.2 Swing组件和容器,9.2.9 JComponent Class JComponent Class API:它从Component and Container 继承了许多方法,同时也提供了一些新的方法。它为它的继承者提供了如下常用功能:Customizing Component Appearance Setting Component State Handling Events Painting Components Dealing with the Containment Hierarchy Layin
10、g Out Components Getting Size and Position Information Specifying Absolute Size and Position 具体方法见表,第九章 编写图形界面应用程序,19,9.3 布局管理器,FlowLayout BorderLayout BoxLayout GridLayout GridBagLayout,第九章 编写图形界面应用程序,20,9.3 布局管理器,使用布局管理器的一般原则:当创建 JPanel 或 添加 components 到一个 content pane 时,需要考虑使用布局管理器。其他的中间容器不需要考虑布局
11、管理器。,第九章 编写图形界面应用程序,21,9.3 布局管理器,BorderLayout是顶层容器(JFrame,JDialog,和 JApplet)的默认布局管理器。有五个位置组件:,第九章 编写图形界面应用程序,22,9.3 布局管理器,BorderLayout见例题:BorderWindow.java,第九章 编写图形界面应用程序,23,9.3 布局管理器,BorderLayout当调整窗口大小时:,第九章 编写图形界面应用程序,24,9.3 布局管理器,.FlowLayoutpanel 类的默认布局管理器。组件按照加入的先后循序从左向右排列,一行排满后之后就下转到下一行继续从左至右排
12、列 每一行中的组件都居中排列 使用components的preferred sizes,第九章 编写图形界面应用程序,25,9.3 布局管理器,.FlowLayout当调整窗口大小时:,第九章 编写图形界面应用程序,26,9.3 布局管理器,.FlowLayout见例题:FlowWindow.java,第九章 编写图形界面应用程序,27,9.3 布局管理器,.GridLayout基本布局策略是把容器的空间划分成若干行乘若干列的网格区域,组件就位于这些划分出来的小区域中。所有的区域大小一样。组件按从左到右,从上到下的方法加入。用构造函数划分出网格的行数和列数,new GridLayout(行数,
13、列数)。,第九章 编写图形界面应用程序,28,9.3 布局管理器,.GridLayout当调整窗口大小时:,第九章 编写图形界面应用程序,29,9.3 布局管理器,.GridLayout见例题:GridWindow.java.,第九章 编写图形界面应用程序,30,9.3 布局管理器,4.BoxLayout 组件按从左到右或从上到下的方法加入考虑components的对齐and minimum,preferred,and maximum sizes 见例题:BoxWindow.java,第九章 编写图形界面应用程序,31,9.3 布局管理器,4.BoxLayout X alignments 和m
14、aximum size X alignment of 0.0(Component.LEFT_ALIGNMENT)X alignment of 0.5(Component.CENTER_ALIGNMENT).X alignment of 1.0(Component.RIGHT_ALIGNMENT),第九章 编写图形界面应用程序,32,9.3 布局管理器,4.BoxLayout 使用隐形Components作为填充。,第九章 编写图形界面应用程序,33,9.3 布局管理器,4.BoxLayout 使用隐形Components作为填充。Rigid area Box.createRigidArea(s
15、ize)container.add(firstComponent);container.add(Box.createRigidArea(new Dimension(5,0);container.add(secondComponent);,第九章 编写图形界面应用程序,34,9.3 布局管理器,4.BoxLayout 使用隐形Components作为填充。Glue:horizontal Box.createHorizontalGlue()vertical Box.createVerticalGlue()container.add(firstComponent);container.add(Box
16、.createHorizontalGlue();container.add(secondComponent);,第九章 编写图形界面应用程序,35,9.3 布局管理器,4.BoxLayout 组件对齐一般来说:所有top-to-bottom BoxLayout object 应该有相同的 X alignment。所有left-to-right Boxlayout应该有相同的 Y alignmentsetAlignmentX和setAlignmentY 可以实现对齐。大部分组件是center X and Y alignment,而buttons,combo boxes,labels,and me
17、nu items 是LEFT_ALIGNMENT,第九章 编写图形界面应用程序,36,9.3 布局管理器,4.BoxLayout 例子:button1.setAlignmentY(Component.BOTTOM_ALIGNMENT);button2.setAlignmentY(Component.BOTTOM_ALIGNMENT);,第九章 编写图形界面应用程序,37,9.3 布局管理器,4.BoxLayout 例子:label.setAlignmentX(Component.CENTER_ALIGNMENT);,第九章 编写图形界面应用程序,38,9.3 布局管理器,GridBagLayo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编写 图形界面 应用程序

链接地址:https://www.31ppt.com/p-6333725.html