public class Question extends JFrame { public Question() { super.setTitle("Quiz Game");public class RunQuiz { public static void main(String[] args) {} Question q = new Question(); }为什么不显示窗口 super.setSize(100, 150); super.setLocation(300, 200); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(); this.add(panel); JTextField textField = new JTextField(); panel.add(textField, BorderLayout.SOUTH); panel.add(textField, new GridLayout(1, 3)); } }
时间: 2024-01-16 17:02:29 浏览: 157
您的代码存在一些错误。您在 `Question` 类的构造函数中添加了一个 `public` 关键字,这将导致编译错误。另外,您在 `add` 方法中添加了两次 `textField`,这将导致在面板中只显示一个文本框。以下是修复后的代码:
```
public class Question extends JFrame {
public Question() {
super.setTitle("Quiz Game");
super.setSize(100, 150);
super.setLocation(300, 200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
this.add(panel);
JTextField textField = new JTextField();
panel.add(textField, BorderLayout.SOUTH);
panel.add(new JLabel("Label 1")); // 添加标签
panel.add(new JLabel("Label 2")); // 添加标签
}
}
public class RunQuiz {
public static void main(String[] args) {
Question q = new Question(); // 创建 Question 实例
}
}
```
这样运行 `main` 方法时,将创建 `Question` 实例并显示窗口,其中包括一个文本框和两个标签。
阅读全文