简体   繁体   中英

ActionListeners don't work in JMenuBar

I have some trouble with this code. My actionListeners are not working when I click on menu items. I think there is a problem in code, where I tried to bold it (**). I need menu on the left side of app, specifically like this. I couldn't write it myself so I used this example from Java GUI and edited. Important thing to say is, that items don't show their presence when I'm hovering mouse on them, they are like normal text. Have you got any advices what I should do to get my result?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class MainSite implements ActionListener
{

    private JMenuItem menu1 = new JMenuItem("menu 1");
    private JMenuItem menu2 = new JMenuItem("menu 2");
    private JMenuItem menu3 = new JMenuItem("menu 3");
    private JMenuItem menu4 = new JMenuItem("menu 4");

    public JMenuBar createMenuBar() 
    {
        JMenuBar menuBar = new JMenuBar();

        menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.PAGE_AXIS));
        menuBar.add(menu1);
        menuBar.add(menu2);
        menuBar.add(menu3);
        menuBar.add(menu4);
        menuBar.setBorder(BorderFactory.createMatteBorder(0,0,0,1,
                                                          Color.BLACK));
        return menuBar;
    }

    **private static void createAndShowGUI() 
    {

        JFrame F = new JFrame("App");
        F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        MainSite demo = new MainSite();
        Container contentPane = F.getContentPane();
        contentPane.setBackground(Color.WHITE);
        contentPane.add(demo.createMenuBar(),
                        BorderLayout.LINE_START);


        F.setSize(640,420);
        F.setVisible(true);
    }

    public static void main(String[] args) 
    {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }**

    @Override
    public void actionPerformed(ActionEvent E) {

        if(E.getSource() == menu1)
        {
            System.out.println("gnskfs");
        }

        if(E.getSource() == menu2)
        {
            System.out.println("gsdfogjs");
        }

        if(E.getSource() == menu3)
        {
            System.out.println("gfsjdkljgs");
        }

        if(E.getSource() == menu4)
        {
            System.out.println("hfsdajhg");
        }
    }
}

You don't add the ActionListener to the menu items:

menu1.addActionListener( this ):
...

Also, the menu bar should NOT be added to the content panel. There is a special area in the frame reserved for the menu bar. You use the setJMenuBar(...) method to add a menu bar to the frame.

Read the section from the Swing tutorial on How to Use Menus for working examples to get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM