Java JMenu不会自动取消选择

Java JMenu不会自动取消选择,java,swing,jmenu,jmenubar,Java,Swing,Jmenu,Jmenubar,我正在从事一个java项目,其中使用了一些swing组件。我对JMenuBar有问题。我有一个扩展JMenuBar的类。它是功能性的,它是一对JMenu对象,每个对象中都有一对jmenuItem。问题是,在某些情况下,在我选择JMenu之后,它不会自动取消选择。例如,如果我在选择JMenu时选择了一个JMenu,它的JMenuItems将像列表一样可见,并按键盘上的alt+tab键,则具有JMenuBar的JFrame对象将被隐藏,但JMenu在屏幕上静止不动。另一种情况是,当我选择一个JMen

我正在从事一个java项目,其中使用了一些swing组件。我对JMenuBar有问题。我有一个扩展JMenuBar的类。它是功能性的,它是一对JMenu对象,每个对象中都有一对jmenuItem。问题是,在某些情况下,在我选择JMenu之后,它不会自动取消选择。例如,如果我在选择JMenu时选择了一个JMenu,它的JMenuItems将像列表一样可见,并按键盘上的alt+tab键,则具有JMenuBar的JFrame对象将被隐藏,但JMenu在屏幕上静止不动。另一种情况是,当我选择一个JMenu并使用鼠标移动主JFrame时,该帧会被移动,但JMenu不会被取消选择或移动,而是再次静止。我会放一些图片和我的代码来解释

这是在选择菜单后按alt+tab的场景-这是不需要的:

这是在选择JMenu后用鼠标右键移动主JFrame的场景-这也是不需要的:

这是我定制的JMenuBar类的源代码:

public class EOPLMenuBar extends JMenuBar{

private EOPLMenuBarListenerDelegate delegate;


public EOPLMenuBar(EOPLMenuBarListenerDelegate delegate){
    this.delegate = delegate;
    initEOPLMenuBar();

}
private void initEOPLMenuBar(){

    // --- JMenu Initializations -------- //
    JMenu eoplMenu = new JMenu("EOPL");
    Font f = new Font(eoplMenu.getFont().getFontName(), Font.BOLD, eoplMenu.getFont().getSize());
    eoplMenu.setFont(f);


    eoplMenu.addMenuListener(new MenuListener() {
        @Override
        public void menuSelected(MenuEvent menuEvent) {
            System.out.println("Menu selected");
            //To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public void menuDeselected(MenuEvent menuEvent) {
            System.out.println("Menu deselected");
        }

        @Override
        public void menuCanceled(MenuEvent menuEvent) {
            System.out.println("Menu canceled");
        }
    });

    JMenu fileMenu = new JMenu("File");
    JMenu examplesMenu = new JMenu("Examples");

    this.add(eoplMenu);
    this.add(fileMenu);
    this.add(examplesMenu);

    ///------EOPLMenu items---------- ///
    JMenuItem item1 = new JMenuItem(MenuItemType.ABOUT_EOPL_GUI.getName());
    JMenuItem item2 = new JMenuItem(MenuItemType.SETTINGS.getName());
    JMenuItem item3 = new JMenuItem(MenuItemType.QUIT_EOPL_GUI.getName());

    eoplMenu.add(item1);
    eoplMenu.addSeparator();
    eoplMenu.add(item2);
    eoplMenu.addSeparator();
    eoplMenu.add(item3);


    ///------FileMenu items-----------///
    JMenuItem item4 = new JMenuItem(MenuItemType.SAVE_FILE.getName());
    fileMenu.add(item4);

    ///------ExampleMenu items -------///
    JMenu letMenu = new JMenu("Proc");
    JMenuItem item5 = new JMenuItem(MenuItemType.PROC_EXAMPLE_1.getName());
    JMenuItem item6 = new JMenuItem(MenuItemType.PROC_EXAMPLE_2.getName());
    letMenu.add(item5);
    letMenu.add(item6);
    JMenuItem item7 = new JMenuItem(MenuItemType.LET_EXAMPLE_1.getName());
    JMenuItem item8 = new JMenuItem(MenuItemType.LET_EXAMPLE_2.getName());
    JMenuItem item9 = new JMenuItem(MenuItemType.LETREC_EXAMPLE_1.getName());
    JMenuItem item10 = new JMenuItem(MenuItemType.LETREC_EXAMPLE_2.getName());
    JMenuItem item11 = new JMenuItem(MenuItemType.CALL_BY_NEED_EXAMPLE_1.getName());
    JMenuItem item12 = new JMenuItem(MenuItemType.CALL_BY_NEED_EXAMPLE_2.getName());
    JMenuItem item13 = new JMenuItem(MenuItemType.CALL_BY_REF_EXAMPLE_1.getName());
    JMenuItem item14 = new JMenuItem(MenuItemType.CALL_BY_REF_EXAMPLE_2.getName());

    examplesMenu.add(letMenu);
    examplesMenu.add(item7);
    examplesMenu.add(item8);
    examplesMenu.add(item9);
    examplesMenu.add(item10);
    examplesMenu.add(item11);
    examplesMenu.add(item12);
    examplesMenu.add(item13);
    examplesMenu.add(item14);

    // -- Add ActionListeners to all items.
    item1.addActionListener(new EOPLMenuBarListener(item1.getText()));
    item2.addActionListener(new EOPLMenuBarListener(item2.getText()));
    item3.addActionListener(new EOPLMenuBarListener(item3.getText()));
    item4.addActionListener(new EOPLMenuBarListener(item4.getText()));
    item5.addActionListener(new EOPLMenuBarListener(item5.getText()));
    item6.addActionListener(new EOPLMenuBarListener(item6.getText()));

}



private class EOPLMenuBarListener implements ActionListener{

