- Swing 教程
- Swing - 首页
- Swing - 概述
- Swing - 环境
- Swing - 控件
- Swing - 事件处理
- Swing - 事件类
- Swing - 事件监听器
- Swing - 事件适配器
- Swing - 布局
- Swing - 菜单
- Swing - 容器
- Swing 有用资源
- Swing - 快速指南
- Swing - 有用资源
- Swing - 讨论
Swing - JPopupMenu 类
简介
弹出式菜单表示一个菜单,可以在组件内的指定位置动态弹出。
类声明
以下是javax.swing.JPopupMenu类的声明:
public class JPopupMenu
extends JComponent
implements Accessible, MenuElement
类构造函数
| 序号 | 构造函数和描述 |
|---|---|
| 1 |
JPopupMenu() 构造一个没有“调用者”的 JPopupMenu。 |
| 2 |
JPopupMenu(String label) 使用指定的标题构造一个 JPopupMenu。 |
类方法
| 序号 | 方法和描述 |
|---|---|
| 1 |
JMenuItem add(Action a) 将一个新的菜单项附加到菜单的末尾,该菜单项分派指定的 Action 对象。 |
| 2 |
JMenuItem add(JMenuItem menuItem) 将指定的菜单项附加到此菜单的末尾。 |
| 3 |
JMenuItem add(String s) 创建一个具有指定文本的新菜单项,并将其附加到此菜单的末尾。 |
| 4 |
void addMenuKeyListener(MenuKeyListener l) 向弹出式菜单添加 MenuKeyListener。 |
| 5 |
void addPopupMenuListener(PopupMenuListener l) 添加 PopupMenu 监听器。 |
| 6 |
void addSeparator() 在菜单的末尾附加一个新的分隔符。 |
| 7 |
protected PropertyChangeListener createActionChangeListener(JMenuItem b) 返回一个正确配置的 PropertyChangeListener,该监听器会在 Action 发生更改时更新控件。 |
| 8 |
protected JMenuItem createActionComponent(Action a) 工厂方法,用于为添加到 JPopupMenu 的 Action 创建 JMenuItem。 |
| 9 |
protected void firePopupMenuCanceled() 通知 PopupMenuListeners 此弹出式菜单已取消。 |
| 10 |
protected void firePopupMenuWillBecomeInvisible() 通知 PopupMenuListeners 此弹出式菜单将变为不可见。 |
| 11 |
protected void firePopupMenuWillBecomeVisible() 通知 PopupMenuListeners 此弹出式菜单将变为可见。 |
| 12 |
AccessibleContext getAccessibleContext() 获取与此 JPopupMenu 关联的 AccessibleContext。 |
| 13 |
Component getComponent() 返回此 JPopupMenu 组件。 |
| 14 |
Component getComponentAtIndex(int i) 已弃用。已替换为 Container.getComponent(int) |
| 15 |
int getComponentIndex(Component c) 返回指定组件的索引。 |
| 16 |
static boolean getDefaultLightWeightPopupEnabled() 获取 defaultLightWeightPopupEnabled 属性,默认值为 true。 |
| 17 |
Component getInvoker() 返回此弹出式菜单的“调用者”组件。 |
| 18 |
String getLabel() 返回弹出式菜单的标签。 |
| 19 |
Insets getMargin() 返回弹出式菜单的边框与其包含项之间的边距(以像素为单位)。 |
| 20 |
MenuKeyListener[]getMenuKeyListeners() 返回使用 addMenuKeyListener() 添加到此 JPopupMenu 的所有 MenuKeyListeners 的数组。 |
| 21 |
PopupMenuListener[] getPopupMenuListeners() 返回使用 addPopupMenuListener() 添加到此 JMenuItem 的所有 PopupMenuListeners 的数组。 |
| 22 |
SingleSelectionModel getSelectionModel() 返回处理单一选择的模型对象。 |
| 23 |
MenuElement[] getSubElements() 返回一个 MenuElements 数组,其中包含此菜单组件的子菜单。 |
| 24 |
PopupMenuUI getUI() 返回呈现此组件的 Look and Feel (L&F) 对象。 |
| 25 |
String getUIClassID() 返回呈现此组件的 L&F 类的名称。 |
| 26 |
void insert(Action a, int index) 在给定位置插入指定 Action 对象的菜单项。 |
| 27 |
void insert(Component component, int index) 在给定位置将指定的组件插入菜单。 |
| 28 |
boolean isBorderPainted() 检查是否应绘制边框。 |
| 29 |
boolean isLightWeightPopupEnabled() 获取 lightWeightPopupEnabled 属性。 |
| 30 |
boolean isPopupTrigger(MouseEvent e) 如果 MouseEvent 被 JPopupMenu 当前安装的 UI 视为弹出式触发器,则返回 true。 |
| 31 |
boolean isVisible() 如果弹出式菜单可见(当前正在显示),则返回 true。 |
| 32 |
void menuSelectionChanged(boolean isIncluded) 当菜单栏选择更改以激活或停用此菜单时发出消息。 |
| 33 |
void pack() 布局容器,使其使用显示其内容所需的最小空间。 |
| 34 |
protected void paintBorder(Graphics g) 如果 borderPainted 属性为 true,则绘制弹出式菜单的边框。 |
| 35 |
protected String paramString() 返回此 JPopupMenu 的字符串表示形式。 |
| 36 |
protected void processFocusEvent(FocusEvent evt) 通过将焦点事件分派给任何已注册的 FocusListener 对象来处理在此组件上发生的焦点事件。 |
| 37 |
protected void processKeyEvent(KeyEvent evt) 处理按键事件,例如助记符和加速键。 |
| 38 |
void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) 处理从 MenuSelectionManager 转发的按键事件,并根据需要使用 MenuSelectionManager 的 API 更改菜单选择。 |
| 39 |
void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager) 此方法需要符合 MenuElement 接口,但未实现。 |
| 40 |
void remove(int pos) 从此弹出式菜单中删除指定索引处的组件。 |
| 41 |
void removeMenuKeyListener(MenuKeyListener l) 从此弹出式菜单中删除 MenuKeyListener。 |
| 42 |
void removePopupMenuListener(PopupMenuListener l) 删除 PopupMenu 监听器。 |
| 43 |
void setBorderPainted(boolean b) 设置是否应绘制边框。 |
| 44 |
static void setDefaultLightWeightPopupEnabled(boolean aFlag) 设置 lightWeightPopupEnabled 属性的默认值。 |
| 45 |
void setInvoker(Component invoker) 设置此弹出式菜单的调用者,即要在其中显示弹出式菜单的组件。 |
| 46 |
void setLabel(String label) 设置弹出式菜单的标签。 |
| 47 |
void setLightWeightPopupEnabled(boolean aFlag) 设置 lightWeightPopupEnabled 属性的值,默认值为 true。 |
| 48 |
void setLocation(int x, int y) 使用 x, y 坐标设置弹出式菜单左上角的位置。 |
| 49 |
void setPopupSize(Dimension d) 使用 Dimension 对象设置弹出窗口的大小。 |
| 50 |
void setPopupSize(int width, int height) 将弹出窗口的大小设置为指定宽度和高度。 |
| 51 |
void setSelected(Component sel) 设置当前选定的组件,这将导致选择模型发生变化。 |
| 52 |
void setSelectionModel(SingleSelectionModel model) 设置模型对象以处理单一选择。 |
| 53 |
void setUI(PopupMenuUI ui) 设置呈现此组件的 L&F 对象。 |
| 54 |
void setVisible(boolean b) 设置弹出式菜单的可见性。 |
| 55 |
void show(Component invoker, int x, int y) 在组件调用者的坐标空间中的 x,y 位置显示弹出式菜单。 |
| 56 |
void updateUI() 将 UI 属性重置为当前外观和风格的值。 |
继承的方法
此类继承自以下类的方法:
- javax.swing.JComponent
- java.awt.Container
- java.awt.Component
- java.lang.Object
JPopupMenu 示例
使用您选择的任何编辑器创建以下 Java 程序,例如在D:/ > SWING > com > tutorialspoint > gui >
SwingMenuDemo.java
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 SwingJpopupMenu{
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingJpopupMenu(){
prepareGUI();
}
public static void main(String[] args){
SwingJpopupMenu swingMenuDemo = new SwingJpopupMenu();
swingMenuDemo.showPopupMenuDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
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);
}
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
statusLabel.setText(e.getActionCommand() + " MenuItem clicked.");
}
}
}
使用命令提示符编译程序。转到D:/ > SWING并键入以下命令。
D:\SWING>javac com\tutorialspoint\gui\SwingMenuDemo.java
如果未发生错误,则表示编译成功。使用以下命令运行程序。
D:\SWING>java com.tutorialspoint.gui.SwingMenuDemo
验证以下输出。(点击屏幕中间。)