目录

PopupMenu

介绍 (Introduction)

弹出菜单表示一个菜单,可以在组件内的指定位置动态弹出。

类声明

以下是java.awt.PopupMenu类的声明:

public class CheckboxMenuItem
   extends MenuItem
      implements ItemSelectable, Accessible

类构造函数

SN 构造函数和描述
1

PopupMenu()

创建一个空名称的新弹出菜单。

2

PopupMenu(String label)

创建具有指定名称的新弹出菜单。

类方法

SN 方法和描述
1

void addNotify()

创建弹出菜单的同伴。

2

AccessibleContext getAccessibleContext()

获取与此PopupMenu关联的AccessibleContext。

3

MenuContainer getParent()

返回此菜单组件的父容器。

4

void show(Component origin, int x, int y)

显示相对于原点组件的x,y位置的弹出菜单。

方法继承

该类继承以下类中的方法:

  • java.awt.MenuItem

  • java.awt.MenuComponent

  • java.lang.Object

PopupMenu示例

使用您选择的任何编辑器创建以下java程序,例如D:/ 》 AWT 》 com 》 iowiki 》 gui 》

AWTMenuDemo.java
package com.iowiki.gui;
import java.awt.*;
import java.awt.event.*;
public class AWTMenuDemo {
   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;
   public AWTMenuDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      AWTMenuDemo  awtMenuDemo = new AWTMenuDemo();     
      awtMenuDemo.showPopupMenuDemo();
   }
   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);
      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());
      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showPopupMenuDemo(){
      final PopupMenu editMenu = new PopupMenu("Edit"); 
      MenuItem cutMenuItem = new MenuItem("Cut");
      cutMenuItem.setActionCommand("Cut");
      MenuItem copyMenuItem = new MenuItem("Copy");
      copyMenuItem.setActionCommand("Copy");
      MenuItem pasteMenuItem = new MenuItem("Paste");
      pasteMenuItem.setActionCommand("Paste");
      MenuItemListener menuItemListener = new MenuItemListener();
      cutMenuItem.addActionListener(menuItemListener);
      copyMenuItem.addActionListener(menuItemListener);
      pasteMenuItem.addActionListener(menuItemListener);
      editMenu.add(cutMenuItem);
      editMenu.add(copyMenuItem);
      editMenu.add(pasteMenuItem);   
      controlPanel.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {            
               editMenu.show(controlPanel, e.getX(), e.getY());
         }               
      });
      controlPanel.add(editMenu); 
      mainFrame.setVisible(true);
   }
   class MenuItemListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {            
         statusLabel.setText(e.getActionCommand() 
            + " MenuItem clicked.");
      }    
   }
}

使用命令提示符编译程序。 转到D:/ 》 AWT并键入以下命令。

D:\AWT>javac com\iowiki\gui\AWTMenuDemo.java

如果没有错误,那意味着编译成功。 使用以下命令运行程序。

D:\AWT>java com.iowiki.gui.AWTMenuDemo

验证以下输出。 (单击屏幕中间的。)

AWT PopupMenu
↑回到顶部↑
WIKI教程 @2018