import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 三级菜单
 * 
 */
public class JPopupMenu3 {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Swing Right-Click Menu Example");
        JTextArea textArea = new JTextArea("Right-click to show menu");

        // 创建一级菜单
        JPopupMenu popupMenu = new JPopupMenu();
        JMenu editMenu = new JMenu("Edit");
        JMenuItem cutItem = new JMenuItem("Cut");
        JMenuItem copyItem = new JMenuItem("Copy");
        JMenuItem pasteItem = new JMenuItem("Paste");
        editMenu.add(cutItem);
        editMenu.add(copyItem);
        editMenu.add(pasteItem);
        popupMenu.add(editMenu);
        popupMenu.add(cutItem);

        // 创建二级菜单
        JMenu subMenu = new JMenu("Submenu");
        JMenuItem item1 = new JMenuItem("Item 1");
        JMenuItem item2 = new JMenuItem("Item 2");
        subMenu.add(item1);
        subMenu.add(item2);
        editMenu.add(subMenu);  // 将二级菜单添加到一级菜单

        // 添加右键点击事件
        textArea.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        JScrollPane scrollPane = new JScrollPane(textArea);
        frame.add(scrollPane);
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}