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 :
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:
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 i1should 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_CLOSEproperty value. You would want it to beDISPOSE_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: