Swing JPopupMenu類

2019-10-16 22:10:16

彈出選單(JPopupMenu在)表示可以在元件內的指定位置動態彈出的選單。

類宣告

以下是javax.swing.JPopupMenu類的宣告 -

public class JPopupMenu
   extends JComponent
      implements Accessible, MenuElement

類建構函式

編號 建構函式 描述說明
1 JPopupMenu() 構造一個沒有引數的JPopupMenu
2 JPopupMenu(String label) 構造具有指定標題的JPopupMenu

類方法

以下是Swing JPopupMenu類中的方法列表。

方法繼承

該類繼承以下類中的方法 -

  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JPopupMenu範例

使用編輯器建立以下Java程式:JPopupMenuDemo.java

package com.yiibai.menu;


import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class JPopupMenuDemo{
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel; 

   public JPopupMenuDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
       JPopupMenuDemo  swingMenuDemo = new JPopupMenuDemo();     
      swingMenuDemo.showPopupMenuDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java SWING JPopupMenuDemo(tw511.com)");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      headerLabel = new JLabel("",JLabel.CENTER );
      statusLabel = new JLabel("",JLabel.CENTER);        
      statusLabel.setSize(350,100);

      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showPopupMenuDemo(){
      final JPopupMenu editMenu = new JPopupMenu("編輯"); 

      JMenuItem cutMenuItem = new JMenuItem("剪下");
      cutMenuItem.setActionCommand("Cut");

      JMenuItem copyMenuItem = new JMenuItem("複製");
      copyMenuItem.setActionCommand("Copy");

      JMenuItem pasteMenuItem = new JMenuItem("貼上");
      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);   

      mainFrame.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {            
            editMenu.show(mainFrame, e.getX(), e.getY());
         }               
      });
      mainFrame.add(editMenu); 
      mainFrame.setVisible(true);
   }
   class MenuItemListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {            
         statusLabel.setText(e.getActionCommand() + " MenuItem clicked.");
      }    
   }   
}

執行上面範例程式碼,得到以下結果:

JPopupMenu