【发布时间】:2016-11-08 23:24:00
【问题描述】:
当我在同一个包中创建 Java 应用程序构建器和主类时,我将 javax swing 导入为import java.swing.*,然后我在主类中出现错误。
主类
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
}
}
我的最后两行错误为The method setVisible(boolean) is undefined for the type 和另一行The method setDefaultCloseOperation(int) is undefined for the type。
我已经将 JRE 路径设置为,
在 Eclipse 中安装(并选择)JDK:
Window -> Preferences -> Java -> Installed JREs选定的 JDK:
Project -> Properties -> Java Build Path -> Libraries包括“Java Builder”:
Project -> Properties -> Builders
但我遇到了上述错误。我可以帮我修复这个错误以运行我的代码吗...?
【问题讨论】:
-
MainFrame未在javax.swing包中声明。我很惊讶您在MainFrame mf = new MainFrame()行没有收到错误,除非您导入的不仅仅是javax.swing.*, -
右键单击您的项目 -> 属性 -> 构建路径 -> 确保您的“库”选项卡有 JDK(系统库)
-
我认为您将 Scala Swing 与 Java Swing 混淆了。我想你想要
JFrame。 -
JFrame mf = new JFrame();
-
MainFrame是你自己的类吗,它扩展了 JFrame 吗?