When I create Java application builder and Main class in same package, and I imported javax swing as import java.swing.* then I have an error in main class.
Main class
import javax.swing.*;
public class SMS {
public static void main(String[] args){
MainFrame mf = new MainFrame(); //ok
mf.setVisible(true); //error
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //error
}
}
I had an error last two lines as The method setVisible(boolean) is undefined for the type and another one The method setDefaultCloseOperation(int) is undefined for the type.
And I already set JRE path as,
Installed (and selected) JDK in Eclipse : Window -> Preferences -> Java -> Installed JREs
Selected JDK : Project -> Properties -> Java Build Path -> Libraries
Included "Java Builder" : Project -> Properties -> Builders
But I had an above error. Can I help me to fix this error to run my code...?
Just change
MainFrame mf = new MainFrame();
to
JFrame mf = new JFrame();
Explanation
Unless mf is a class that extends JFrame you will not be able to invoke methods that are unique to it. Thus, in this case you can just simply change the object to a JFrame instead of MainFrame.
If MainFrame is your own internal class that you want to treat as a JFrame, add this to the class declaration:
extends JFrame
Are you possibly looking for JFrame rather than MainFrame?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With