已閱讀5頁(yè),還剩5頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
中文 1972 字 畢業(yè)設(shè)計(jì)(論文)文獻(xiàn)翻譯 題 目 學(xué)生姓名 學(xué) 號(hào) 專業(yè)名稱 計(jì)算機(jī)科學(xué)與技術(shù) 年 級(jí) 2009 級(jí) 指導(dǎo)教師 職 稱 講 師 所 在 系(院) 計(jì)算機(jī)科學(xué)與技術(shù) 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類的替代產(chǎn)品 , 它創(chuàng)建一個(gè)水平菜單欄組件 , 其中可連接零個(gè)以上菜單。 JMenuBar 類將 DefaultSingleSelectionModel用做自身的數(shù)據(jù)模型,因?yàn)橛脩粼谀骋粫r(shí)刻只能喚起( raise)或激活( activate)一個(gè)菜單。而且,只要鼠標(biāo)指針離開(kāi)菜單,該類就把菜單從屏幕上去除(或按照Swing說(shuō)法,將其“取消”) ,所有菜單又恢復(fù)為可以被喚起的狀態(tài)。圖 14-4顯示了 JMenuBar組件的類層次結(jié)構(gòu)( class hierarchy)。 Figure 14-4 JMenuBar 類圖 程序員可以用 JMenuBar 類的 add()方法添加 JMenu 對(duì)象。 JMenuBar 便隨之分配一個(gè)整數(shù)索引,里面的菜單按照其加入索引的順序排列,而菜單欄則依照此索引內(nèi)容的順序從左至右顯示菜單。從理論上講,幫助菜單是惟一的例外。程序員應(yīng)該讓某一個(gè)菜單被標(biāo)記為幫助菜單,但幫助菜單的位置由外觀風(fēng)格決定。在實(shí)踐中,如果程序員越俎代庖, JMenuBar 就會(huì)拋出一個(gè) Error 對(duì)象。 菜單欄布局 有兩種方法可以將菜單欄連接到 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()也讓外觀風(fēng)格來(lái)確定菜單的位置(一般情況下,它會(huì)將菜單欄錨定在框架頂部,并對(duì)框架的固有 Insets做適當(dāng)調(diào)整)。 JApplet和 JDialog 都包含 setJMenuBar()方法,這也就是說(shuō), applet 和對(duì)話框中都可以添加菜單欄。不管怎樣,程序員在使用 Swing 菜單時(shí)千萬(wàn)不要混淆 setJMenuBar()方法和老版的 AWT setMenuBar()方法,否則編譯器一定吃 不消。 如果應(yīng)用程序在 Macintosh上運(yùn)行,用戶可以設(shè)置其外觀風(fēng)格,按自己的意愿把菜單欄放到屏幕頂部。系統(tǒng)屬性 com.apple.macos.useScreenMenuBar為 true時(shí),該狀態(tài)即被激活。但是,在默認(rèn)情況下此狀態(tài)是被禁止的,因?yàn)榇蠖鄶?shù) Java 程序并不期待出現(xiàn)這種情況,而且必須對(duì)程序適當(dāng)編碼才可處理該狀態(tài)。尤其要注意的是, Aqua 人機(jī)界面規(guī)范( Aqua Human Interface Guidelines)要求菜單欄始終可見(jiàn)。如果應(yīng)用程序的某個(gè)框架沒(méi)有菜單欄,那么,當(dāng)該窗口獲得輸入焦點(diǎn) 時(shí),菜單欄會(huì)立刻消失,讓用戶不知所措。解決這一問(wèn)題最普遍的方法是編寫(xiě)一個(gè)菜單工廠( menufactory),為應(yīng)用程序用到的每個(gè)框架生成一個(gè)同樣的菜單欄。盡管這樣做增大了程序員的工作量,但為了給 Mac 用戶呈現(xiàn)一個(gè)親切、舒適的界面,多做點(diǎn)工作還是值得的。 連接菜單欄的第二種方法是調(diào)用 JComponent的子類 JMenuBar 類,這種方法相比之下用得不多。這意味著可以和其他 Swing 組件一樣,通過(guò) Swing 的布局管理器實(shí)現(xiàn)菜單欄定位。舉例來(lái)說(shuō),我們可以用下列代碼替換對(duì) setJMenuBar()的調(diào)用: menuBar.setBorder(new BevelBorder(BevelBorder.RAISED); frame.getContentPane().add(menuBar, BorderLayout.SOUTH); Figure 14-5 按 Swing 組件的方式定位 JMenuBar 這樣一來(lái),菜單欄被置于框架底部,如圖 14-5 所示(請(qǐng)注意,設(shè)置環(huán)繞菜 8 單欄的斜面邊框是為了突出菜單欄的位置)。甚至還可以在不同的位置添加多個(gè)菜單欄。 Swing 不要求將單個(gè)菜單欄錨定在框架頂部。由于都是 JComponent的子類,所以多重( multiple)菜單欄可以位于容器內(nèi)的任何位置。當(dāng)然,如果沒(méi)有強(qiáng)制性的理由,程序員是不會(huì)照此辦理的。但這樣就影響了外觀風(fēng)格在適當(dāng)?shù)臅r(shí)機(jī)給菜單欄妥善定位。移動(dòng)菜單欄這類基礎(chǔ)組件,幾乎總是會(huì)讓用戶手忙腳亂,不知該如何操作。如果程序中還包含多重菜單欄,大概就更熱鬧了。 屬性 JMenuBar 類的屬性如表 14-3 所示。其中, menu 屬性是一個(gè)索引屬性( indexed property),引用了每一個(gè)連接到菜單欄的 JMenu 對(duì)象。只讀屬性menuCount 則存放被連接菜單的數(shù)目 。請(qǐng)記住,單個(gè)選擇模型只允許每次激活一個(gè)菜單。如果當(dāng)前有菜單被激活, sele
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年張家港市第一人民醫(yī)院自主招聘編外合同制衛(wèi)技人員備考題庫(kù)完整答案詳解
- 2026年慶陽(yáng)職業(yè)技術(shù)學(xué)院公開(kāi)引進(jìn)高層次急需緊缺人才12人備考題庫(kù)及一套完整答案詳解
- 2026年華南師范大學(xué)環(huán)境學(xué)院非事業(yè)編制人員招聘?jìng)淇碱}庫(kù)參考答案詳解
- 2026年廈門市前埔南區(qū)小學(xué)非在編人員招聘?jìng)淇碱}庫(kù)及完整答案詳解一套
- 2026年分子細(xì)胞卓越中心孟飛龍組招聘實(shí)驗(yàn)室技術(shù)員備考題庫(kù)附答案詳解
- 2026年中北大學(xué)招聘?jìng)淇碱}庫(kù)及1套參考答案詳解
- 2026年中船黃岡貴金屬有限公司招聘?jìng)淇碱}庫(kù)及答案詳解參考
- 2026年中國(guó)安能集團(tuán)第二工程局有限公司招聘?jìng)淇碱}庫(kù)及參考答案詳解一套
- 2026年平潭綜合實(shí)驗(yàn)區(qū)公開(kāi)招聘高端人才備考題庫(kù)及完整答案詳解一套
- 2026年中外運(yùn)物流投資控股有限公司招聘?jìng)淇碱}庫(kù)及一套完整答案詳解
- 2025年國(guó)資委主任年終述職報(bào)告
- 大學(xué)教學(xué)督導(dǎo)與課堂質(zhì)量監(jiān)控工作心得體會(huì)(3篇)
- 2025年下半年國(guó)家教師資格幼兒園《綜合素質(zhì)》考試真題及參考答案
- 項(xiàng)目專家評(píng)審意見(jiàn)書(shū)標(biāo)準(zhǔn)模板
- 評(píng)審委托協(xié)議書(shū)
- 黑龍江中醫(yī)藥大學(xué)《無(wú)機(jī)化學(xué)》2025 學(xué)年第二學(xué)期期末試卷
- 2025年高中計(jì)算機(jī)操作試題題庫(kù)及答案
- 研學(xué)基地課程書(shū)籍或課件
- 日本所有番號(hào)分類
- 2024年江蘇省普通高中學(xué)業(yè)水平測(cè)試小高考生物、地理、歷史、政治試卷及答案(綜合版)
- GA/T 850-2009城市道路路內(nèi)停車泊位設(shè)置規(guī)范
評(píng)論
0/150
提交評(píng)論