計(jì)算機(jī)外文翻譯--菜單和工具欄_第1頁
計(jì)算機(jī)外文翻譯--菜單和工具欄_第2頁
計(jì)算機(jī)外文翻譯--菜單和工具欄_第3頁
計(jì)算機(jī)外文翻譯--菜單和工具欄_第4頁
計(jì)算機(jī)外文翻譯--菜單和工具欄_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

中文 1972 字 畢業(yè)設(shè)計(jì)(論文)文獻(xiàn)翻譯 題 目 學(xué)生姓名 學(xué) 號 專業(yè)名稱 計(jì)算機(jī)科學(xué)與技術(shù) 年 級 2009 級 指導(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)建一個水平菜單欄組件 , 其中可連接零個以上菜單。 JMenuBar 類將 DefaultSingleSelectionModel用做自身的數(shù)據(jù)模型,因?yàn)橛脩粼谀骋粫r刻只能喚起( raise)或激活( activate)一個菜單。而且,只要鼠標(biāo)指針離開菜單,該類就把菜單從屏幕上去除(或按照Swing說法,將其“取消”) ,所有菜單又恢復(fù)為可以被喚起的狀態(tài)。圖 14-4顯示了 JMenuBar組件的類層次結(jié)構(gòu)( class hierarchy)。 Figure 14-4 JMenuBar 類圖 程序員可以用 JMenuBar 類的 add()方法添加 JMenu 對象。 JMenuBar 便隨之分配一個整數(shù)索引,里面的菜單按照其加入索引的順序排列,而菜單欄則依照此索引內(nèi)容的順序從左至右顯示菜單。從理論上講,幫助菜單是惟一的例外。程序員應(yīng)該讓某一個菜單被標(biāo)記為幫助菜單,但幫助菜單的位置由外觀風(fēng)格決定。在實(shí)踐中,如果程序員越俎代庖, 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()也讓外觀風(fēng)格來確定菜單的位置(一般情況下,它會將菜單欄錨定在框架頂部,并對框架的固有 Insets做適當(dāng)調(diào)整)。 JApplet和 JDialog 都包含 setJMenuBar()方法,這也就是說, applet 和對話框中都可以添加菜單欄。不管怎樣,程序員在使用 Swing 菜單時千萬不要混淆 setJMenuBar()方法和老版的 AWT setMenuBar()方法,否則編譯器一定吃 不消。 如果應(yīng)用程序在 Macintosh上運(yùn)行,用戶可以設(shè)置其外觀風(fēng)格,按自己的意愿把菜單欄放到屏幕頂部。系統(tǒng)屬性 com.apple.macos.useScreenMenuBar為 true時,該狀態(tài)即被激活。但是,在默認(rèn)情況下此狀態(tài)是被禁止的,因?yàn)榇蠖鄶?shù) Java 程序并不期待出現(xiàn)這種情況,而且必須對程序適當(dāng)編碼才可處理該狀態(tài)。尤其要注意的是, Aqua 人機(jī)界面規(guī)范( Aqua Human Interface Guidelines)要求菜單欄始終可見。如果應(yīng)用程序的某個框架沒有菜單欄,那么,當(dāng)該窗口獲得輸入焦點(diǎn) 時,菜單欄會立刻消失,讓用戶不知所措。解決這一問題最普遍的方法是編寫一個菜單工廠( menufactory),為應(yīng)用程序用到的每個框架生成一個同樣的菜單欄。盡管這樣做增大了程序員的工作量,但為了給 Mac 用戶呈現(xiàn)一個親切、舒適的界面,多做點(diǎn)工作還是值得的。 連接菜單欄的第二種方法是調(diào)用 JComponent的子類 JMenuBar 類,這種方法相比之下用得不多。這意味著可以和其他 Swing 組件一樣,通過 Swing 的布局管理器實(shí)現(xiàn)菜單欄定位。舉例來說,我們可以用下列代碼替換對 setJMenuBar()的調(diào)用: menuBar.setBorder(new BevelBorder(BevelBorder.RAISED); frame.getContentPane().add(menuBar, BorderLayout.SOUTH); Figure 14-5 按 Swing 組件的方式定位 JMenuBar 這樣一來,菜單欄被置于框架底部,如圖 14-5 所示(請注意,設(shè)置環(huán)繞菜 8 單欄的斜面邊框是為了突出菜單欄的位置)。甚至還可以在不同的位置添加多個菜單欄。 Swing 不要求將單個菜單欄錨定在框架頂部。由于都是 JComponent的子類,所以多重( multiple)菜單欄可以位于容器內(nèi)的任何位置。當(dāng)然,如果沒有強(qiáng)制性的理由,程序員是不會照此辦理的。但這樣就影響了外觀風(fēng)格在適當(dāng)?shù)臅r機(jī)給菜單欄妥善定位。移動菜單欄這類基礎(chǔ)組件,幾乎總是會讓用戶手忙腳亂,不知該如何操作。如果程序中還包含多重菜單欄,大概就更熱鬧了。 屬性 JMenuBar 類的屬性如表 14-3 所示。其中, menu 屬性是一個索引屬性( indexed property),引用了每一個連接到菜單欄的 JMenu 對象。只讀屬性menuCount 則存放被連接菜單的數(shù)目 。請記住,單個選擇模型只允許每次激活一個菜單。如果當(dāng)前有菜單被激活, sele

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論