JMenu Popup不准确吗?

时间:2013-04-21 16:47:22

标签: java swing debugging jbutton jpopupmenu

问题图片:

http://gyazo.com/56c4f7f5fc10805695fc80de567b92c5.png

    private JButton settingsButton = new JButton("Settings");

    private JPopupMenu settings = new JPopupMenu();

    private JMenuItem accounts = new JMenuItem("Accounts");
settings.add(accounts);     
        settingsButton.addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    settings.show(e.getComponent(), 0, 0);
                }
            });

正如您在图片上看到的那样,我认为这不是准确的。是否有(更好的)方法将它放在正确的位置?

我点击了JButton:“设置”,它应该弹出一点点。

1 个答案:

答案 0 :(得分:1)

您最好使用JMenuBar,然后将JMenus添加到JMenuItems,重新发明轮子是没有意义的:

JMenuBar menubar = new JMenuBar();
JMenu settings = new JMenu("Settings");
JMenuItem accounts = new JMenuItem("Accounts");
settings.add(accounts);
menubar.add(settings);
myFrame.setJMenuBar(menubar);