ImageVerifierCode 换一换
格式:DOCX , 页数:6 ,大小:42.31KB ,
资源ID:21668256      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/21668256.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(计算机外文翻译 菜单和工具栏大学论文Word文档下载推荐.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

计算机外文翻译 菜单和工具栏大学论文Word文档下载推荐.docx

1、Excerpted from Java Swing(2Nd Edition)(OReilly)by Marc Loy,Robert Eckstein etc.The JMenuBar ClassSwingsJMenuBar 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 m

2、odel 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 JM

3、enuBar 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 based on the order in which the menus were added. The menu bar displays the menus from left to right on the bar according t

4、o 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 PlacementYou can attach menu bars

5、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 menuBar = new JMenuBar( );/ Attach the menu bar to the frame.frame.setJMenuBar(menuBar);The setJMenuBar( ) method is anal

6、ogous 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( )

7、 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

8、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

9、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

10、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 th

11、e 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( ) with the following code:menuBar.setBorder(new BevelBorder(BevelBorder.RAISED);frame.getContentPane( ).add(menuBar, Borde

12、rLayout.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 border around the menu bar to help outline its location.) It would even be possible to add two or three menu bars in different

13、 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 opportun

14、ity 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.PropertiesThe properties of the JMenuBar class are shown inTable 14-3. menu i

15、s 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 ret

16、urns 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 aComponent. If there is no component associated with that index, the getComponentAtIndex( ) acc

17、essor 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 ClassEvent HandlingThere are a number of ways to process events from menu items. Because menu items inherit ActionEvent

18、 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 getActionComma

19、nd( ) 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.Altern

20、atively, 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 th

21、e 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, accelerat

22、or, 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

23、. If the application needs to disable the action because its not currently appropriate, calling setEnabled on the Action instance automatically updates all user interface elements associated with the action (thus dimming both your menu item and toolbar button). Similarly, changing other attributes o

24、f 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 a

25、ppropriateJMenuItem for you.菜单和工具栏摘自Java Swing(第二版)Marc Loy,Robert Eckstein等著JMenuBar 类Swing 的JMenuBar类是AWT的MenuBar类的替代产品,它创建一个水平菜单栏组件,其中可连接零个以上菜单。JMenuBar 类将DefaultSingleSelectionModel用做自身的数据模型,因为用户在某一时刻只能唤起(raise)或激活(activate)一个菜单。而且,只要鼠标指针离开菜单,该类就把菜单从屏幕上去除(或按照Swing说法,将其“取消”),所有菜单又恢复为可以被唤起的状态。图14-

26、4显示了JMenuBar组件的类层次结构(class hierarchy)。Figure 14-4 JMenuBar 类图程序员可以用JMenuBar 类的add()方法添加JMenu 对象。JMenuBar 便随之分配一个整数索引,里面的菜单按照其加入索引的顺序排列,而菜单栏则依照此索引内容的顺序从左至右显示菜单。从理论上讲,帮助菜单是惟一的例外。程序员应该让某一个菜单被标记为帮助菜单,但帮助菜单的位置由外观风格决定。在实践中,如果程序员越俎代庖,JMenuBar 就会抛出一个Error 对象。菜单栏布局有两种方法可以将菜单栏连接到Swing 框架或applet 上。第一,使用JFrame、

27、JDialog、JApplet 或JInternalFrame 的setJMenuBar()方法:JMenuBar menuBar = new JMenuBar();/ the menu bar to the frame.这段程序中的setJMenuBar()方法类似于java.awt.Frame的setMenuBar()方法。与老版本一样,setJMenuBar()也让外观风格来确定菜单的位置(一般情况下,它会将菜单栏锚定在框架顶部,并对框架的固有Insets做适当调整)。JApplet和JDialog 都包含setJMenuBar()方法,这也就是说,applet 和对话框中都可以添加菜单

28、栏。不管怎样,程序员在使用Swing 菜单时千万不要混淆setJMenuBar()方法和老版的AWT setMenuBar()方法,否则编译器一定吃不消。如果应用程序在Macintosh上运行,用户可以设置其外观风格,按自己的意愿把菜单栏放到屏幕顶部。系统属性com.apple.macos.useScreenMenuBar为true时,该状态即被激活。但是,在默认情况下此状态是被禁止的,因为大多数Java 程序并不期待出现这种情况,而且必须对程序适当编码才可处理该状态。尤其要注意的是,Aqua 人机界面规范(Aqua Human Interface Guidelines)要求菜单栏始终可见。如

29、果应用程序的某个框架没有菜单栏,那么,当该窗口获得输入焦点时,菜单栏会立刻消失,让用户不知所措。解决这一问题最普遍的方法是编写一个菜单工厂(menufactory),为应用程序用到的每个框架生成一个同样的菜单栏。尽管这样做增大了程序员的工作量,但为了给Mac 用户呈现一个亲切、舒适的界面,多做点工作还是值得的。连接菜单栏的第二种方法是调用JComponent的子类JMenuBar 类,这种方法相比之下用得不多。这意味着可以和其他Swing 组件一样,通过Swing 的布局管理器实现菜单栏定位。举例来说,我们可以用下列代码替换对setJMenuBar()的调用:frame.getContentP

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

31、操作。如果程序中还包含多重菜单栏,大概就更热闹了。属性JMenuBar 类的属性如表14-3 所示。其中,menu 属性是一个索引属性(indexed property),引用了每一个连接到菜单栏的JMenu 对象。只读属性menuCount 则存放被连接菜单的数目。请记住,单个选择模型只允许每次激活一个菜单。如果当前有菜单被激活,selected属性返回true;否则返回false。componentAtIndex属性可访问与给定索引有关的菜单,它与被索引的menu属性相似,只是它的内容被抛给一个Component 对象。如果没有组件与索引相关,getComponentAtIndex()存取方法将返回null值。component属性返回一个对this的引用(即菜单栏本身),subElements 属性返回由菜单栏中各个菜单组成的数组。JMenuItem 类事件处理处理菜单项事件的方法有很多种。鉴于菜单项从AbstractButton 处继承了ActionEvent 功能,因而一个可行的方法便是为每个菜单项赋予一个操作指令(这通常由指定组件自动完成),并将所有菜单项连接到同一个ActionListener方法上。接下来,在监听器的actionPerformed()方法中调用事件的getActionCommand()方法获取

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1