gpt4 book ai didi

java - Jframe 未显示

转载 作者:行者123 更新时间:2023-12-01 17:56:54 24 4
gpt4 key购买 nike

我在应用程序中创建 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com