资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
中文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 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 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 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 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 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 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 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 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.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 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
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号