Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot solve import javax.swing.* error in Eclipse

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...?

like image 434
Nuwan Samarasinghe Avatar asked Aug 08 '26 09:08

Nuwan Samarasinghe


2 Answers

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
like image 158
DarkV1 Avatar answered Aug 10 '26 21:08

DarkV1


Are you possibly looking for JFrame rather than MainFrame?

like image 39
gottfred Avatar answered Aug 10 '26 21:08

gottfred



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!