1

我正在尝试为文件浏览器制作一组导航按钮。我想要这样,如果用户点击专用的历史按钮,就会出现一个 JPopupMenu。但是,我还希望在用户右键单击或将光标向下拖动到后退或前进按钮时出现完全相同的菜单。如何为不同手势的多个 GUI 组件制作完全相同的 JPopupMenu(不是副本,而是完全相同的)?

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

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());
  }
});

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

4

1 回答 1

2

弹出弹出菜单mousePressed()显示了使用和mouseReleased()的传统实现isPopupTrigger()。请注意,“应显示弹出菜单的确切手势因外观而异。” 您可以将显示的内容与您的实现进行比较,该实现使用mousePressed().

附录:作为参考,@mKorbel 回忆了这个可能被证明有用的客户端属性。

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();
            }
        });
    }
}
于 2012-06-02T11:20:45.157 回答