Java JButton在调整大小时以一种奇怪的方式复制自己

Java JButton在调整大小时以一种奇怪的方式复制自己,java,swing,jframe,jbutton,paintcomponent,Java,Swing,Jframe,Jbutton,Paintcomponent,所以,我正在为一个班级做一个多人乒乓球游戏,当我试着做一个按钮,然后调整窗口大小时,我遇到了这个奇怪的问题 缩短代码: private void init(String title) { JFrame frame = new JFrame(title); frame.setMinimumSize(new Dimension(480, 480)); frame.setSize(600, 600); frame.add(this); frame.setDefa

所以,我正在为一个班级做一个多人乒乓球游戏,当我试着做一个按钮,然后调整窗口大小时,我遇到了这个奇怪的问题

缩短代码:

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);
}
private void init(字符串标题){
JFrame=新JFrame(标题);
框架设置最小尺寸(新尺寸(480480));
框架。设置尺寸(600600);
框架。添加(此);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setFocusable(真);
此参数为.setFocusable(true);
这个.背景(颜色.白色);
定时器=新定时器(36,new TimeAction());
timer.start();
frame.addKeyListener(这个);
}
public void addRestartButton(){
JButton btnRestartGame=新JButton(“重新启动游戏”);
btnRestartGame.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
pong.initServer();
pong.nowReady();
w、 setVisible(假);
}
});
btnRestartGame.setBounds(getWidth()/2-100,getHeight()*3/4200,25);
添加(BTN开始游戏);
}
公共组件(java.awt.g){
超级组件(g);
Image bufferImage=createImage(this.getSize().width,this.getSize().height);
Graphics2D bufferGraphics=(Graphics2D)bufferImage.getGraphics();
bufferGraphics.setRenderingHint(RenderingHits.KEY\u抗锯齿,RenderingHits.VALUE\u抗锯齿\u开启);
g2=(图形2D)缓冲图形;
paintPolygon();
彩弹();
对于(int i=0;i
有关问题的图片:


有人知道为什么会发生这种情况以及我如何修复它吗?

在您的
paintComponent
方法中删除这两行

// if (comm instanceof Server)
//   addRestartButton();

+1是的,绘制方法仅用于绘制,因为每当Swing确定需要重新绘制组件时,都会调用这些方法。你不应该在这个方法中创建一个组件。这些行在我检查游戏是否结束的代码部分。是的,从技术上讲,它确实解决了我的问题,但它也完全删除了按钮。@KuroMajutsu你没有检查游戏是否先结束。你只是一直在添加按钮。