Swing
Swing教學首頁
Swing介紹
Swing開發環境安裝
Swing控件
Swing Component類
Swing Container類
Swing JComponent類
Swing JLabel
Swing JButton
Swing JColorChooser
Swing JCheckBox
Swing JRadioButton
Swing JList
Swing JComboBox
Swing JTextField
Swing JTextArea
Swing ImageIcon
Swing JScrollBar
Swing JOptionPane
Swing JFileChooser
Swing JProgressBar
Swing JSlider
Swing JSpinner
Swing事件處理
SWING Event事件類
SWING AWTEvent事件類
SWING ActionEvent事件類
SWING InputEvent事件類
SWING KeyEvent事件類
SWING MouseEvent事件類
SWING WindowEvent事件類
SWING AdjustmentEvent事件處理
SWING ComponentEvent處理類
SWING ContainerEvent事件處理類
SWING MouseMotionEvent事件處理
SWING PaintEvent事件類
Swing事件監聽器
SWING ActionListener接口
SWING ComponentListener接口
Swing ItemListener接口
Swing KeyListener接口
Swing MouseListener Interface
Swing WindowListener接口
Swing AdjustmentListener接口
Swing ContainerListener接口
Swing MouseMotionListener接口
Swing FocusListener接口
Swing事件適配器
Swing FocusAdapter類
Swing KeyAdapter類
Swing MouseAdapter類
Swing WindowAdapter類
Swing MouseMotionAdapter類
Swing Layout佈局
Swing LayoutManager接口
Swing LayoutManager2接口
Swing BorderLayout佈局
Swing CardLayout佈局
Swing FlowLayout佈局類
Swing GridLayout佈局類
Swing GridBagLayout佈局類
Swing GroupLayout佈局類
Swing SpringLayout佈局類
Swing Menu菜單類
Swing JMenuBar類
Swing JMenuItem類
Swing JMenu類
Swing JCheckboxMenuItem類及例子
Swing JRadioButtonMenuItem類及例子
Swing JPopupMenu類及實例
Swing容器
Swing JPanel類及實例
Swing JFrame類和實例
Swing JWindow類及實例

Swing JPopupMenu類及實例

彈出菜單表示可以在一個組件內的指定位置動態彈出的菜單。

類聲明

以下是聲明 javax.swing.JPopupMenu類:

public class JPopupMenu extends JComponent implements Accessible, MenuElement

類構造函數

S.N.

構造函數 & 描述

1

JPopupMenu()
Constructs a JPopupMenu without an "invoker".

2

JPopupMenu(String label)
Constructs a JPopupMenu with the specified title.

類方法

S.N.

方法 & 描述

1

JMenuItem add(Action a)
Appends a new menu item to the end of the menu which dispatches the specified Action object.

2

JMenuItem add(JMenuItem menuItem)
Appends the specified menu item to the end of this menu.

3

JMenuItem add(String s)
Creates a new menu item with the specified text and appends it to the end of this menu.

4

void addMenuKeyListener(MenuKeyListener l)
Adds a MenuKeyListener to the popup menu.

5

void addPopupMenuListener(PopupMenuListener l)
Adds a PopupMenu listener.

6

void addSeparator()
Appends a new separator at the end of the menu.

7

protected PropertyChangeListener createActionChangeListener(JMenuItem b)
Returns a properly configured PropertyChangeListener which updates the control as changes to the Action occur.

8

protected JMenuItem createActionComponent(Action a)
Factory method which creates the JMenuItem for Actions added to the JPopupMenu.

9

protected void firePopupMenuCanceled()
Notifies PopupMenuListeners that this popup menu is cancelled.

10

protected void firePopupMenuWillBecomeInvisible()
Notifies PopupMenuListeners that this popup menu will become invisible.

11

protected void firePopupMenuWillBecomeVisible()
Notifies PopupMenuListeners that this popup menu will become visible.

12

AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JPopupMenu.

13

Component getComponent()
Returns this JPopupMenu component.

14

Component getComponentAtIndex(int i)
Deprecated. replaced by Container.getComponent(int)

15

int getComponentIndex(Component c)
Returns the index of the specified component.

16

static boolean getDefaultLightWeightPopupEnabled()
Gets the defaultLightWeightPopupEnabled property, which by default is true.

17

Component getInvoker()
Returns the component which is the 'invoker' of this popup menu.

18

String getLabel()
Returns the popup menu's label

19

Insets getMargin()
Returns the margin, in pixels, between the popup menu's border and its containees.

20

MenuKeyListener[] getMenuKeyListeners()
Returns an array of all the MenuKeyListeners added to this JPopupMenu with addMenuKeyListener().

21

PopupMenuListener[] getPopupMenuListeners()
Returns an array of all the PopupMenuListeners added to this JMenuItem with addPopupMenuListener().

22

