我正在写一个蛇游戏,但有一个小问题。有时面板和框架之间有一点间隙。我真的不知道可能是什么问题,因为它看起来如此不规则。
SSCCE:
public class Game {
static JFrame frame = new JFrame("The Snake Game");
static Game game = new Game();
JPanel cards;
static JPanel card1 = new JPanel();
private Game() {
}
public void addComponentToPane(Container pane) {
// CARD 1
card1.setLayout(null);
card1.setPreferredSize(new Dimension(600, 625));
CardLayout cl = new CardLayout();
cards = new JPanel(cl);
cards.add(card1);
pane.add(cards, BorderLayout.CENTER);
}
private static void createAndShowGUI() {
game.addComponentToPane(frame.getContentPane());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
} // end of main
}