0

I am trying to open the menu frame using a button on the main frame. I added an event to the button and I tried calling the other class but it keeps giving me an error of ":: expected after this token"

This is my main frame

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

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


public class Main extends JFrame {

public static JPanel mainPane;
public final JButton menuButton = new JButton("New button");

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Main frame = new Main();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    mainPane = new JPanel();
    mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(mainPane);
    mainPane.setLayout(null);
    menuButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Menu.main(String[] args);
        }
    });
    menuButton.setBounds(76, 89, 104, 32);
    mainPane.add(menuButton);
}
}

And this is my menu frame

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;


public class Menu extends JFrame {

public static JPanel menuPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Menu frame = new Menu();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Menu() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    menuPane = new JPanel();
    menuPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(menuPane);
    menuPane.setLayout(null);

    JLabel menuTitle = new JLabel("Menu");
    menuTitle.setBounds(194, 11, 46, 14);
    menuPane.add(menuTitle);

}
}
Javv48
  • 11

1 Answers1

1

change your action event to this.no need to call main method .create a new instance of Menu class instead.

 menuButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Menu menu = new Menu();
            menu.setVisible(true);
        }
    });

if you relly want to call main method then use

menuButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
           Menu.main(new String[0]);
        }
    });

the error is here

Menu.main(String[] args);//error

this is not a correct way of passing arguments to a methods.this is declaration of parameter list.

you can correct error by changing it to ,

String args[] = null;
Menu.main(args);    //correct
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60