2023年全國碩士研究生考試考研英語一試題真題(含答案詳解+作文范文)_第1頁
已閱讀1頁,還剩9頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、<p><b>  中文1972字</b></p><p>  畢業(yè)設(shè)計(jì)(論文)文獻(xiàn)翻譯</p><p>  題 目 </p><p>  學(xué)生姓名 學(xué) 號(hào) </p><p>

2、;  專業(yè)名稱 計(jì)算機(jī)科學(xué)與技術(shù) 年 級(jí) 2009級(jí) </p><p>  指導(dǎo)教師 職 稱 講 師 </p><p>  所 在 系(院) 計(jì)算機(jī)科學(xué)與技術(shù) </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)建一個(gè)水平菜單欄組件,其中可連接零個(gè)以上菜單。JMenuBar 類將DefaultSingleSelectionModel用做自身的數(shù)據(jù)模型,因?yàn)橛脩粼谀骋粫r(shí)刻只能喚起(raise)或激活(activate)一個(gè)菜單。而且,只要鼠標(biāo)指針離開菜單,該類就把菜單從屏幕上去除(或按照Swing說法,將其“取消”),所有菜單又恢復(fù)為可以被喚起的狀態(tài)。圖14-4顯示了JMenuBar組件的類層次結(jié)構(gòu)(cl

36、ass hierarchy)。</p><p>  Figure 14-4 JMenuBar 類圖</p><p>  程序員可以用JMenuBar 類的add()方法添加JMenu 對(duì)象。JMenuBar 便隨之分配一個(gè)整數(shù)索引,里面的菜單按照其加入索引的順序排列,而菜單欄則依照此索引內(nèi)容的順序從左至右顯示菜單。從理論上講,幫助菜單是惟一的例外。程序員應(yīng)該讓某一個(gè)菜單被標(biāo)記為幫助菜單,但

37、幫助菜單的位置由外觀風(fēng)格決定。在實(shí)踐中,如果程序員越俎代庖,JMenuBar 就會(huì)拋出一個(gè)Error 對(duì)象。</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()也讓外觀風(fēng)格來確定菜單的位置(一般情況下,它會(huì)將菜單欄錨定在框架頂部,并對(duì)框架的固有Insets做適當(dāng)調(diào)整)。JApplet和JDialog 都包含setJMenuBar()方法,這也就是說,applet 和對(duì)話框中都可以添加菜單欄。不管怎

40、樣,程序員在使用Swing 菜單時(shí)千萬不要混淆setJMenuBar()方法和老版的AWT setMenuBar()方法,否則編譯器一定吃不消。</p><p>  如果應(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ù)Ja

41、va 程序并不期待出現(xiàn)這種情況,而且必須對(duì)程序適當(dāng)編碼才可處理該狀態(tài)。尤其要注意的是,Aqua 人機(jī)界面規(guī)范(Aqua Human Interface Guidelines)要求菜單欄始終可見。如果應(yīng)用程序的某個(gè)框架沒有菜單欄,那么,當(dāng)該窗口獲得輸入焦點(diǎn)時(shí),菜單欄會(huì)立刻消失,讓用戶不知所措。解決這一問題最普遍的方法是編寫一個(gè)菜單工廠(menufactory),為應(yīng)用程序用到的每個(gè)框架生成一個(gè)同樣的菜單欄。盡管這樣做增大了程序員的工作量,

42、但為了給Mac 用戶呈現(xiàn)一個(gè)親切、舒適的界面,多做點(diǎn)工作還是值得的。</p><p>  連接菜單欄的第二種方法是調(diào)用JComponent的子類JMenuBar 類,這種方法相比之下用得不多。這意味著可以和其他Swing 組件一樣,通過Swing 的布局管理器實(shí)現(xiàn)菜單欄定位。舉例來說,我們可以用下列代碼替換對(duì)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 所示(請(qǐng)注意,設(shè)置環(huán)繞菜單欄的斜面邊框是為了突出菜單欄的位置)。甚至還可以在不同的位置添加多個(gè)菜單欄。Swing 不要求將單個(gè)菜單欄錨定在框架頂部。由于都是JComponent的子類,所以多重(multiple)菜單欄可以位于容器內(nèi)的任何位置。當(dāng)然,如果沒有強(qiáng)制性的理由,程序員是不會(huì)照此辦理的。但這樣就影響了外觀風(fēng)格在適當(dāng)?shù)臅r(shí)機(jī)給菜單欄妥善定位。移動(dòng)菜單欄這類基礎(chǔ)組件,幾乎總是會(huì)讓用戶手忙腳亂,不知該如何操作。如果程序中還包含多

