Java Learning: Notes on AWT Components and event handling (1)--menu bar, menus, menu items

Source: Internet
Author: User

The menu is placed in the menu bar and the menu item is placed
1.MenuBar
In the java.awt package, the creation of a menu bar, or an instance of MenuBar, is a menu bar.
The Setmenubar (MenuBar Bar) method in the frame class allows you to add a menu bar to the top of the window, but only adds a menu bar to the window
2.Menu
In the java.awt package, it is the menu that is responsible for creating a menu, that is, an instance of menu.
The methods of the Meun class are as follows:
Menu () to create an empty caption
Menu (String s) to create a menus titled S
public void Add (MenuItem Item) adds the menu item specified by the parameter item
Public MenuItem GetItem (int n) Gets the menu item at the specified index
public int GetItemCount () gets the number of menu items
3.MenuItem
In the java.awt package, it is the responsibility to create a menu item, which is an instance of menu.
The methods of the MenuItem class are as follows:
MenuItem () Create untitled menu item
MenuItem (String s) constructs a menu item titled S
public void Setenable (Boolean B) sets whether the current menu item can be selected
Public String Getlabel () Gets the name of the menu item
4. A few tips on menus
(1) Add menu item Divider Line
To add a separator line between menu items. Just use the AddSeparator () method in the menu class
As follows:
Menu1.add ("new");//menu1 is an instance of menu
Menu1.addseparator ();//Add a divider between new and saved
Menu1.add ("Save");
Menu1.add ("print");
(2) check box menu item
If you want a checkmark to appear when you select a menu item, you can use the Checkboxmenuitem class to create this menu item
Such as:
Item1=new Checkboxmenuitem ("new");
(3) Embed sub-menu
Menu not only can add the MenuItem object, but also can add the menus object, and the submenu that is added is called a submenu.

5. Windows and Screens
Toolkit Class (abstract class, which cannot be created directly from a constructor method, provided in the Java Runtime Environment, can be referenced by the Gettoolkit () method)
There are the following methods: Dimension getscreensize ();
The value of width in the returned dimension object is the width of the screen, and the value of heigt is the height of the screen

Practice task: Create a window with a menu, the width of the window and screen is the same, height is half the height of the screen
The following code:

Importjava.awt.*;classFirstwindowextendsframe{MenuBar MenuBar;    Menu menu;    MenuItem item1,item2;        Firstwindow (String s) {settitle (s); Toolkit Tool=Gettoolkit (); Dimension Dim=tool.getscreensize (); SetBounds (0,0,dim.width,dim.height/2); MenuBar=NewMenuBar (); Menu=NewMenu ("File"); Item1=NewMenuItem ("Open"); ITEM2=NewMenuItem ("Save");        Menu.add (ITEM1);        Menu.add (ITEM2);        Menubar.add (menu);        Setmenubar (menubar); SetVisible (true); }    } Public classexample1{ Public Static voidMain (string[] args) {NewFirstwindow ("This is a window with a menu"); }}

Java Learning: Notes on AWT Components and event handling (1)--menu bar, menus, menu items

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.