SingleSelectionModel getSelectionModel()
Returns the model object that handles single selections.

23

MenuElement[] getSubElements()
Returns an array of MenuElements containing the submenu for this menu component.

24

PopupMenuUI getUI()
Returns the look and feel (L&F) object that renders this component.

25

String getUIClassID()
Returns the name of the L&F class that renders this component.

26

void insert(Action a, int index)
Inserts a menu item for the specified Action object at a given position.

27

void insert(Component component, int index)
Inserts the specified component into the menu at a given position.

28

boolean isBorderPainted()
Checks whether the border should be painted.

29

boolean isLightWeightPopupEnabled()
Gets the lightWeightPopupEnabled property.

30

boolean isPopupTrigger(MouseEvent e)
Returns true if the MouseEvent is considered a popup trigger by the JPopupMenu's currently installed UI.

31

boolean isVisible()
Returns true if the popup menu is visible (currently being displayed).

32

void menuSelectionChanged(boolean isIncluded)
Messaged when the menubar selection changes to activate or deactivate this menu.

33

void pack()
Lays out the container so that it uses the minimum space needed to display its contents.

34

protected void paintBorder(Graphics g)
Paints the popup menu's border if the borderPainted property is true.

35

protected String paramString()
Returns a string representation of this JPopupMenu.

36

protected void processFocusEvent(FocusEvent evt)
Processes focus events occurring on this component by dispatching them to any registered FocusListener objects.

37

protected void processKeyEvent(KeyEvent evt)
Processes key stroke events such as mnemonics and accelerators.

38

void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
Processes a key event forwarded from the MenuSelectionManager and changes the menu selection, if necessary, by using MenuSelectionManager's API.

39

void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
This method is required to conform to the MenuElement interface, but it not implemented.

40

void remove(int pos)
Removes the component at the specified index from this popup menu.

41

void removeMenuKeyListener(MenuKeyListener l)
Removes a MenuKeyListener from the popup menu.

42

void removePopupMenuListener(PopupMenuListener l)
Removes a PopupMenu listener.

43

void setBorderPainted(boolean b)
Sets whether the border should be painted.

44

static void setDefaultLightWeightPopupEnabled(boolean aFlag)
Sets the default value of the lightWeightPopupEnabled property.

45

void setInvoker(Component invoker)
Sets the invoker of this popup menu -- the component in which the popup menu menu is to be displayed.

46

void setLabel(String label)
Sets the popup menu's label.

47

void setLightWeightPopupEnabled(boolean aFlag)
Sets the value of the lightWeightPopupEnabled property, which by default is true.

48

void setLocation(int x, int y)
Sets the location of the upper left corner of the popup menu using x, y coordinates.

49

void setPopupSize(Dimension d)
Sets the size of the Popup window using a Dimension object.

50

void setPopupSize(int width, int height)
Sets the size of the Popup window to the specified width and height.

51

void setSelected(Component sel)
Sets the currently selected component, This will result in a change to the selection model.

52

void setSelectionModel(SingleSelectionModel model)
Sets the model object to handle single selections.

53

void setUI(PopupMenuUI ui)
Sets the L&F object that renders this component.

54

void setVisible(boolean b)
Sets the visibility of the popup menu.

55

void show(Component invoker, int x, int y)
Displays the popup menu at the position x,y in the coordinate space of the component invoker.

56

void updateUI()
Resets the UI property to a value from the current look and feel.

方法繼承

這個類從以下類繼承的方法:

  • javax.swing.JComponent

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

JPopupMenu 例子

選擇使用任何編輯器創建以下java程序在 D:/ > SWING > com > yiibai > gui >

SwingMenuDemo.java

package com.yiibai.gui; import java.awt.*; import java.awt.event.*; public class SwingMenuDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingMenuDemo(){ prepareGUI(); } public static void main(String[] args){ SwingMenuDemo swingMenuDemo = new SwingMenuDemo(); swingMenuDemo.showPopupMenuDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java SWING Examples - by www.yiibai.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("Edit"); JMenuItem cutMenuItem = new JMenuItem("Cut"); cutMenuItem.setActionCommand("Cut"); JMenuItem copyMenuItem = new JMenuItem("Copy"); copyMenuItem.setActionCommand("Copy"); JMenuItem pasteMenuItem = new JMenuItem("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); mainFrame.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { editMenu.show(mainFrame, e.getX(), e.getY()); } }); mainFrame.add(editMenu); mainFrame.setVisible(true); } }

編譯程序,使用命令提示符。到 D:/ > SWING 然後輸出以下命令。

D:SWING>javac comyiibaiguiSwingMenuDemo.java

如果沒有錯誤出現,這意味着編譯成功。使用下面的命令來運行程序。

D:SWING>java com.yiibai.gui.SwingMenuDemo

驗證下面的輸出。(點擊屏幕中間)