我刚刚开始使用java并尝试使用我在网上找到的一些示例代码来开始,但由于某种原因,我无法编译此代码 . 我在Ubuntu 16.04上安装了“default-jdk” .
这是代码:
import java.awt.*;
import java.awt.event.WindowListener;
import javax.swing.*;
import java.io.*;
public class Test extends JFrame{
public static void main (String argv [])
{
new Test("Window Application");
}
public Test(String title)
{
super(title);
setSize(200, 100);
addWindowListener((WindowListener) new WindowDestroyer());
setVisible(true);
}
private class WindowDestroyer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
当我尝试做javac Test.java时,我得到2找不到符号错误 .
私有类WindowDestroyer扩展WindowAdapter
public void windowClosing(WindowEvent e)
2 回答
我可以看到,您创建了简单的Swing应用程序窗口并关闭它关闭窗口 . 你以不正确的方式做到这一点 . 使用
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
要好得多(只有你不打算做smth . 特殊之前) . 并使用SwingUtilities.invokeLater()
在AWT事件派发线程上异步执行:从Java 8 docs for WindowAdapter开始,它被定义为
java.awt.event.WindowAdapter
.您需要先导入该类:
除了你的其他进口 .
作为旁注,你可能很想做
避免将来出现导入错误 .
我建议阅读有关Why is using a wild card with a Java import statement bad?的讨论,以了解这方面的问题 .