我在应用程序中创建 JFrame 时遇到问题,因此我尝试运行最简单的代码来找出发生的情况。
public class prueba {
public static void main(String[] args) {
JFrame f = new JFrame("Primer JFrame");
f.add(new JLabel("Intenteu tancar per la creueta..."));
f.setSize(800,600);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
我很惊讶,这什么也没做,但是这段代码是从另一个项目中重新使用的,在这个项目中一切正常。这不会创建 JFrame,我只在控制台中得到它作为响应:
C:\Users\Ivan\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: -1073740791
最佳答案
运行此程序时我也遇到错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: EXIT_ON_CLOSE cannot be resolved to a variable
但是当我删除这一行时:
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
运行正常,但是退出窗口时,程序并没有立即退出。因此,您可以删除此行,或者如果您希望程序在窗口关闭后立即终止,请扩展 JFrame 并使用构造函数,如下所示:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class prueba extends JFrame {
private static final long serialVersionUID = 1L;
public prueba() {
add(new JLabel("Intenteu tancar per la creueta..."));
setTitle("Primer JFrame");
setSize(800,600);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new prueba();
}
}
关于java - Jframe 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44060644/