当单击,右键单击或拖动三个JButton中的任何一个时,如何显示JPopupMenu? [英] How do I make a JPopupMenu appear when any one of 3 JButtons is clicked, right-clicked, or dragged upon?

查看:175
本文介绍了当单击,右键单击或拖动三个JButton中的任何一个时,如何显示JPopupMenu?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为文件浏览器设置一组导航按钮.我想要这样,以便如果用户单击专用历史记录按钮,则会显示JPopupMenu.但是,我还希望用户在后退或前进按钮上单击鼠标右键或拖动鼠标时,出现完全相同的菜单. 如何为多个具有不同手势的GUI组件显示完全相同的JPopupMenu(不是一个副本,而是一个完全相同的副本)?

I am trying to make a set of navigation buttons for a file browser. I want it so that if the user clicks the dedicated history button, a JPopupMenu appears. However, I also want that exact same menu to appear when the user right-clicks or drags the cursor down the back or forward button. How can I make that exact same JPopupMenu (not a copy, but the same exact one) appear for multiple GUI components for different gestures?

到目前为止,我已经尝试了以下方法:

So far I've tried the following:

histButton.addMouseListener(new MouseAdapter()
{
  @Override public void mouseClicked(MouseEvent e)
  {
    showPopup(e);
  }

  @Override public void mouseDragged(MouseEvent e)
  {
    showPopup(e);
  }

  private void showPopup(MouseEvent e)
  {
    histPopupMenu.show(e.getComponent(), e.getX(), e.getY());
  }
});
forwardButton.addMouseListener(new MouseAdapter()
{
  @Override public void mouseClicked(MouseEvent e)
  {
    if (e.isPopupTrigger())
      showPopup(e);
  }

  @Override public void mouseDragged(MouseEvent e)
  {
    showPopup(e);
  }

  private void showPopup(MouseEvent e)
  {
    histPopupMenu.show(e.getComponent(), e.getX(), e.getY());
  }
});
backButton.addMouseListener(new MouseAdapter()
{
  @Override public void mouseClicked(MouseEvent e)
  {
    if (e.isPopupTrigger())
      showPopup(e);
  }

  @Override public void mouseDragged(MouseEvent e)
  {
    showPopup(e);
  }

  private void showPopup(MouseEvent e)
  {
    histPopupMenu.show(e.getComponent(), e.getX(), e.getY());
  }
});

所有组件均已添加并正确显示,并且调试显示它们已注册事件,但没有菜单出现.

All components are added and display correctly, and debugging shows me that they register the events, but no menu appears.

推荐答案

mousePressed()的实现进行比较.

Bringing Up a Popup Menu shows the traditional implementation using mousePressed(), mouseReleased() and isPopupTrigger(). Note that "The exact gesture that should bring up a popup menu varies by look and feel." You might compare what's shown with your implementation, which uses mousePressed().

附录:@mKorbel回顾了客户端属性,以供参考.有用.

Addendum: For reference, @mKorbel recalls this client property that may prove useful.

import java.awt.Component;
import java.awt.event.*;
import javax.swing.*;

/** @author mKorbel */
public class ComboBoxAction extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JComboBox comboBox;
    private JFrame frame;

    public ComboBoxAction() {
        comboBox = new JComboBox();
        comboBox.addActionListener(this);
        comboBox.addItem("Item 1");
        comboBox.addItem("Item 2");
        comboBox.addItem("Item 3");
        comboBox.addItem("Item 4");
        for (Component component : comboBox.getComponents()) {
            if (component instanceof AbstractButton) {
                if (component.isVisible()) {
                    comboBox.remove(component);
                }
            }
        }
        //This prevents action events from being fired when the
        //up/down arrow keys are used on the dropdown menu
        comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
        comboBox.firePopupMenuWillBecomeVisible();
        frame = new JFrame();
        frame.add(comboBox);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(comboBox.getSelectedItem());
        //make sure popup is closed when 'isTableCellEditor' is used
        comboBox.hidePopup();
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ComboBoxAction();
            }
        });
    }
}

这篇关于当单击,右键单击或拖动三个JButton中的任何一个时,如何显示JPopupMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