📜  Java AWT PopupMenu

📅  最后修改于: 2020-09-29 00:43:33             🧑  作者: Mango

Java AWT PopupMenu

PopupMenu可以在组件内的特定位置动态弹出。它继承了Menu类。

AWT PopupMenu类声明

public class PopupMenu extends Menu implements MenuContainer, Accessible

Java AWT PopupMenu示例

import java.awt.*;
import java.awt.event.*;
class PopupMenuExample
{
 PopupMenuExample(){
         final Frame f= new Frame("PopupMenu Example");
         final PopupMenu popupmenu = new PopupMenu("Edit"); 
         MenuItem cut = new MenuItem("Cut");
         cut.setActionCommand("Cut");
         MenuItem copy = new MenuItem("Copy");
         copy.setActionCommand("Copy");
         MenuItem paste = new MenuItem("Paste");
         paste.setActionCommand("Paste");    
         popupmenu.add(cut);
         popupmenu.add(copy);
         popupmenu.add(paste);      
         f.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {            
            popupmenu.show(f , e.getX(), e.getY());
            }               
         });
         f.add(popupmenu); 
         f.setSize(400,400);
         f.setLayout(null);
         f.setVisible(true);
 }
public static void main(String args[])
{
        new PopupMenuExample();
}
}

输出: