在JFrame测试程序中,我意识到我不能这样说:
setSize(450, 300);
setResizable(false);
setVisible(true);
在构造函数的顶部,否则它不能正常工作。但是,如果我把它放在底部就好了:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class GameWindow extends JFrame {
private GameGraphics gameGraphics;
public GameWindow() {
super("Fellice");
gameGraphics = new GameGraphics();
gameGraphics.setBackground(Color.BLACK);
add(gameGraphics, BorderLayout.CENTER);
setSize(450, 300);
setResizable(false);
setVisible(true);
}
}
如果我在调用超类之后将它放入,它不起作用,但为什么?我试图把这些:
setSize(450, 300);
setResizable(false);
setVisible(true);
在顶部逐个查看导致问题的原因,它似乎是一个这样的组合:
setSize(450, 300);
setVisible(true);
这在顶部不起作用。但是,在我的其他程序中,它的工作完全正常:
@SuppressWarnings("serial")
public class Client extends JFrame {
private JTextField messageField;
private JTextArea chatWindow;
public Client(String host) {
super("Messenger");
setSize(300, 400);
setVisible(true);
chatWindow = new JTextArea();
chatWindow.setEditable(false);
messageField = new JTextField();
// .. actions etc.
add(new JScrollPane(chatWindow), BorderLayout.CENTER);
add(messageField, BorderLayout.SOUTH);
}
*编辑
工作代码:
@SuppressWarnings("serial")
public class GameWindow extends JFrame {
private GameGraphics gameGraphics;
public GameWindow() {
super("Fellice");
gameGraphics = new GameGraphics();
gameGraphics.setBackground(Color.BLACK);
add(gameGraphics, BorderLayout.CENTER);
setSize(450, 300);
setResizable(false);
setVisible(true);
}
}
Glitched代码(屏幕显示为空白):
@SuppressWarnings("serial")
public class GameWindow extends JFrame {
private GameGraphics gameGraphics;
public GameWindow() {
super("Fellice");
setSize(450, 300);
setResizable(false);
setVisible(true);
gameGraphics = new GameGraphics();
gameGraphics.setBackground(Color.BLACK);
add(gameGraphics, BorderLayout.CENTER);
}
}