简体   繁体   中英

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

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

你可能在寻找JFrame而不是MainFrame吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM