java 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
有人知道为什么会发生这种情况,以及我如何解决它吗
# 1 楼答案
在
paintComponent
方法中,删除这两行