javanetbeansmainclass

<No main classes found> ............ how can i solve it?


I was trying to compile my simple program. But when I want to compile this program :

import javax.swing.*;

public class First {


JFrame f;  

First(){  


f=new JFrame();   

    String i1 = JOptionPane.showInputDialog(f,"Enter Name");      
}  

public static void main(String[] args) {  


new JOptionPane();  
}  

}

I get this message : <No main classes found>

and my IDE is : netbeans

and it is the photo :

https://i.sstatic.net/pq6Y8.png


Solution

  • Well, the way you've created this class, it makes me wonder if there yet another class in your project that is suppose to actually be the startup class since, in order to display the Input Box for name entry, you need to make an instance of First (not JOptionPane()) so that the constructor can fire it.

    If this is the only class within your project then you can still fire the Input Box but you need to create an instance of First within the main() method, like this:

    public static void main(String[] args) {
        /* You could just use:  new First();  but you'll see 
           the NetBeans yellow Warning underline beneath the 
           code line which you can ignore. Better to provide
           a variable to the instance of First as done here. */
        First f2 = new First();  
    }
    

    Overall, it may be better to just place the name entry prompt directly into the main() method. Once the name is supplied you would also want to retain that name into a string variable that is perhaps global to the entire class rather than just within the scope of the Constructor. String i1 should perhaps be declared as a class member variable, and named something a little more appropriate, like perhaps: String userName;.

    I get the idea of the JFrame since JOptionPanes like to hide behind the IDE (or other 'On Top' windows) if there is no parent component and null is used. But if you do this then set it up so that it doesn't inadvertently close your application with the default EXIT_ON_CLOSE property value. You would want it to be DISPOSE_ON_CLOSE. You would also want the JFrame's setAlwaysOnTop property to be set to boolean true. After your JOptionPane is used be sure to dispose of the JFrame otherwise your application will remain active until something actually closes it. You can see this in the example below:

    import javax.swing.*;
    
    public class First {
    
        private static JFrame dialogPARENT;    // Parent used for dialogs that don't have a parent.
        private static String userName;        // Holds the User Name supplied in either the Input Box or Setter method.
    
        // Class Constructor
        First() {
            dialogPARENT = new JFrame();
            dialogPARENT.setAlwaysOnTop(true);
            dialogPARENT.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            dialogPARENT.setLocationRelativeTo(null);
    
            userName = JOptionPane.showInputDialog(dialogPARENT, "Enter Your Name:");
            dialogPARENT.dispose();  // dispose of the JFrame.
        }
    
        public static void main(String[] args) {
            First first = new First();   // Fires the constructor
    
            // Display the User Name. As you can see, basic HTML can 
            // be used in your Message Box dialog display string.
            JOptionPane.showMessageDialog(dialogPARENT, "<html>The name you entered is:<br><br>"
                    + "<center><font color=red><b>" + userName + "</b></font></center><br></html>", 
                      "Supplied User Name", JOptionPane.INFORMATION_MESSAGE);
            dialogPARENT.dispose();   // dispose of the JFrame.
        }
    }