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

class Main extends JFrame
{
   public  Main(String st)
   {

      setLayout(null);
     setDefaultCloseOperation(javax.swing. WindowConstants.DISPOSE_ON_CLOSE);

     JLabel lab=new JLabel("Welcome  "+st+"!! \n Click OK to Continue");
     JButton OK = new JButton("OK");
                OK.setMnemonic('k');
		OK.addActionListener(
                new ActionListener()
                  {
                    JButton OK;
                     public void actionPerformed(ActionEvent e)
			{
                         Main();
                }
                });

     lab.setBounds(10,10,500,20);
     OK.setBounds (80,80,120,28);
     add (OK);
     add(lab);
       setSize(300,200);
      }


	public  Main()
	{
		super("Malay Synonym Word Checker");

		JMenuBar bar = new JMenuBar();
		setJMenuBar(bar);

		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic('r');



		bar.add(fileMenu);
		bar.add(helpMenu);

		JMenuItem aboutItem = new JMenuItem("About");
		aboutItem.setMnemonic('A');
		aboutItem.addActionListener(

		new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showMessageDialog(getRootPane(), "This is a final year project by \nNur Amillia bt Amran.", "About", JOptionPane.PLAIN_MESSAGE);
			}
		}
		);

		JMenuItem exitItem = new JMenuItem("Exit");
		exitItem.setMnemonic('x');
		exitItem.addActionListener(

		new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		}
		);

		fileMenu.add(aboutItem);
		fileMenu.addSeparator();
		fileMenu.add(exitItem);



        }

	public static void main(String[] Args)
	{

               Main f =new Main();

                JPanel mypane = new JPanel(new GridLayout(5,3));

                //f.setLayout(null);
                JButton Request = new JButton("REQUEST");....
..................................................

hai...I have problem when I have to call public Main() after clicking the OK button...this Main () should be written under the red color site..but when i called Main(),it said 'cannot find symbol'

what shoul i do??

Recommended Answers

All 11 Replies

You can't call a constructor like that - it's implicit in new Main(...).
I'm confused about your two constructors (not to mention the initialisation code in the main(...) method). Do you want to execute all the code in both of these when instantiating a new Main? If not, when do you want to execute each of them? Probably these should either be a single constructor, or at least one of these should just be an ordinary method

I have a class call Login..

if (value1.equals(uname) && value2.equals(pass)) {
            f.setVisible(false);
            Main page=new Main(uname);
             page.setVisible(true);

after the login succeed,it will go to Main class where it will execute

public  Main(String st)
   {

      setLayout(null);
     setDefaultCloseOperation(javax.swing. WindowConstants.DISPOSE_ON_CLOSE);

     JLabel lab=new JLabel("Welcome  "+st+"!! \n Click OK to Continue");
     JButton OK = new JButton("OK");
                OK.setMnemonic('k');
		OK.addActionListener(
                new ActionListener()
                  {
                    JButton OK;
                     public void actionPerformed(ActionEvent e)
			{
                       Main(); 
                }
                });

     lab.setBounds(10,10,500,20);
     OK.setBounds (80,80,120,28);
     add (OK);
     add(lab);
       setSize(300,200);
      }

So,after we click the OK button it will execute

public  Main()
	{
		super("Malay Synonym Word Checker");

		JMenuBar bar = new JMenuBar();
		setJMenuBar(bar);

		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic('r');



		bar.add(fileMenu);
		bar.add(helpMenu);

		JMenuItem aboutItem = new JMenuItem("About");
		aboutItem.setMnemonic('A');
		aboutItem.addActionListener(

		new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showMessageDialog(getRootPane(), "This is a final year project by \nNur Amillia bt Amran.", "About", JOptionPane.PLAIN_MESSAGE);
			}
		}
		);

		JMenuItem exitItem = new JMenuItem("Exit");
		exitItem.setMnemonic('x');
		exitItem.addActionListener(

		new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		}
		);

		fileMenu.add(aboutItem);
		fileMenu.addSeparator();
		fileMenu.add(exitItem);



        }

	public static void main(String[] Args)
	{

               Main f =new Main();

                JPanel mypane = new JPanel(new GridLayout(5,3));

                //f.setLayout(null);
                JButton Request = new JButton("REQUEST");........
...................

OK, in that case I see no reason why the second method is a constructor - why not just have it as an ordinary method called addMenus() or some such (I assume the call in the final line 55 is just a debugging thing, not how it will normally be used)

OK, in that case I see no reason why the second method is a constructor - why not just have it as an ordinary method called addMenus() or some such (I assume the call in the final line 55 is just a debugging thing, not how it will normally be used)

So,what should i do with that error at line 55??? I can't run the program..

In one post you describe starting with Main(string) which calls Main() sometime later. In another post you call Main() directly at startup. What exactly is the correct sequence starting from when the program is first run?

ok2...I'm sorry if I get u confused...I'll try to explain what I have now...


I have a class call Login..

if (value1.equals(uname) && value2.equals(pass))
 
            f.setVisible(false);
            Main page=new Main(uname);
             page.setVisible(true);

after the login succeed,it will go to Main class where it will execute

public  Main(String st)
   {
super("Malay Synonym Word Checker");
      setLayout(null);
     setDefaultCloseOperation(javax.swing. WindowConstants.DISPOSE_ON_CLOSE);

     JLabel lab=new JLabel("Welcome  "+st+"!! \n Click OK to Continue");
     JButton OK = new JButton("OK");
                OK.setMnemonic('k');
		OK.addActionListener(
                new ActionListener()
                  {
                    JButton OK;
                     public void actionPerformed(ActionEvent e)
			{
                       addMenus();
                }
                });

     lab.setBounds(10,10,500,20);
     OK.setBounds (80,80,120,28);
     add (OK);
     add(lab);
       setSize(300,200);
      }

So,after we click the OK button it will execute

public void addMenus()
	{
	
		JMenuBar bar = new JMenuBar();
		setJMenuBar(bar);
 
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic('r');...............

 ................................
}
 

 
	public static void main(String[] Args)
	{
 
                Main f =new Main(); <--- ERROR
 
                JPanel mypane = new JPanel(new GridLayout(5,3));
 
                //f.setLayout(null);
                JButton Request = new JButton("REQUEST");........
...................

So why does main want to call Main()??? From your description main should be calling the Logon class - that's where execution starts.

I can't do that,bcause all the operations is start from the Main()..if I changes it to Login class, then this error occur...

public static void main(String[] Args)
	{

               Login f =new Login();

                JPanel mypane = new JPanel(new GridLayout(5,3));

               
                JButton Request = new JButton("REQUEST");

                JButton Search = new JButton("SEARCH");
                Search.setMnemonic('a');
		Search.addActionListener(
                new ActionListener()...........................
.......................................................
...............................................................
  }
                });



                mypane.add(Request);
                mypane.add(new JLabel(""));
                mypane.add(Request);
                f.add(mypane);
                f.setVisible(true);
                f.setSize(550,200);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

The problem here is that the code for initialising various parts of your app is distributed around various classes & methods in a confusing way. Based on what you have said so far it would be better to group it together like this:
Login class:
Constructor does everything to set up and display Login dialog. When login is completed successfully it creates a new MainWindow passing the name as parameter.
MainWindow class (better name than just Main!):
Constructor takes user name as parameter and does everything to set up and display the main window in its initial state.
ActionListener calls method to add menus (etc) as required.

public static void main(String[] Args)
In a real app there will be other application initialisation (eg opening databases, loading preferences) and it makes sense to have an app startup class that does all this via main. But in your case you can put this method in almost any public class - eg Logon. The important thing is that all it does is call new Logon() - nothing else. All the other code for setting up windows etc belongs in the appropriate constructor - this is why you are having problems with the code in your latest post.

My Program workk!!!! yeayyyyy!!! thank you soooo soooo much for your help!!! =D

That's great. Time to mark this thread as "solved"?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.