tailieunhanh - giáo trình Java By Example phần 3

Tham khảo tài liệu 'giáo trình java by example phần 3', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | MenuBar menuBar new MenuBar As you can see the MenuBar constructor requires no arguments. After you ve created the MenuBar object you have to tell Java to associate the menu bar with the frame window. You do this by calling the window s setMenuBar method setMenuBar menuBar At this point you have an empty menu bar associated with the window. In the next steps you add menus to the menu bar. Adding Menus to a Menu Bar A menu bar is the horizontal area near the top of a window that contains the names of the menus contained in the menu bar. After creating and setting the MenuBar object you have the menu bar but it contains no menus. To add these menus you first create objects of the Menu class for each menu you want in the menu bar like this Menu fileMenu new Menu File Menu editMenu new Menu Edit Menu optionMenu new Menu Options The Menu class s constructor takes a single argument which is the string that ll appear as the menu s name on the menu bar. The example lines above create three menus for the menu bar. After creating the Menu objects you have to add them to the menu bar which you do by calling the MenuBar object s add method like this fileMenu editMenu optionMenu After Java executes the above three lines you have a menu bar with three menus as shown in Figure . Note however that at this point the menus contain no commands. If you were to click on the menu http names no pop-up menus would appear. Figure This window s menu bar contains three empty menus. Adding Menu Items to Menus You may have empty menus at this point but you re about to remedy that problem. To add items to your menus you first create objects of the Menuitem or CheckboxMenuitem classes for each menu item you need. To add items to the Options menus you made previously you might use Java code something like this Menuitem optionl new Menuitem Menuitem option2 new Menuitem Option 1 Option 2 Menuitem options new Menuitem Option 3 The .