45、重菜單欄,大概就更熱鬧了。</p><p><b>  屬性</b></p><p>  JMenuBar 類的屬性如表14-3 所示。其中,menu 屬性是一個(gè)索引屬性(indexed property),引用了每一個(gè)連接到菜單欄的JMenu 對(duì)象。只讀屬性menuCount 則存放被連接菜單的數(shù)目。請(qǐng)記住,單個(gè)選擇模型只允許每次激活一個(gè)菜單。如果當(dāng)前有菜單被激活,

46、selected屬性返回true;否則返回false。componentAtIndex屬性可訪問與給定索引有關(guān)的菜單,它與被索引的menu屬性相似,只是它的內(nèi)容被拋給一個(gè)Component 對(duì)象。如果沒有組件與索引相關(guān),getComponentAtIndex()存取方法將返回null值。component屬性返回一個(gè)對(duì)this的引用(即菜單欄本身),subElements 屬性返回由菜單欄中各個(gè)菜單組成的數(shù)組。</p>&

47、lt;p>  JMenuItem 類</p><p><b>  事件處理</b></p><p>  處理菜單項(xiàng)事件的方法有很多種。鑒于菜單項(xiàng)從AbstractButton 處繼承了ActionEvent 功能,因而一個(gè)可行的方法便是為每個(gè)菜單項(xiàng)賦予一個(gè)操作指令(這通常由指定組件自動(dòng)完成),并將所有菜單項(xiàng)連接到同一個(gè)ActionListener方法上。接下來,

48、在監(jiān)聽器的actionPerformed()方法中調(diào)用事件的getActionCommand()方法獲取產(chǎn)生事件的菜單項(xiàng)的操作指令。這一操作將告知監(jiān)聽器哪個(gè)菜單項(xiàng)已經(jīng)被單擊,并允許其進(jìn)行適當(dāng)回應(yīng)。本章之前介紹的IntroExample.java 和稍后將介紹的PopupMenuExample.java 都使用上述方法。</p><p>  另一種方法是,程序員給每個(gè)菜單項(xiàng)注冊(cè)一個(gè)獨(dú)立的ActionListener

49、 類,來判斷哪個(gè)菜單項(xiàng)被選中。但是,Swing 能做的還不止這些。最面向?qū)ο蟮姆椒ㄊ莿?chuàng)建一個(gè)專門的Action 類,讓它應(yīng)對(duì)用戶可能向應(yīng)用程序提交的所有任務(wù),從而讓程序員把實(shí)現(xiàn)每個(gè)動(dòng)作的程序代碼與動(dòng)作的名稱、圖標(biāo)、按鍵及其他屬性捆綁為一體。程序員隨后便能用此Action類創(chuàng)建菜單項(xiàng),自動(dòng)地設(shè)置菜單項(xiàng)的文字、圖像、快捷方式,等等。</p><p>  上述技巧在程序員希望用多種方法(如分別用工具欄和菜單入手)調(diào)用同

50、一個(gè)操作時(shí)尤其有效。程序員可以用同樣的Action 實(shí)例創(chuàng)建菜單項(xiàng)和工具欄按扭,兩者的標(biāo)簽和外觀都會(huì)很合適。當(dāng)應(yīng)用程序需要中止某個(gè)當(dāng)前不適宜的操作時(shí),調(diào)用Action實(shí)例的setEnabled就會(huì)自動(dòng)將與該操作有關(guān)的用戶接口元素進(jìn)行全部更新(從而對(duì)菜單項(xiàng)和工具欄按扭不加區(qū)分)。同樣地,對(duì)操作的名稱或圖標(biāo)等其他屬性的改變也會(huì)自動(dòng)地更新所有相關(guān)的用戶接口組件。</p><p>  盡管SDK 1.3 之前的版本無法直

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫僅提供信息存儲(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論