Question

I've just begun a simple GUI project, and while creating the menu bar, I ran into an error that I find to be inexplicable. I want to add an ActionListener to a JMenuItem using addActionListener, as I have done in the past. However, when I apply said method, Eclipse gives an error: "Syntax error on token "addActionListener", = expected after this token." My only thoughts are that perhaps addActionListener is being interpreted as a property rather than a method... but I have used this method in the past so I do know that it works. I am unsure of how much code I should provide, so please let me know if I should edit in more.

package com.movethehead;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class Main extends JFrame {

    private final int W = 500;
    private final int H = 500;

    JMenuBar menuBar = new JMenuBar();

    JMenu file = new JMenu("File");
    JMenuItem exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
                System.exit(0);
        }
    });

    JMenu headMenu = new JMenu("Heads");
    JMenu bgMenu = new JMenu("Backgrounds"); 

    public Main() {
        setTitle("Move the Head");
        setSize(W, H);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        add(new Pnl());
        setJMenuBar(menuBar);
    } // end constructor

    public static void main( String[] args ) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Main f = new Main();
                f.setVisible(true);
        }
    });
} // end main()
} // end Main
Was it helpful?

Solution

What it looks to me is you have

JMenuBar menuBar = new JMenuBar();

JMenu file = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ev) {
            System.exit(0);
    }
});

JMenu headMenu = new JMenu("Heads");

outside of any method definition and there is no way to call that code.

Try this:

public class Main extends JFrame{

  //initialize integer height/width values along with declaring 
  //Swing component variables
  private final int W = 500,
                    H = 500;

  private JMenu file, headMenu, bgMenu;
  private JMenuBar menuBar;
  private JMenuItem exitItem;

  //constructor
  public Main(){
    setTitle("Move the Head");
    setSize(W, H);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    initializeElements();

  }

  //Initializes the elements, this part is missing from your code above.
  public void initializeElements(){

    menuBar = new JMenuBar();
    file = new JMenu("File");
    exitItem = new JMenuItem("Exit");

    exitItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        System.exit(0);
      }
    });

    headMenu = new JMenu("Heads");
    bgMenu = new JMenu("Backgrounds");

  }

  public static void main( String[] args ) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            Main f = new Main();
            f.setVisible(true);
        }
    });
  }
}

OTHER TIPS

That code is fine. There's likely a syntax error on a nearby line that's messing up the parsing. Either the previous line, or something that screws up the declaration of exitItem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top