版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、<p><b> 中文1972字</b></p><p> 畢業(yè)設計(論文)文獻翻譯</p><p> 題 目 </p><p> 學生姓名 學 號 </p><p>
2、; 專業(yè)名稱 計算機科學與技術 年 級 2009級 </p><p> 指導教師 職 稱 講 師 </p><p> 所 在 系(院) 計算機科學與技術 </p><p> 2013 年 3 月 1 日</p>&l
3、t;p> Menus and Toolbars</p><p> Excerpted from "Java Swing(2Nd Edition)(O'Reilly)"by Marc Loy,Robert Eckstein etc.</p><p> The JMenuBar Class</p><p> Swing'
4、sJMenuBar 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
5、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
6、be raised Figure 14-4 shows the class hierarchy for th</p><p> Figure 14-4. JMenuBar class diagram</p><p> You can add JMenu objects to the menu bar with theadd( ) method of theJMenuBar class.
7、 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
8、: 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 E</p><p>
9、 Menu Bar Placement</p><p> You 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:</p>&l
10、t;p> JFrame frame = new JFrame("Menu");</p><p> JMenuBar menuBar = new JMenuBar( );</p><p> // Attach the menu bar to the frame.</p><p> frame.setJMenuBar(menuBar);
11、</p><p> 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 anch
12、ors the menu bar to the top of a frame, adjusting the frame's internal Insets accordingly). Both JApplet and JDialog contain a setJMenuBar( ) method— this means that you can add menu bars to both applets and dialogs.
13、 Either way, be sure not to confuse the setJMenuBar( ) method with the older setMenuBar( ) </p><p> If your application is running on a Macintosh, the Mac L&F can be configured to place menu bars at the
14、 top of the screen, where Mac users expect to find them. Setting the system property com.apple.macos.useScreenMenuBar to true activates thisbehavior. It's disabled by default because most Java programs do not expect
15、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 </p><p> The second way
16、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 se
17、tJMenuBar( ) with the following code:</p><p> menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));</p><p> frame.getContentPane( ).add(menuBar, BorderLayout.SOUTH);</p><p> Fi
18、gure 14-5. JMenuBar positioned as a Swing component</p><p> 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 outli
19、ne 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 me
20、nu bars can be positioned anywhere inside a container.</p><p> Of course, you'd never actually want to do this without a very compelling reason. It robs the L&F of its opportunity to place the menu
21、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.</p><p&g
22、t; Properties</p><p> 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
23、 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 r
24、eturnsfalse. ThecomponentAtIndex property accesses the menu associated with the given index. It i</p><p> The JMenuItem Class</p><p> Event Handling</p><p> There are a number of
25、 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 name
26、d components) and attach all of the menu items to the same ActionListener. Then, in theactionPerformed( ) method of the listener, use the event's getActionCommand( ) method to obtain the action command of the menu it
27、em generating the event.</p><p> 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 andPopupMe
28、nuExample.java, which is discussed later.</p><p> Alternatively, you can register a separate ActionListener class with each menu item, which takes the guesswork out of determining the menu item selected. Ho
29、wever, 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
30、 the code for each program action together with the action's name, icon, keystrokes, and other attributes in one place. You </p><p> This technique is particularly powerful if you want to be able to inv
31、oke 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 they'll both have appropriate labels and appearanc
32、es. If the application needs to disable the action because it's not currently appropriate, calling setEnabled on the Action instance automatically updates all user interface elements associated with the action (thus&
33、lt;/p><p> Although prior to SDK 1.3 it wasn't 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
34、an appropriateJMenuItem for you.</p><p><b> 菜單和工具欄</b></p><p> 摘自《Java Swing》(第二版)Marc Loy,Robert Eckstein等著</p><p> JMenuBar 類</p><p> Swing 的JMenuBar類
35、是AWT的MenuBar類的替代產(chǎn)品,它創(chuàng)建一個水平菜單欄組件,其中可連接零個以上菜單。JMenuBar 類將DefaultSingleSelectionModel用做自身的數(shù)據(jù)模型,因為用戶在某一時刻只能喚起(raise)或激活(activate)一個菜單。而且,只要鼠標指針離開菜單,該類就把菜單從屏幕上去除(或按照Swing說法,將其“取消”),所有菜單又恢復為可以被喚起的狀態(tài)。圖14-4顯示了JMenuBar組件的類層次結(jié)構(gòu)(cl
36、ass hierarchy)。</p><p> Figure 14-4 JMenuBar 類圖</p><p> 程序員可以用JMenuBar 類的add()方法添加JMenu 對象。JMenuBar 便隨之分配一個整數(shù)索引,里面的菜單按照其加入索引的順序排列,而菜單欄則依照此索引內(nèi)容的順序從左至右顯示菜單。從理論上講,幫助菜單是惟一的例外。程序員應該讓某一個菜單被標記為幫助菜單,但
37、幫助菜單的位置由外觀風格決定。在實踐中,如果程序員越俎代庖,JMenuBar 就會拋出一個Error 對象。</p><p><b> 菜單欄布局</b></p><p> 有兩種方法可以將菜單欄連接到Swing 框架或applet 上。第一,使用JFrame、JDialog、JApplet 或JInternalFrame 的setJMenuBar()方法:<
38、;/p><p> JFrame frame = new JFrame("Menu");</p><p> JMenuBar menuBar = new JMenuBar();</p><p> // the menu bar to the frame.</p><p> frame.setJMenuBar(menuBa
39、r);</p><p> 這段程序中的setJMenuBar()方法類似于java.awt.Frame的setMenuBar()方法。與老版本一樣,setJMenuBar()也讓外觀風格來確定菜單的位置(一般情況下,它會將菜單欄錨定在框架頂部,并對框架的固有Insets做適當調(diào)整)。JApplet和JDialog 都包含setJMenuBar()方法,這也就是說,applet 和對話框中都可以添加菜單欄。不管怎
40、樣,程序員在使用Swing 菜單時千萬不要混淆setJMenuBar()方法和老版的AWT setMenuBar()方法,否則編譯器一定吃不消。</p><p> 如果應用程序在Macintosh上運行,用戶可以設置其外觀風格,按自己的意愿把菜單欄放到屏幕頂部。系統(tǒng)屬性com.apple.macos.useScreenMenuBar為true時,該狀態(tài)即被激活。但是,在默認情況下此狀態(tài)是被禁止的,因為大多數(shù)Ja
41、va 程序并不期待出現(xiàn)這種情況,而且必須對程序適當編碼才可處理該狀態(tài)。尤其要注意的是,Aqua 人機界面規(guī)范(Aqua Human Interface Guidelines)要求菜單欄始終可見。如果應用程序的某個框架沒有菜單欄,那么,當該窗口獲得輸入焦點時,菜單欄會立刻消失,讓用戶不知所措。解決這一問題最普遍的方法是編寫一個菜單工廠(menufactory),為應用程序用到的每個框架生成一個同樣的菜單欄。盡管這樣做增大了程序員的工作量,
42、但為了給Mac 用戶呈現(xiàn)一個親切、舒適的界面,多做點工作還是值得的。</p><p> 連接菜單欄的第二種方法是調(diào)用JComponent的子類JMenuBar 類,這種方法相比之下用得不多。這意味著可以和其他Swing 組件一樣,通過Swing 的布局管理器實現(xiàn)菜單欄定位。舉例來說,我們可以用下列代碼替換對setJMenuBar()的調(diào)用:</p><p> menuBar.setBo
43、rder(new BevelBorder(BevelBorder.RAISED));</p><p> frame.getContentPane().add(menuBar, BorderLayout.SOUTH);</p><p> Figure 14-5 按Swing 組件的方式定位JMenuBar</p><p> 這樣一來,菜單欄被置于框架底部,如圖1
44、4-5 所示(請注意,設置環(huán)繞菜單欄的斜面邊框是為了突出菜單欄的位置)。甚至還可以在不同的位置添加多個菜單欄。Swing 不要求將單個菜單欄錨定在框架頂部。由于都是JComponent的子類,所以多重(multiple)菜單欄可以位于容器內(nèi)的任何位置。當然,如果沒有強制性的理由,程序員是不會照此辦理的。但這樣就影響了外觀風格在適當?shù)臅r機給菜單欄妥善定位。移動菜單欄這類基礎組件,幾乎總是會讓用戶手忙腳亂,不知該如何操作。如果程序中還包含多
45、重菜單欄,大概就更熱鬧了。</p><p><b> 屬性</b></p><p> JMenuBar 類的屬性如表14-3 所示。其中,menu 屬性是一個索引屬性(indexed property),引用了每一個連接到菜單欄的JMenu 對象。只讀屬性menuCount 則存放被連接菜單的數(shù)目。請記住,單個選擇模型只允許每次激活一個菜單。如果當前有菜單被激活,
46、selected屬性返回true;否則返回false。componentAtIndex屬性可訪問與給定索引有關的菜單,它與被索引的menu屬性相似,只是它的內(nèi)容被拋給一個Component 對象。如果沒有組件與索引相關,getComponentAtIndex()存取方法將返回null值。component屬性返回一個對this的引用(即菜單欄本身),subElements 屬性返回由菜單欄中各個菜單組成的數(shù)組。</p>&
47、lt;p> JMenuItem 類</p><p><b> 事件處理</b></p><p> 處理菜單項事件的方法有很多種。鑒于菜單項從AbstractButton 處繼承了ActionEvent 功能,因而一個可行的方法便是為每個菜單項賦予一個操作指令(這通常由指定組件自動完成),并將所有菜單項連接到同一個ActionListener方法上。接下來,
48、在監(jiān)聽器的actionPerformed()方法中調(diào)用事件的getActionCommand()方法獲取產(chǎn)生事件的菜單項的操作指令。這一操作將告知監(jiān)聽器哪個菜單項已經(jīng)被單擊,并允許其進行適當回應。本章之前介紹的IntroExample.java 和稍后將介紹的PopupMenuExample.java 都使用上述方法。</p><p> 另一種方法是,程序員給每個菜單項注冊一個獨立的ActionListener
49、 類,來判斷哪個菜單項被選中。但是,Swing 能做的還不止這些。最面向?qū)ο蟮姆椒ㄊ莿?chuàng)建一個專門的Action 類,讓它應對用戶可能向應用程序提交的所有任務,從而讓程序員把實現(xiàn)每個動作的程序代碼與動作的名稱、圖標、按鍵及其他屬性捆綁為一體。程序員隨后便能用此Action類創(chuàng)建菜單項,自動地設置菜單項的文字、圖像、快捷方式,等等。</p><p> 上述技巧在程序員希望用多種方法(如分別用工具欄和菜單入手)調(diào)用同
50、一個操作時尤其有效。程序員可以用同樣的Action 實例創(chuàng)建菜單項和工具欄按扭,兩者的標簽和外觀都會很合適。當應用程序需要中止某個當前不適宜的操作時,調(diào)用Action實例的setEnabled就會自動將與該操作有關的用戶接口元素進行全部更新(從而對菜單項和工具欄按扭不加區(qū)分)。同樣地,對操作的名稱或圖標等其他屬性的改變也會自動地更新所有相關的用戶接口組件。</p><p> 盡管SDK 1.3 之前的版本無法直
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
評論
0/150
提交評論