所以,我正在为一个类进行多人Pong游戏,当我尝试制作JButton然后调整窗口大小时,我遇到了这个奇怪的问题。
缩短代码:
private void init(String title) {
JFrame frame = new JFrame(title);
frame.setMinimumSize(new Dimension(480, 480));
frame.setSize(600, 600);
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setFocusable(true);
this.setFocusable(true);
this.setBackground(Color.WHITE);
Timer timer = new Timer(36, new TimeAction());
timer.start();
frame.addKeyListener(this);
}
public void addRestartButton() {
JButton btnRestartGame = new JButton("Restart Game");
btnRestartGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
pong.initServer();
pong.nowReady();
w.setVisible(false);
}
});
btnRestartGame.setBounds(getWidth() / 2 - 100, getHeight() * 3 / 4, 200, 25);
add(btnRestartGame);
}
public void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
Image bufferImage = createImage(this.getSize().width, this.getSize().height);
Graphics2D bufferGraphics = (Graphics2D) bufferImage.getGraphics();
bufferGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2 = (Graphics2D) bufferGraphics;
paintPolygon();
paintBall();
for (int i = 0; i < Pong.getPolygon().npoints; i++)
paintSide(Pong.getPolygon().getSide(i));
g2.setTransform(unrotate);
paintScoreField();
if (!pong.getScore().isPlaying()) {
paintWinnerName(pong.getScore().getWinner());
if (comm instanceof Server)
addRestartButton();
}
g.drawImage(bufferImage, 0, 0, this);
}
有问题的图片: http://imgur.com/a/rHxOa
有谁知道为什么会发生这种情况以及如何解决这个问题?
答案 0 :(得分:3)
在paintComponent
方法中删除这两行
// if (comm instanceof Server)
// addRestartButton();