JPanel 未在 JFrame 中显示,但 JFrame 仍会更改大小
JPanel not showing in JFrame, but JFrame still changes size
我不知道我做了什么,或者出了什么问题,但我在最近某个时候所做的更改使我的 JPanel 完全不可见。它嵌套的 JFrame 仍然会改变大小以容纳它,我仍然可以切换组合框中的内容。
无奈之下,我尝试用一个按钮替换 SnakeSettingsPanel class 的内容,但同样的事情发生了——完全不可见,但我仍然可以与之交互。我认为这可能是计算机错误,所以我尝试重新启动,但仍然没有。当我尝试向 JPanel 外部的框架添加一个按钮时,它工作得很好。我做错了什么?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SnakeSettingsPanel extends JPanel {
public boolean quit = false;
public boolean play = false;
public int width = 20;
public int height = 15;
public Speed speed = Speed.SLOW;
public JTextField wField;
public JTextField hField;
public JComboBox<Speed> sField;
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setTitle("Snake");
SnakeSettingsPanel settings = new SnakeSettingsPanel();
jf.add(settings);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public SnakeSettingsPanel() {
setLayout(new GridBagLayout());
// @author Create our labels.
JLabel wLabel = new JLabel("Width:");
JLabel hLabel = new JLabel("Height:");
JLabel sLabel = new JLabel("Speed:");
GridBagConstraints p = new GridBagConstraints();
p.gridx = 0;
p.gridy = 0;
p.insets = new Insets(10, 10, 10, 10);
// @author Create the buttons, and add listeners
JButton y = new JButton("Play");
JButton n = new JButton("Quit");
y.addActionListener(new PlayListener());
n.addActionListener(new QuitListener());
// @author Create text fields for height/width
wField = new JTextField(15);
wField.setText("20");
hField = new JTextField(15);
hField.setText("15");
// @author Creates a combobox for selecting speed.
Speed[] speeds = {Speed.SLOW, Speed.MEDIUM, Speed.FAST};
sField = new JComboBox<Speed>(speeds);
// @author Stitch everything into the panel.
add(wLabel, p);
p.gridx = 1;
add(wField, p);
p.gridx = 0;
p.gridy = 1;
add(hLabel, p);
p.gridx = 1;
add(hField, p);
p.gridx = 0;
p.gridy = 2;
add(sLabel, p);
p.gridx = 1;
add(sField, p);
p.gridx = 0;
p.gridy = 3;
add(y, p);
p.gridx = 1;
add(n, p);
setVisible(true);
}
public boolean getPlay() {
return play;
}
public boolean getQuit() {
return quit;
}
// @author Returns all settings as a SnakeSettings object
public SnakeSettings getSettings() {
return new SnakeSettings(width, height, speed);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Speed getSpeed() {
return speed;
}
// @author Sends out the word to start a new game.
public class PlayListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
quit = false;
play = true;
width = Integer.parseInt(wField.getText());
height = Integer.parseInt(hField.getText());
speed = (Speed) sField.getSelectedItem();
}
}
// @author Sends out the word to shut down the program.
public class QuitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
quit = true;
play = false;
}
}
}
让这成为一个教训,说明为什么您应该避免将模型(您的应用程序的数据)与视图(它的显示方式)混合。您的 SnakeSettingsPanel
目前是两者。
- 作为模型,它包含3个重要字段:
width
、height
和speed
。
- 作为View,它是一个完整的JPanel。 JPanel 有 lot 个字段,您应该避免直接接触这些字段。包括
width
和 height
,通常通过 getHeight
和 getWidth
访问——您正在用始终 return 相同的版本覆盖 built-in 值 20 和 15(直到用户通过他们看不到的 UI 更改这些值)。
fast 修复是重命名您当前的 getWidth()
和 getHeight()
以避免与 built-in getWidth()
和getHeight()
parent JPanel class 的方法。称它们为 getMyWidth()
、getMyHeight()
,突然一切正常。
更好 修复是完全删除这些字段和方法,并将您自己的模型属性存储在 SnakeSettings
属性中。当用户点击播放时更新它,并在通过 getSettings()
请求时 return 它。更少的代码,更少的意外名称与您的 parent JPanel
class 冲突的可能性。这看起来像:
// SnakeSettingsPanel, before
public int width = 20;
public int height = 15;
public Speed speed = Speed.SLOW;
public int getWidth() { // <-- clashes with superclass
return width;
}
public int getHeight() { // <-- clashes with superclass
return height;
}
public Speed getSpeed() {
return speed;
}
public SnakeSettings getSettings() {
return new SnakeSettings(width, height, speed);
}
// SnakeSettingsPanel, after
SnakeSettings settings = new SnakeSettings();
public SnakeSettings getSettings() {
return settings;
}
我不知道我做了什么,或者出了什么问题,但我在最近某个时候所做的更改使我的 JPanel 完全不可见。它嵌套的 JFrame 仍然会改变大小以容纳它,我仍然可以切换组合框中的内容。
无奈之下,我尝试用一个按钮替换 SnakeSettingsPanel class 的内容,但同样的事情发生了——完全不可见,但我仍然可以与之交互。我认为这可能是计算机错误,所以我尝试重新启动,但仍然没有。当我尝试向 JPanel 外部的框架添加一个按钮时,它工作得很好。我做错了什么?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SnakeSettingsPanel extends JPanel {
public boolean quit = false;
public boolean play = false;
public int width = 20;
public int height = 15;
public Speed speed = Speed.SLOW;
public JTextField wField;
public JTextField hField;
public JComboBox<Speed> sField;
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setTitle("Snake");
SnakeSettingsPanel settings = new SnakeSettingsPanel();
jf.add(settings);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public SnakeSettingsPanel() {
setLayout(new GridBagLayout());
// @author Create our labels.
JLabel wLabel = new JLabel("Width:");
JLabel hLabel = new JLabel("Height:");
JLabel sLabel = new JLabel("Speed:");
GridBagConstraints p = new GridBagConstraints();
p.gridx = 0;
p.gridy = 0;
p.insets = new Insets(10, 10, 10, 10);
// @author Create the buttons, and add listeners
JButton y = new JButton("Play");
JButton n = new JButton("Quit");
y.addActionListener(new PlayListener());
n.addActionListener(new QuitListener());
// @author Create text fields for height/width
wField = new JTextField(15);
wField.setText("20");
hField = new JTextField(15);
hField.setText("15");
// @author Creates a combobox for selecting speed.
Speed[] speeds = {Speed.SLOW, Speed.MEDIUM, Speed.FAST};
sField = new JComboBox<Speed>(speeds);
// @author Stitch everything into the panel.
add(wLabel, p);
p.gridx = 1;
add(wField, p);
p.gridx = 0;
p.gridy = 1;
add(hLabel, p);
p.gridx = 1;
add(hField, p);
p.gridx = 0;
p.gridy = 2;
add(sLabel, p);
p.gridx = 1;
add(sField, p);
p.gridx = 0;
p.gridy = 3;
add(y, p);
p.gridx = 1;
add(n, p);
setVisible(true);
}
public boolean getPlay() {
return play;
}
public boolean getQuit() {
return quit;
}
// @author Returns all settings as a SnakeSettings object
public SnakeSettings getSettings() {
return new SnakeSettings(width, height, speed);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Speed getSpeed() {
return speed;
}
// @author Sends out the word to start a new game.
public class PlayListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
quit = false;
play = true;
width = Integer.parseInt(wField.getText());
height = Integer.parseInt(hField.getText());
speed = (Speed) sField.getSelectedItem();
}
}
// @author Sends out the word to shut down the program.
public class QuitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
quit = true;
play = false;
}
}
}
让这成为一个教训,说明为什么您应该避免将模型(您的应用程序的数据)与视图(它的显示方式)混合。您的 SnakeSettingsPanel
目前是两者。
- 作为模型,它包含3个重要字段:
width
、height
和speed
。 - 作为View,它是一个完整的JPanel。 JPanel 有 lot 个字段,您应该避免直接接触这些字段。包括
width
和height
,通常通过getHeight
和getWidth
访问——您正在用始终 return 相同的版本覆盖 built-in 值 20 和 15(直到用户通过他们看不到的 UI 更改这些值)。
fast 修复是重命名您当前的 getWidth()
和 getHeight()
以避免与 built-in getWidth()
和getHeight()
parent JPanel class 的方法。称它们为 getMyWidth()
、getMyHeight()
,突然一切正常。
更好 修复是完全删除这些字段和方法,并将您自己的模型属性存储在 SnakeSettings
属性中。当用户点击播放时更新它,并在通过 getSettings()
请求时 return 它。更少的代码,更少的意外名称与您的 parent JPanel
class 冲突的可能性。这看起来像:
// SnakeSettingsPanel, before
public int width = 20;
public int height = 15;
public Speed speed = Speed.SLOW;
public int getWidth() { // <-- clashes with superclass
return width;
}
public int getHeight() { // <-- clashes with superclass
return height;
}
public Speed getSpeed() {
return speed;
}
public SnakeSettings getSettings() {
return new SnakeSettings(width, height, speed);
}
// SnakeSettingsPanel, after
SnakeSettings settings = new SnakeSettings();
public SnakeSettings getSettings() {
return settings;
}