    private String itemType;


    public EOPLMenuBarListener(String type){
        this.itemType = type;
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        //To change body of implemented methods use File | Settings | File Templates.

        if(itemType.equals(EOPLMenuBar.MenuItemType.LET_EXAMPLE_1.getName())){

        delegate.prepareUIWithExampleSourcecode("let x = -(4,1) in -(x,1)","let");

        }else if(itemType.equals(EOPLMenuBar.MenuItemType.LET_EXAMPLE_2.getName())){

        delegate.prepareUIWithExampleSourcecode("let x = 3 in let x = -(x,1) in x","let");

        }
    }


}

public enum MenuItemType{

    ABOUT_EOPL_GUI    (String.format("%-15s","About EOPL-GUI")),
    SETTINGS          (String.format("%-15s","Settings")),
    QUIT_EOPL_GUI     (String.format("%-15s","Quit EOPL-GUI")),
    SAVE_FILE         (String.format("%-15s","Save File")),
    LET_EXAMPLE_1     (String.format("%-15s","Let example-1")),
    LET_EXAMPLE_2     (String.format("%-15s","Let example-2")),
    PROC_EXAMPLE_1     (String.format("%-15s","Example 1")),
    PROC_EXAMPLE_2     (String.format("%-15s","Example 2")),
    LETREC_EXAMPLE_1     (String.format("%-15s","Letrec example-1")),
    LETREC_EXAMPLE_2     (String.format("%-15s","Letrec example-2")),
    CALL_BY_NEED_EXAMPLE_1     (String.format("%-15s","Call-by-need example-1")),
    CALL_BY_NEED_EXAMPLE_2     (String.format("%-15s","Call-by-need example-2")),
    CALL_BY_REF_EXAMPLE_1     (String.format("%-15s","Call-by-ref example-1")),
    CALL_BY_REF_EXAMPLE_2     (String.format("%-15s","Call-by-ref example-2"));


    private String name;

    MenuItemType(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }
  }
}
这是我使用自定义JMenuBar的源代码:

private static void startGUIFrame(){
    JFrame frame = new JFrame("EOPL GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(true);
    frame.setSize(900, 700);
    frame.setLocationRelativeTo(null);

    //Add content to the window.
    frame.add(eoplPanel);

    frame.setJMenuBar(new EOPLMenuBar(controller));

    //Display the window.
    frame.pack();
    frame.setVisible(true);


}

我在谷歌上搜索了几个小时,但没有找到运气。非常感谢您的帮助。

您的代码实际上没有问题。这是JMenuBar实现中的一个bug。

我希望调用“startGUIFrame”的代码实际上是在上执行的,我建议您谈论的是使用SwingUtilities调用“startGUIFrame”方法。invokeLater,不,我没有这样做。为了解决我用sdk从1.6更新到1.7的问题,问题解决了!是的,没错,这样的问题也会出现,若GUI并没有在EDT上更新,谢谢,我将java sdk从1.6更新到了1.7,现在问题已经解决了!