1

I am working on a java project where I use some swing components. I have a problem with the JMenuBar. I have a class that extends the JMenuBar. It is functional, it as couple of JMenu objects and couple of JMenuItems in each of it. The problem is that in some situations after I select a JMenu, it is not deselected automatically. For example if I select a JMenu (when a JMenu is selected its JMenuItems becomes visible like a list) and press alt+tab on the keyboard, JFrame object that has the JMenuBar gets hidden, but the JMenu stands still on the screen. Another situation is that, when I select a JMenu and move the main JFrame using mouse, the frame gets moved but JMenu doesn't get deselected nor moved, again stands still. I will put some images and my code to be more explanatory.

This is the scenario of pressing alt+tab after selecting a menu -- This is unwanted:

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

This is the scenario of moving the main JFrame with mouse right after selecting a JMenu -- This is also unwanted:

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

And here is my source code for my custom JMenuBar class:

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;
    }
  }
}

And here is my source code where I use my custom 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);


}

I searched for hours on google, but no luck. Any help is appreciated.

4

1 回答 1

0

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

于 2013-07-21T05:03:24.540 回答