我一直在尝试让这段代码正常工作,但我不知道它出了什么问题。我所展示的所有内容都表明要正确声明 JFrame,但我已经完成了,但它没有出现。这是我的代码:
import javax.swing.*;
import java.awt.*;
public class test extends JFrame {
private JFrame f;
private JPanel p;
private JButton b1;
private JLabel lab;
public void test() {
gui();
}
public void gui() {
JFrame f = new JFrame("Frame");
f.setBounds(30, 30, 700, 1000);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setFocusable(true);
p = new JPanel();
p.setBackground(Color.YELLOW);
b1 = new JButton("Button");
lab = new JLabel("Label");
p.add(b1);
p.add(lab);
f.add(p, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new test();
}
}
我不太了解编码,无法诊断问题,所以我来这里寻求帮助。预先感谢您!
最佳答案
那是因为您没有调用 test()
方法。看来您的意图是使此方法成为构造函数:
public void test() {
gui();
}
它应该是(构造函数没有返回类型):
public test() {
gui();
}
关于java - 文件编译正确,但 JFrame 未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48343783/