计算机外文翻译--菜单和工具栏_第1页
计算机外文翻译--菜单和工具栏_第2页
计算机外文翻译--菜单和工具栏_第3页
计算机外文翻译--菜单和工具栏_第4页
计算机外文翻译--菜单和工具栏_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

中文 1972 字 毕业设计(论文)文献翻译 题 目 学生姓名 学 号 专业名称 计算机科学与技术 年 级 2009 级 指导教师 职 称 讲 师 所 在 系(院) 计算机科学与技术 2013 年 3 月 1 日 1 Menus and Toolbars Excerpted from Java Swing(2Nd Edition)(OReilly)by Marc Loy,Robert Eckstein etc. The JMenuBar Class SwingsJMenuBar class supersedes the AWTMenuBar class. This class creates a horizontal 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 cancels 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 diagram You can add JMenu objects to the menu bar with theadd( ) method of theJMenuBar class. JMenuBar then assigns an integer index based 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 the L&F. In practice, trying to do this results in JMenuBar throwing an Error. Menu Bar Placement You can attach menu bars to Swing frames or applets in one of two ways. First, 2 you can use thesetJMenuBar( ) method of JFrame, JDialog, JApplet, or JInternalFrame: JFrame frame = new JFrame(Menu); JMenuBar 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 to 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 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 activates 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, whenever 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 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 3 to setJMenuBar( ) with the following code: menuBar.setBorder(new BevelBorder(BevelBorder.RAISED); frame.getContentPane( ).add(menuBar, BorderLayout.SOUTH); Figure 14-5. JMenuBar positioned as a Swing component This places the menu bar at the bottom of the frame, as shown in Figure 14-5. (Note that we set a beveled border 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 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; having multiple menu bars would be baffling. Properties The 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 selection 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, except the contents are cast to 4 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 JMenuItem Class Event Handling There 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 menu 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 is 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 go 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 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 the 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 5 automatically updates all user interface elements 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 an Action directly, adding theAction to a JMenu or JPopupMenu had the same effect: the menu would create and configure an appropriateJMenuItem for you. 6 菜单和工具栏 摘自 Java Swing ( 第二版 ) Marc Loy,Robert Eckstein等著 JMenuBar 类 Swing 的 JMenuBar类是 AWT的 MenuBar类的替代产品 , 它创建一个水平菜单栏组件 , 其中可连接零个以上菜单。 JMenuBar 类将 DefaultSingleSelectionModel用做自身的数据模型,因为用户在某一时刻只能唤起( raise)或激活( activate)一个菜单。而且,只要鼠标指针离开菜单,该类就把菜单从屏幕上去除(或按照Swing说法,将其“取消”) ,所有菜单又恢复为可以被唤起的状态。图 14-4显示了 JMenuBar组件的类层次结构( class hierarchy)。 Figure 14-4 JMenuBar 类图 程序员可以用 JMenuBar 类的 add()方法添加 JMenu 对象。 JMenuBar 便随之分配一个整数索引,里面的菜单按照其加入索引的顺序排列,而菜单栏则依照此索引内容的顺序从左至右显示菜单。从理论上讲,帮助菜单是惟一的例外。程序员应该让某一个菜单被标记为帮助菜单,但帮助菜单的位置由外观风格决定。在实践中,如果程序员越俎代庖, 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); 7 这段程序中的 setJMenuBar()方法类似于 java.awt.Frame的 setMenuBar()方法。与老版本一样, setJMenuBar()也让外观风格来确定菜单的位置(一般情况下,它会将菜单栏锚定在框架顶部,并对框架的固有 Insets做适当调整)。 JApplet和 JDialog 都包含 setJMenuBar()方法,这也就是说, applet 和对话框中都可以添加菜单栏。不管怎样,程序员在使用 Swing 菜单时千万不要混淆 setJMenuBar()方法和老版的 AWT setMenuBar()方法,否则编译器一定吃 不消。 如果应用程序在 Macintosh上运行,用户可以设置其外观风格,按自己的意愿把菜单栏放到屏幕顶部。系统属性 com.apple.macos.useScreenMenuBar为 true时,该状态即被激活。但是,在默认情况下此状态是被禁止的,因为大多数 Java 程序并不期待出现这种情况,而且必须对程序适当编码才可处理该状态。尤其要注意的是, Aqua 人机界面规范( Aqua Human Interface Guidelines)要求菜单栏始终可见。如果应用程序的某个框架没有菜单栏,那么,当该窗口获得输入焦点 时,菜单栏会立刻消失,让用户不知所措。解决这一问题最普遍的方法是编写一个菜单工厂( menufactory),为应用程序用到的每个框架生成一个同样的菜单栏。尽管这样做增大了程序员的工作量,但为了给 Mac 用户呈现一个亲切、舒适的界面,多做点工作还是值得的。 连接菜单栏的第二种方法是调用 JComponent的子类 JMenuBar 类,这种方法相比之下用得不多。这意味着可以和其他 Swing 组件一样,通过 Swing 的布局管理器实现菜单栏定位。举例来说,我们可以用下列代码替换对 setJMenuBar()的调用: menuBar.setBorder(new BevelBorder(BevelBorder.RAISED); frame.getContentPane().add(menuBar, BorderLayout.SOUTH); Figure 14-5 按 Swing 组件的方式定位 JMenuBar 这样一来,菜单栏被置于框架底部,如图 14-5 所示(请注意,设置环绕菜 8 单栏的斜面边框是为了突出菜单栏的位置)。甚至还可以在不同的位置添加多个菜单栏。 Swing 不要求将单个菜单栏锚定在框架顶部。由于都是 JComponent的子类,所以多重( multiple)菜单栏可以位于容器内的任何位置。当然,如果没有强制性的理由,程序员是不会照此办理的。但这样就影响了外观风格在适当的时机给菜单栏妥善定位。移动菜单栏这类基础组件,几乎总是会让用户手忙脚乱,不知该如何操作。如果程序中还包含多重菜单栏,大概就更热闹了。 属性 JMenuBar 类的属性如表 14-3 所示。其中, menu 属性是一个索引属性( indexed property),引用了每一个连接到菜单栏的 JMenu 对象。只读属性menuCount 则存放被连接菜单的数目 。请记住,单个选择模型只允许每次激活一个菜单。如果当前有菜单被激活, sele

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论