个性化阅读
专注于IT技术分析

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();
}
}

输出:

java awt popup示例1
java awt popup示例2

赞(0)
未经允许不得转载:srcmini » Java AWT PopupMenu

评论 抢沙发

评论前必须登录!