收藏 分销(赏)

计算机-菜单和工具栏.doc

上传人:w****g 文档编号:2765171 上传时间:2024-06-05 格式:DOC 页数:13 大小:123.50KB
下载 相关 举报
计算机-菜单和工具栏.doc_第1页
第1页 / 共13页
计算机-菜单和工具栏.doc_第2页
第2页 / 共13页
计算机-菜单和工具栏.doc_第3页
第3页 / 共13页
计算机-菜单和工具栏.doc_第4页
第4页 / 共13页
计算机-菜单和工具栏.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

1、中文1972字 毕业设计(论文)文献翻译 题 目 学生姓名 学 号 专业名称 计算机科学与技术 年 级 2009级 指导教师 职 称 讲 师 所 在 系(院) 计算机科学与技术 2013 年 3 月 1 日2Menus and ToolbarsExcerpted from Java Swing(2Nd Edition)(OReilly)by Marc Loy,Robert Eckstein etc.The JMenuBar ClassSwingsJMenuBar class supersedes the AWTMenuBar class. This class creates a horizo

2、ntal menu bar component with zero or more menus attached to it. JMenuBar uses theDefaultSingleSelectionModel as its data model because the user can raise, or activate , only one of its menus at a given time. Once the mouse pointer leaves that menu, the class removes the menu from the screen (or canc

3、els it, in Swing lingo), and all menus again become eligible to be raised Figure 14-4 shows the class hierarchy for the JMenuBar component.Figure 14-4. JMenuBar class diagramYou can add JMenu objects to the menu bar with theadd( ) method of theJMenuBar class. JMenuBar then assigns an integer index b

4、ased on the order in which the menus were added. The menu bar displays the menus from left to right on the bar according to their assigned index. In theory, there is one exception: the help menu. You are supposed to be allowed to mark one menu as the help menu; the location of the help menu is up to

5、 the L&F. In practice, trying to do this results in JMenuBar throwing an Error.Menu Bar PlacementYou can attach menu bars to Swing frames or applets in one of two ways. First, you can use thesetJMenuBar( ) method of JFrame, JDialog, JApplet, or JInternalFrame:JFrame frame = new JFrame(Menu);JMenuBar

6、 menuBar = new JMenuBar( );/ Attach the menu bar to the frame.frame.setJMenuBar(menuBar);The setJMenuBar( ) method is analogous to thesetMenuBar( ) method ofjava.awt.Frame. Like its predecessor, setJMenuBar( ) allows the L&F to determine the location of the menu (typically, it anchors the menu bar t

7、o the top of a frame, adjusting the frames internal Insets accordingly). Both JApplet and JDialog contain a setJMenuBar( ) method this means that you can add menu bars to both applets and dialogs. Either way, be sure not to confuse the setJMenuBar( ) method with the older setMenuBar( ) method of AWT

8、 when working with Swing menus, or the compiler complains bitterly.If your application is running on a Macintosh, the Mac L&F can be configured to place menu bars at the top of the screen, where Mac users expect to find them. Setting the system property com.apple.macos.useScreenMenuBar to true activ

9、ates thisbehavior. Its disabled by default because most Java programs do not expect this behavior, and they must be coded properly to deal with it. Notably, the Aqua Human Interface Guidelines require that the menu bar is always visible. If your application has any frames that lack menu bars, whenev

10、er one of these gains focus, it causes the menu bar to disappear, much to the users consternation. The most common way of dealing with this is to write a menu factory that generates an identical menu bar for each frame your application uses. Although this is a little extra work, the familiarity and

