The menu is the most commonly used component in the GUI, the menu is not a subclass of the component class, cannot be placed in a normal container, is not constrained by the layout manager, and can only be placed in the menu bar.
The menu component consists of a menu bar (MenuBar), a menu, and a menu item (MenuItem). A menu bar consists of several menus, and a menu consists of several menu items. The General menu bar is placed in the frame window, as long as the Setmenubar () method of the frame class is invoked.
Common menus are: pull-down menus and pop-up menus (displayed independently, can appear anywhere).
One: Drop-down menu creation steps:
1. Create a menu bar.
2. The Setmenubar () method that invokes the frame adds the menu bar to the frame.
3. Create several menu objects separately and add them to the MenuBar.
4. For each menu object, create several MenuItem objects, and add them to the menu.
Instance:
Copy Code code as follows:
Import java.awt.*;
public class Menudemo {
Frame F;
MenuBar MB;
Menu Menufile;
MenuItem mitemnew, Mitemopen, Mitemsave;
Menudemo () {
f = new Frame ("My Menu instance");
F.setbounds (300, 100, 400, 300);
MB = new MenuBar (); Create menu bar MenuBar
Menufile = new Menu ("file");
Mitemnew = new MenuItem ("new");
Mitemopen = new MenuItem ("open");
Menufile.add (mitemnew);
Mitemsave = new MenuItem ("Save");
Menufile.add (Mitemopen);
Add Split Line
Menufile.addseparator ();
Menufile.add (Mitemsave);
Mb.add (Menufile);
Add the File menu to the menu bar
Setmenubar: Sets the menu bar for this form to the specified menu bar.
F.setmenubar (MB);
F.setvisible (TRUE);
}
public static void Main (string[] args) {
New Menudemo ();
}
}
There is a picture of the truth: (dropdown menu items can not be screenshots)
Two: the pop-up menu creation step:
1. First, establish the most basic jframe framework.
2. Create a right-click pop-up menu (JPopupMenu) and Add a menu item (JMenuItem). 3. Add or insert to JPopupMenu using the Add method and the Insert method. 4. Display the pop-up menu by invoking the Show method corresponding to the pop-up menu trigger, checking all MouseEvent events to see if it is a pop-up menu trigger, and then displaying the pop-up menu when appropriate
Instance:
Copy Code code as follows:
Import java.awt.event.*;
Import javax.swing.*;
public class Jpopmenu_demo extends JFrame {
pop-up menu implementation, pop-up menu is a pop-up and display a series of options to the small window
JPopupMenu PopupMenu;
Public Jpopmenu_demo () {
Super ("Right key pop-up menu"); Calling the parent class constructor
Instantiating a pop-up menu
PopupMenu = new JPopupMenu ();
Add menu items to menu
Popupmenu.add (New JMenuItem ("menu item"));
Popupmenu.add (New JButton ("button"));
Popupmenu.add (New JLabel ("label"));
MyEvents ();
SetSize (350, 300); To set the window size
SetLocation (400, 200);
SetVisible (TRUE); Set window to Visual
Setdefaultcloseoperation (Jframe.exit_on_close); Exit Program when window closes
}
private void MyEvents () {
Mouse Event handling for Windows
Addmouselistener (New Mouseadapter () {
Click the mouse
public void mousepressed (MouseEvent event) {
Calling the Triggerevent method to handle events
Triggerevent (event);
}
Release mouse
public void mousereleased (MouseEvent event) {
Triggerevent (event);
}
private void Triggerevent (MouseEvent event) {//Handling events
Ispopuptrigger (): Returns whether this mouse event is a pop-up menu trigger event for the platform.
if (Event.ispopuptrigger ())
Show Menu
Popupmenu.show (Event.getcomponent (), Event.getx (),
Event.gety ());
}
});
}
public static void Main (String args[]) {
New Jpopmenu_demo ();
}
}
There is a picture of the truth (but very ugly)
Three: Tab form:
1. Basic Description:
2. Common methods:
We add multiple JPanel objects to the Jtabbedpanel. and then
Jtabbedpanel is added to the form with the following code:
Copy Code code as follows:
JTabbedPane jp=new JTabbedPane (jtabbedpane.left); Set tab in coordinates
JPanel p1=new JPanel ();
JPanel p2=new JPanel ();
JPanel p3=new JPanel ();
JPanel p4=new JPanel (); Create multiple containers
Jp.add ("Form 1", p1);
Jp.add ("Form 2", p2);
Jp.add ("Form 3", p3);//Add child containers and add a name to the tab
This.add (Jp,borderlayout.center); Add a tab form to the main form
3. Code samples and screenshots:
Copy Code code as follows:
Import java.awt.*;
Import javax.swing.*;
/**
* <p>title: Tab Demo </p>
* <p>description: Here is a tab demo, click on a different card to display a different content </p>
*/
public class JTabbedPaneDemo1 extends JPanel {
Public JTabbedPaneDemo1 () {
Super (New GridLayout (1, 1));
ImageIcon icon = Createimageicon ("Images/myicon.gif");
JTabbedPane TabbedPane = new JTabbedPane ();
Component Panel1 = Maketextpanel ("#第一个卡片 #");
Tabbedpane.addtab ("One", Icon, Panel1, "First card tips!") ");
Tabbedpane.setselectedindex (0);
Component Panel2 = Maketextpanel ("# #第二个卡片 # #");
Tabbedpane.addtab ("Two", Icon, Panel2, "Second card message!") ");
Component panel3 = Maketextpanel ("# # #第三个卡片 ###");
Tabbedpane.addtab ("Three", Icon, Panel3, "the third card prompts the message!" ");
Component Panel4 = Maketextpanel ("### #第四个卡片");
Tabbedpane.addtab ("Four", Icon, Panel4, "The fourth card message!" ");
Add tabs to Panl
Add (TabbedPane);
}
/**
* <br>
* Method Description: Add information to the tab <br>
* Input Parameters: Information content displayed in String text <br>
* Return type: Component member object
*/
Protected Component Maketextpanel (String text) {
JPanel panel = new JPanel (false);
JLabel filler = new JLabel (text);
Filler.sethorizontalalignment (Jlabel.center);
Panel.setlayout (New GridLayout (1, 1));
Panel.add (filler);
return panel;
}
/**
* <br>
* Method Description: Get pictures <br>
* Input parameter: path of String path picture <br>
* Return type: ImageIcon Picture Object
*/
protected static ImageIcon Createimageicon (String path) {
Java.net.URL Imgurl = TabbedPaneDemo.class.getResource (path);
if (path!= null) {
return new ImageIcon (path);
} else {
System.out.println ("couldn ' t Find file:" + path);
return null;
}
}
public static void Main (string[] args) {
Using the Swing form description
Jframe.setdefaultlookandfeeldecorated (TRUE);
try {
Uimanager.setlookandfeel (Uimanager.getsystemlookandfeelclassname ());
catch (Exception e) {
}
Create a form
JFrame frame = new JFrame ("Tabbedpanedemo");
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Frame.getcontentpane (). Add (New JTabbedPaneDemo1 (), borderlayout.center);
Show form
Frame.setsize (400, 200);
Frame.setvisible (TRUE);
}
}
Run Screenshots: