Menus are the most commonly used components in GUIs. Menus are not subclasses of the Component class and cannot be placed in ordinary containers. They are not constrained by the layout manager and can only be placed in the menu bar.
The menu component consists of three parts: menu bar (MenuBar), menu (Menu) and menu item (MenuItem). A menu bar consists of several menus, and a menu consists of several menu items. Generally, the menu bar is placed in the Frame window, just call the setMenuBar() method of the Frame class.
Commonly used menus include: drop-down menus and pop-up menus (displayed independently and can appear anywhere).
1: Steps to create a drop-down menu:
1. Create a menu bar.
2. Call the setMenuBar() method of Frame to add the menu bar to the Frame.
3. Create several Menu objects respectively and add them to MenuBar.
4. For each Menu object, create several MenuItem objects and add them to the Menu.
Example:
Copy the code code as follows:
import java.awt.*;
public class MenuDemo {
Frame f;
MenuBarmb;
Menu menuFile;
MenuItem mItemNew, mItemOpen, mItemSave;
MenuDemo() {
f = new Frame("My menu instance");
f.setBounds(300, 100, 400, 300);
mb = new MenuBar(); // Create menu barMenuBar
menuFile = new Menu("File");
mItemNew = new MenuItem("New");
mItemOpen = new MenuItem("Open");
menuFile.add(mItemNew);
mItemSave = new MenuItem("Save");
menuFile.add(mItemOpen);
//Add dividing line
menuFile.addSeparator();
menuFile.add(mItemSave);
mb.add(menuFile);
// Add the "File" menu to the menu bar
// setMenuBar: Set the menu bar of this form to the specified menu bar.
f.setMenuBar(mb);
f.setVisible(true);
}
public static void main(String[] args) {
new MenuDemo();
}
}
There are pictures and there are truths: (The drop-down menu items cannot be screenshots)
2: Steps to create a pop-up menu:
1. First establish the most basic JFrame framework.
2. Create a right-click pop-up menu (JPopupMenu) and added menu item (JMenuItem). 3. Use the add method and insert method to add or insert into JPopupMenu. 4. Display the pop-up by calling the show method corresponding to the pop-up menu trigger. pop-up menu, check all MouseEvent events to see if they are pop-up menu triggers, and then display the pop-up menu when appropriate
Example:
Copy the code code as follows:
import java.awt.event.*;
import javax.swing.*;
public class JPopMenu_Demo extends JFrame {
// Implementation of pop-up menu. Pop-up menu is a small window that pops up and displays a series of options.
JPopupMenu popupMenu;
public JPopMenu_Demo() {
super("right-click pop-up menu"); // Call the parent class constructor
// Instantiate the pop-up menu
popupMenu = new JPopupMenu();
//Add menu items to the menu
popupMenu.add(new JMenuItem("menu item"));
popupMenu.add(new JButton("Button"));
popupMenu.add(new JLabel("Label"));
myEvents();
setSize(350, 300); //Set the window size
setLocation(400, 200);
setVisible(true); // Set the window to be visible
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Exit the program when closing the window
}
private void myEvents() {
//Window mouse event handling
addMouseListener(new MouseAdapter() {
//Click the mouse
public void mousePressed(MouseEvent event) {
//Call the triggerEvent method to handle the event
triggerEvent(event);
}
// release mouse
public void mouseReleased(MouseEvent event) {
triggerEvent(event);
}
private void triggerEvent(MouseEvent event) { // handle events
// isPopupTrigger(): Returns whether this mouse event triggers an event for the platform's pop-up menu.
if (event.isPopupTrigger())
// show menu
popupMenu.show(event.getComponent(), event.getX(),
event.getY());
}
});
}
public static void main(String args[]) {
new JPopMenu_Demo();
}
}
There are pictures and the truth (but it’s pretty ugly)
Three: Tab form:
1.Basic instructions:
2. Commonly used methods:
We add multiple JPanel objects to JTabbedPanel. and then again
Add JTabbedPanel to the form, the code is as follows:
Copy the code code as follows:
JTabbedPane jp=new JTabbedPane(JTabbedPane.LEFT); //Set the tab coordinates
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel(); //Create multiple containers
jp.add("Form1", p1);
jp.add("Form 2", p2);
jp.add("Form 3", p3);//Add a subcontainer and add a name to the tab
this.add(jp,BorderLayout.CENTER); //Add the tab form to the main form
3. Code examples and screenshots:
Copy the code code as follows:
import java.awt.*;
import javax.swing.*;
/**
* <p>Title: Tab demonstration</p>
* <p>Description: Here is a tab demonstration. Click on different cards to display 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("#First Card#");
tabbedPane.addTab("One", icon, panel1, "The first card prompt information!");
tabbedPane.setSelectedIndex(0);
Component panel2 = makeTextPanel("##Second Card##");
tabbedPane.addTab("Two", icon, panel2, "The second card prompt information!");
Component panel3 = makeTextPanel("###The third card###");
tabbedPane.addTab("Three", icon, panel3, "The third card prompt information!");
Component panel4 = makeTextPanel("####The fourth card####");
tabbedPane.addTab("Four", icon, panel4, "The fourth card prompt information!");
//Add tab to panl
add(tabbedPane);
}
/**
* <br>
* Method description: Add information to the tab<br>
* Input parameter: String text Displayed information content<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 the picture<br>
* Input parameters: String path image path<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) {
// Use Swing form description
// JFrame.setDefaultLookAndFeelDecorated(true);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
//Create form
JFrame frame = new JFrame("TabbedPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JTabbedPaneDemo1(), BorderLayout.CENTER);
// display form
frame.setSize(400, 200);
frame.setVisible(true);
}
}