11、comfort it brings your Mac users is probably worth it.The second way to add a menu bar is much less common. Recall that the JMenuBar class extendsJComponent. This means it can be positioned by a Swing layout manager like other Swing components. For example, we could replace the call to setJMenuBar(

12、) with the following code:menuBar.setBorder(new BevelBorder(BevelBorder.RAISED);frame.getContentPane( ).add(menuBar, BorderLayout.SOUTH);Figure 14-5. JMenuBar positioned as a Swing componentThis places the menu bar at the bottom of the frame, as shown in Figure 14-5. (Note that we set a beveled bord

13、er around the menu bar to help outline its location.) It would even be possible to add two or three menu bars in different locations. Swing does not require a single menu bar to be anchored to the top of a frame. Because they extend JComponent, multiple menu bars can be positioned anywhere inside a

14、container.Of course, youd never actually want to do this without a very compelling reason. It robs the L&F of its opportunity to place the menu bar in the appropriate location. Moving something as fundamental as a menu bar is almost certain to cause confusion and usability challenges for your users;

15、 having multiple menu bars would be baffling.PropertiesThe properties of the JMenuBar class are shown inTable 14-3. menu is an indexed property that references eachJMenu attached to the menu bar. The read-only menuCount property maintains a count of these attached menus. Remember that the single sel

16、ection model allows only one menu to be activated at a time. If any menu is currently activated, the selected property returns true; otherwise, the property returnsfalse. ThecomponentAtIndex property accesses the menu associated with the given index. It is similar to the indexed menu property, excep

17、t the contents are cast to aComponent. If there is no component associated with that index, the getComponentAtIndex( ) accessor returns null. Thecomponent property returns a reference to this (i.e., the menu bar itself);subElements returns an array consisting of the menus on the menu bar.The JMenuIt

18、em ClassEvent HandlingThere are a number of ways to process events from menu items. Because menu items inherit ActionEvent functionality from AbstractButton, one approach is to assign an action command to each menu item (this is often done automatically with named components) and attach all of the m

19、enu items to the same ActionListener. Then, in theactionPerformed( ) method of the listener, use the events getActionCommand( ) method to obtain the action command of the menu item generating the event.This tells the listener which menu item has been clicked, allowing it to react accordingly. This i

20、s the approach used in IntroExample.java earlier in this chapter andPopupMenuExample.java, which is discussed later.Alternatively, you can register a separate ActionListener class with each menu item, which takes the guesswork out of determining the menu item selected. However, Swing allows you to g

21、o a step further. The most object-oriented approach is to create a specialized Action class that corresponds to each of the tasks a user might request of your application. This lets you bundle the code for each program action together with the actions name, icon, keystrokes, and other attributes in

22、one place. You can then use this Action to create the menu item, which automatically sets the items text, image, accelerator, and so on.This technique is particularly powerful if you want to be able to invoke the same action in multiple ways (such as from a toolbar as well as a menu). You can use th

23、e same Action instance to create the menu item and toolbar button, and theyll both have appropriate labels and appearances. If the application needs to disable the action because its not currently appropriate, calling setEnabled on the Action instance automatically updates all user interface element

24、s associated with the action (thus dimming both your menu item and toolbar button). Similarly, changing other attributes of the action, such as its name or icon, automatically updates any associated user-interface components.Although prior to SDK 1.3 it wasnt possible to construct a JMenuItem from a

25、n Action directly, adding theAction to a JMenu or JPopupMenu had the same effect: the menu would create and configure an appropriateJMenuItem for you.菜单和工具栏摘自Java Swing(第二版)Marc Loy,Robert Eckstein等著JMenuBar 类Swing 的JMenuBar类是AWT的MenuBar类的替代产品,它创建一个水平菜单栏组件,其中可连接零个以上菜单。JMenuBar 类将DefaultSingleSelecti

26、onModel用做自身的数据模型,因为用户在某一时刻只能唤起(raise)或激活(activate)一个菜单。而且,只要鼠标指针离开菜单,该类就把菜单从屏幕上去除(或按照Swing说法,将其“取消”),所有菜单又恢复为可以被唤起的状态。图14-4显示了JMenuBar组件的类层次结构(class hierarchy)。Figure 14-4 JMenuBar 类图程序员可以用JMenuBar 类的add()方法添加JMenu 对象。JMenuBar 便随之分配一个整数索引,里面的菜单按照其加入索引的顺序排列,而菜单栏则依照此索引内容的顺序从左至右显示菜单。从理论上讲,帮助菜单是惟一的例外。程序

27、员应该让某一个菜单被标记为帮助菜单,但帮助菜单的位置由外观风格决定。在实践中,如果程序员越俎代庖,JMenuBar 就会抛出一个Error 对象。菜单栏布局有两种方法可以将菜单栏连接到Swing 框架或applet 上。第一,使用JFrame、JDialog、JApplet 或JInternalFrame 的setJMenuBar()方法:JFrame frame = new JFrame(Menu);JMenuBar menuBar = new JMenuBar();/ the menu bar to the frame.frame.setJMenuBar(menuBar);这段程序中的se

28、tJMenuBar()方法类似于java.awt.Frame的setMenuBar()方法。与老版本一样,setJMenuBar()也让外观风格来确定菜单的位置(一般情况下,它会将菜单栏锚定在框架顶部,并对框架的固有Insets做适当调整)。JApplet和JDialog 都包含setJMenuBar()方法,这也就是说,applet 和对话框中都可以添加菜单栏。不管怎样,程序员在使用Swing 菜单时千万不要混淆setJMenuBar()方法和老版的AWT setMenuBar()方法,否则编译器一定吃不消。如果应用程序在Macintosh上运行,用户可以设置其外观风格,按自己的意愿把菜单栏

29、放到屏幕顶部。系统属性com.apple.macos.useScreenMenuBar为true时,该状态即被激活。但是,在默认情况下此状态是被禁止的,因为大多数Java 程序并不期待出现这种情况,而且必须对程序适当编码才可处理该状态。尤其要注意的是,Aqua 人机界面规范(Aqua Human Interface Guidelines)要求菜单栏始终可见。如果应用程序的某个框架没有菜单栏,那么,当该窗口获得输入焦点时,菜单栏会立刻消失,让用户不知所措。解决这一问题最普遍的方法是编写一个菜单工厂(menufactory),为应用程序用到的每个框架生成一个同样的菜单栏。尽管这样做增大了程序员的工

30、作量,但为了给Mac 用户呈现一个亲切、舒适的界面,多做点工作还是值得的。连接菜单栏的第二种方法是调用JComponent的子类JMenuBar 类,这种方法相比之下用得不多。这意味着可以和其他Swing 组件一样,通过Swing 的布局管理器实现菜单栏定位。举例来说,我们可以用下列代码替换对setJMenuBar()的调用:menuBar.setBorder(new BevelBorder(BevelBorder.RAISED);frame.getContentPane().add(menuBar, BorderLayout.SOUTH);Figure 14-5 按Swing 组件的方式定位

31、JMenuBar这样一来,菜单栏被置于框架底部,如图14-5 所示(请注意,设置环绕菜单栏的斜面边框是为了突出菜单栏的位置)。甚至还可以在不同的位置添加多个菜单栏。Swing 不要求将单个菜单栏锚定在框架顶部。由于都是JComponent的子类,所以多重(multiple)菜单栏可以位于容器内的任何位置。当然,如果没有强制性的理由,程序员是不会照此办理的。但这样就影响了外观风格在适当的时机给菜单栏妥善定位。移动菜单栏这类基础组件,几乎总是会让用户手忙脚乱,不知该如何操作。如果程序中还包含多重菜单栏,大概就更热闹了。属性JMenuBar 类的属性如表14-3 所示。其中,menu 属性是一个索引

32、属性(indexed property),引用了每一个连接到菜单栏的JMenu 对象。只读属性menuCount 则存放被连接菜单的数目。请记住,单个选择模型只允许每次激活一个菜单。如果当前有菜单被激活,selected属性返回true;否则返回false。componentAtIndex属性可访问与给定索引有关的菜单,它与被索引的menu属性相似,只是它的内容被抛给一个Component 对象。如果没有组件与索引相关,getComponentAtIndex()存取方法将返回null值。component属性返回一个对this的引用(即菜单栏本身),subElements 属性返回由菜单栏中各

33、个菜单组成的数组。JMenuItem 类事件处理处理菜单项事件的方法有很多种。鉴于菜单项从AbstractButton 处继承了ActionEvent 功能,因而一个可行的方法便是为每个菜单项赋予一个操作指令(这通常由指定组件自动完成),并将所有菜单项连接到同一个ActionListener方法上。接下来,在监听器的actionPerformed()方法中调用事件的getActionCommand()方法获取产生事件的菜单项的操作指令。这一操作将告知监听器哪个菜单项已经被单击,并允许其进行适当回应。本章之前介绍的IntroExample.java 和稍后将介绍的PopupMenuExample

34、.java 都使用上述方法。另一种方法是,程序员给每个菜单项注册一个独立的ActionListener 类,来判断哪个菜单项被选中。但是,Swing 能做的还不止这些。最面向对象的方法是创建一个专门的Action 类,让它应对用户可能向应用程序提交的所有任务,从而让程序员把实现每个动作的程序代码与动作的名称、图标、按键及其他属性捆绑为一体。程序员随后便能用此Action类创建菜单项,自动地设置菜单项的文字、图像、快捷方式,等等。上述技巧在程序员希望用多种方法(如分别用工具栏和菜单入手)调用同一个操作时尤其有效。程序员可以用同样的Action 实例创建菜单项和工具栏按扭,两者的标签和外观都会很合

35、适。当应用程序需要中止某个当前不适宜的操作时,调用Action实例的setEnabled就会自动将与该操作有关的用户接口元素进行全部更新(从而对菜单项和工具栏按扭不加区分)。同样地,对操作的名称或图标等其他属性的改变也会自动地更新所有相关的用户接口组件。尽管SDK 1.3 之前的版本无法直接从Action 构造JMenuItem,但是使用添加Action 到JMenu或JPopupMenu的方法可以达到同样的效果 菜单自会妥善创建并配置JMenuItem。目 录第一章 总论11.1项目名称与承办单位11.2研究工作的依据、内容及范围11.3编制原则31.4项目概况31.5技术经济指标51.6结

36、论6第二章 项目背景及建设必要性82.1项目背景82.2建设的必要性9第三章 建设条件113.1项目区概况113.2建设地点选择错误!未定义书签。3.3项目建设条件优劣势分析错误!未定义书签。第四章 市场分析与销售方案134.1市场分析134.2营销策略、方案、模式14第五章 建设方案155.1建设规模和产品方案155.2建设规划和布局155.3运输185.4建设标准185.5公用工程205.6工艺技术方案215.7设备方案215.8节能减排措施24第六章 环境影响评价256.1环境影响256.2环境保护与治理措施266.3评价与审批28第七章 项目组织与管理297.1组织机构与职能划分297

37、.2劳动定员297.3经营管理措施307.4技术培训30第八章 劳动、安全、卫生与消防318.1编制依据及采用的标准318.2安全卫生防护原则318.3自然灾害危害因素分析及防范措施328.4生产过程中产生的危害因素分析及防范措施328.5消防编制依据及采用的标准348.6消防设计原则358.7火灾隐患分析358.8总平面消防设计358.9消防给水设计368.10建筑防火368.11火灾检测报警系统378.12预期效果37第九章 项目实施进度389.1实施进度计划389.2项目实施建议38第十章 项目招投标方案4010.1招标原则4010.2项目招标范围4010.3投标、开标、评标和中标程序4010.4评标委员会的人员组成和资格要求42第十一章 投资估算和资金筹措4311.1投资估算4311.2资金筹措及使用计划45第十二章 财务评价4712.1费用与效益估算4712.2财务分析4812.3不确定性分析4912.5财务评价结论50第十三章 建设合理性分析5113.1产业政策符合性分析5113.2清洁生产符合性分析5113.3规划符合性分析5113.4项目建设环保政策符合性分析5113.5环境承载性分析5113.6结论52第十四章 结论与建议5312

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 通信科技 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服