Java API 分類導覽 - javax.swing.JPopupMenu addPopupMenuListener()

JPopupMenu 類別 (class) 的 addPopupMenuListener() 方法 (method) 用來設定快顯功能表的相關事件處理。



修飾子與參數
public void addPopupMenuListener(PopupMenuListener l)


addPopupMenuListener() 沒有回傳值 (return value) ,需要一個 PopupMenuListener 型態 (type) 的物件 (object) 常數當參數 (paramenter) 。


舉例如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class addPopupMenuListenerDemo extends MouseAdapter implements PopupMenuListener {
    JFrame frame;
    JMenuItem item1, item2;
    JPopupMenu popupmenu;
    
    public static void main(String[] args) {
        new addPopupMenuListenerDemo();
    }
    
    public addPopupMenuListenerDemo() {
        frame = new JFrame("SwingDemo");
        frame.setSize(600, 400);
        frame.setLayout(new GridLayout(6, 4));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addMouseListener(this);
        
        popupmenu = new JPopupMenu();
        item1 = new JMenuItem("one");
        item2 = new JMenuItem("two");
        popupmenu.add(item1);
        popupmenu.add(item2);
        popupmenu.addPopupMenuListener(this);
        
        frame.add(popupmenu);
        frame.setVisible(true);
    }
    
    public void mousePressed(MouseEvent e) {
        popupmenu.show(e.getComponent(), e.getX(), e.getY());
    }
    
    public void popupMenuCanceled(PopupMenuEvent e)  {
        System.out.println("popupMenuCanceled");
    }
    
    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        System.out.println("popupMenuWillBecomeInvisible");
    }
    
    public void popupMenuWillBecomeVisible(PopupMenuEvent e)  {
        System.out.println("popupMenuWillBecomeVisible");
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:addPopupMenuListenerDemo.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2012 年 9 月 */


此例的 addPopupMenuListenerDemo 直接實作 PopupMenuListener 介面
public class addPopupMenuListenerDemo extends MouseAdapter implements PopupMenuListener {


然後建立快顯功能表物件 popupmenu ,最後呼叫 addPopupMenuListener() 並以 this 當參數
popupmenu = new JPopupMenu();
item1 = new JMenuItem("one");
item2 = new JMenuItem("two");
popupmenu.add(item1);
popupmenu.add(item2);
popupmenu.addPopupMenuListener(this);


由於 addPopupMenuListenerDemo 實作 PopupMenuListener 介面,因此要寫出 popupMenuCanceled() 、 popupMenuWillBecomeInvisible() 、 popupMenuWillBecomeVisible()
public void popupMenuCanceled(PopupMenuEvent e)  {
    System.out.println("popupMenuCanceled");
}
    
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
    System.out.println("popupMenuWillBecomeInvisible");
}
    
public void popupMenuWillBecomeVisible(PopupMenuEvent e)  {
    System.out.println("popupMenuWillBecomeVisible");
}


popupMenuCanceled() 為取消快顯功能表的動作, popupMenuWillBecomeInvisible() 為快顯功能表變得不可見, popupMenuWillBecomeVisible() 為快顯功能表變得可見。


編譯後執行,結果如下



中英文術語對照
類別class
方法method
回傳值return value
型態type
物件object
參數parameter


您可以繼續參考
Swing 元件
  • ButtonGroup
  • JComponent
    • AbstractButton
      • JButton
      • JToggleButton
        • JCheckBox
        • JRadioButton
      • JMenuItem
        • JCheckBoxMenuItem
        • JMenu
        • JRadioButtonMenuItem
    • JColorChooser
    • JComboBox
    • JFileChooser
    • JInternalFrame
    • JLabel
    • JLayeredPane
      • JDesktopPane
    • JList
    • JMenuBar
    • JOptionPane
    • JPanel
    • JPopupMenu
    • JProgressBar
    • JRootPane
    • JScrollBar
    • JScrollPane
    • JSeparator
    • JSlider
    • JSpinner
    • JSplitPane
    • JTabbedPane
    • JTable
    • text.JTextComponent
      • JEditorPane
        • JTextPane
      • JTextField
        • JFormattedTextField
        • JPasswordField
      • JTextArea
    • JToolBar
    • JToolTip
    • JTree
    • JViewport
  • JDialog
  • JFrame
  • JWindow


相關目錄
Java API 分類導覽
Java 教材
首頁


參考資料
http://docs.oracle.com/javase/6/docs/api/javax/swing/JPopupMenu.html

沒有留言: