JMenu未自动取消选择

时间:2013-07-20 23:50:17

标签: java swing jmenu jmenubar

我正在开发一个java项目,我使用了一些swing组件。我遇到了JMenuBar的问题。我有一个扩展JMenuBar的类。它是功能性的,它是每个JMenu对象和几个JMenuItems。问题是在我选择JMenu之后的某些情况下,它不会自动取消选择。例如,如果我选择JMenu(当选择JMenu时,其JMenuItems变得像列表一样可见)并按下键盘上的alt + tab,则JMenuBar的JFrame对象将被隐藏,但JMenu仍然停留在屏幕上。另一种情况是,当我选择一个JMenu并使用鼠标移动主JFrame时,框架会被移动,但JMenu不会被取消选择也不会移动,再次静止不动。我会把一些图像和我的代码更加解释。

这是在选择菜单后按下alt + tab的情况 - 这是不需要的:

http://i.imgur.com/0qL2pdD.png

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

http://i.imgur.com/c0BL4dE.png

这是我的自定义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);


}

我在谷歌搜索了几个小时,但没有运气。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您的代码实际上没有任何问题。这是JMenuBar实施中的错误。