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); JTextField textField = new JTextField(); this.add(textField,BorderLayout.SOUTH); this.add( textField, new GridLayout(1, 3)); } }为什么不显示面板
时间: 2024-01-30 21:03:24 浏览: 68
可能是因为您没有将面板添加到窗口中。在您的代码中,您只添加了一个文本框,但没有添加任何面板。您可以创建一个面板并将其添加到窗口中,例如:
```
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(textField, new GridLayout(1, 3));
}
}
```
这样您应该可以看到一个带有文本框和面板的窗口。
相关问题
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)); } }
您的代码存在一些错误。您在 `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` 实例并显示窗口,其中包括一个文本框和两个标签。
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(textField, new GridLayout(1, 3)); } }public class RunQuiz { public static void main(String[] args) {} Question q = new Question(); }这样写为什么不弹出窗口
你的代码有两个问题:
1. `main` 方法为空,而且没有调用 `Question` 类的构造函数。所以当你运行这个程序时,它什么也不会做。你需要在 `main` 方法中调用 `Question` 类的构造函数。
2. 在 `Question` 类的构造函数中,你需要将窗口设置为可见的,但是你的代码中没有设置。你需要在构造函数中添加 `this.setVisible(true);` 来让窗口显示出来。
以下是修改后的代码:
```
public class Question extends JFrame {
public Question() {
super.setTitle("Quiz Game");
super.setSize(100, 150);
super.setLocation(300, 200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
this.add(panel);
JTextField textField = new JTextField();
panel.add(textField, new GridLayout(1, 3));
this.setVisible(true);
}
}
public class RunQuiz {
public static void main(String[] args) {
Question q = new Question();
}
}
```
现在运行 `RunQuiz` 类,你应该能够看到弹出的窗口。
阅读全文