English 中文(简体)
2. 选择JPopupMenu职位
原标题:
  • 时间:2009-05-18 16:21:55
  •  标签:

我在遇到麻烦时看到<><>条码>。

What I need is to be able to right click on the work area, click some
menu item, and then create an element in place right under the position of the previously displayed menu.

I tried getting the position of the menu itself and of its items,
but it gives me constant values around 0. (see comments in code)

Because of separation of concerns, the menu is displayed in one class
while its actions are handled in another.

public final class MainFrameMenu
    extends JPopupMenu
    implements ActionListener {

  private final MainFrame mainFrame;
  private final JMenuItem item1 = new JMenuItem("add line");
  private final JMenuItem item2 = new JMenuItem("add element");

  public MainFrameMenu(MainFrame mainFrame) {
    super("Main menu");
    this.mainFrame = mainFrame;

    item1.addActionListener(this);
    item2.addActionListener(this);

    add(item1);
    add(item2);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == item1) {
      System.out.println(getLocation()); // gives ALWAYS [0, 0]
      System.out.println(item1.getLocation()); // gives ALWAYS [1, 3]

      // I want to create an element IN PLACE
      mainFrame.addConnectionLine(getX(), getY());
    }
  }

}

public final class PopupMouseListener
    extends MouseAdapter {

  private final JPopupMenu menu;

  public PopupMouseListener(JPopupMenu menu) {
    this.menu = menu;
  }

  @Override
  public void mousePressed(MouseEvent e) {
    popup(e);
  }

  @Override
  public void mouseReleased(MouseEvent e) {
    popup(e);
  }

  private void popup(MouseEvent e) {
    if (e.isPopupTrigger()) {
      menu.show(e.getComponent(), e.getX(), e.getY());
    }
  }

}

I know I could store the last known position in PopupMouseListener, and read it from MainFrameMenu, but it would ruin the classes hierarchy (most other occurrences of PopupMouseListener don t need it).

最佳回答

相对于母体组成部分而言,回收地点将协调空间。 因此,项目1. 植被(甲型六氯环己烷)的回报总是与在同一个地方的人群相同。

从阅读 Java理论来看,它像叫主人Frame.getMousePosition一样,可以回头看。

问题回答

问题已经过时,但最近我有这个问题。 这里的问题是,在《行动计划》发出呼吁期间,PopupMenu的可见度已经确定为虚假,即其位置不再界定(即0,0))。 我正在开发一个无休止的接口,因此,我不可能gra忙。 我最后说,每当看到菜单时,就把菜单储存起来,并以此建立我的组成部分。





相关问题
热门标签