0
JButton jButton1 = new JButton("Click Me");

public void showPopupMenu1(JButton invoker){
    JPopupMenu popupMenu = new JPopupMenu();
    popupMenu.setLayout(new GridLayout(5, 1));
    JMenu menu = new JMenu("Other");
    menu.add(new JMenuItem("A"));
    menu.add(new JMenuItem("B"));
    menu.add(new JMenuItem("C"));
    popupMenu.add(menu);
    popupMenu.add(new JButton("Apple"));
    popupMenu.add(new JButton("Banana"));
    popupMenu.add(new JButton("Carrot"));
    popupMenu.add(new JButton("Orange"));
    popupMenu.show(invoker, 0, invoker.getHeight());
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
    showPopupMenu1(jButton1);
}

当光标转到 Apple Button 时,菜单的弹出窗口不会关闭并仍标记为已选中。

4

0 回答 0