【问题标题】:Java Swing Drawn Shapes DisappearingJava Swing 绘制的形状消失了
【发布时间】:2016-05-30 13:41:52
【问题描述】:

我正在使用 Swing 绘制图形在 Java 中制作 Connect 4 游戏。 我遇到的问题是,如果我单击指定列下的按钮以向该列添加另一个图块,则它下面的图块会消失。此外,颜色从红色变为黄色,但随后保持黄色而不是变回红色(假设每次奇数点击为红色,直到 41,每次偶数点击为黄色,直到 42)。

这是代码片段:

    import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

public class Connect_4 extends JFrame {

private JPanel contentPane;
public int countClicks = 0;
public boolean check = false;
public boolean check2 = false;
public boolean flag1;
public boolean flag2;
public boolean flag3;
public boolean flag4;
public boolean flag5;
public boolean flag6;
public boolean flag7;
public int btn1Count = 0;
public int btn2Count = 0;
public int btn3Count = 0;
public int btn4Count = 0;
public int btn5Count = 0;
public int btn6Count = 0;
public int btn7Count = 0;
public boolean flag;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Connect_4 frame = new Connect_4();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public void paint(Graphics g) {
    super.paint(g);

    g.setColor(Color.red);
    Font font = new Font("Serif", Font.PLAIN, 20);
    g.setFont(font);
    g.drawString("C O N N E C T  4", 34, 47);

    g.setColor(Color.blue);
    g.fillRect(34, 50, 140, 120);

    g.setColor(Color.black);
    // ~~~~row 1~~~~
    g.drawRect(34, 50, 20, 20);
    g.drawRect(54, 50, 20, 20);
    g.drawRect(74, 50, 20, 20);
    g.drawRect(94, 50, 20, 20);
    g.drawRect(114, 50, 20, 20);
    g.drawRect(134, 50, 20, 20);
    g.drawRect(154, 50, 20, 20);
    g.setColor(Color.white);
    g.fillOval(36, 52, 16, 16);
    g.fillOval(56, 52, 16, 16);
    g.fillOval(76, 52, 16, 16);
    g.fillOval(96, 52, 16, 16);
    g.fillOval(116, 52, 16, 16);
    g.fillOval(136, 52, 16, 16);
    g.fillOval(156, 52, 16, 16);
    g.setColor(Color.black);
    // ~~~~row 2~~~~
    g.drawRect(34, 70, 20, 20);
    g.drawRect(54, 70, 20, 20);
    g.drawRect(74, 70, 20, 20);
    g.drawRect(94, 70, 20, 20);
    g.drawRect(114, 70, 20, 20);
    g.drawRect(134, 70, 20, 20);
    g.drawRect(154, 70, 20, 20);
    g.setColor(Color.white);
    g.fillOval(36, 72, 16, 16);
    g.fillOval(56, 72, 16, 16);
    g.fillOval(76, 72, 16, 16);
    g.fillOval(96, 72, 16, 16);
    g.fillOval(116, 72, 16, 16);
    g.fillOval(136, 72, 16, 16);
    g.fillOval(156, 72, 16, 16);
    g.setColor(Color.black);
    // ~~~~row 3~~~~
    g.drawRect(34, 90, 20, 20);
    g.drawRect(54, 90, 20, 20);
    g.drawRect(74, 90, 20, 20);
    g.drawRect(94, 90, 20, 20);
    g.drawRect(114, 90, 20, 20);
    g.drawRect(134, 90, 20, 20);
    g.drawRect(154, 90, 20, 20);
    g.setColor(Color.white);
    g.fillOval(36, 92, 16, 16);
    g.fillOval(56, 92, 16, 16);
    g.fillOval(76, 92, 16, 16);
    g.fillOval(96, 92, 16, 16);
    g.fillOval(116, 92, 16, 16);
    g.fillOval(136, 92, 16, 16);
    g.fillOval(156, 92, 16, 16);
    g.setColor(Color.black);
    // ~~~~row 4~~~~
    g.drawRect(34, 110, 20, 20);
    g.drawRect(54, 110, 20, 20);
    g.drawRect(74, 110, 20, 20);
    g.drawRect(94, 110, 20, 20);
    g.drawRect(114, 110, 20, 20);
    g.drawRect(134, 110, 20, 20);
    g.drawRect(154, 110, 20, 20);
    g.setColor(Color.white);
    g.fillOval(36, 112, 16, 16);
    g.fillOval(56, 112, 16, 16);
    g.fillOval(76, 112, 16, 16);
    g.fillOval(96, 112, 16, 16);
    g.fillOval(116, 112, 16, 16);
    g.fillOval(136, 112, 16, 16);
    g.fillOval(156, 112, 16, 16);
    g.setColor(Color.black);
    // ~~~~row 5~~~~
    g.drawRect(34, 130, 20, 20);
    g.drawRect(54, 130, 20, 20);
    g.drawRect(74, 130, 20, 20);
    g.drawRect(94, 130, 20, 20);
    g.drawRect(114, 130, 20, 20);
    g.drawRect(134, 130, 20, 20);
    g.drawRect(154, 130, 20, 20);
    g.setColor(Color.white);
    g.fillOval(36, 132, 16, 16);
    g.fillOval(56, 132, 16, 16);
    g.fillOval(76, 132, 16, 16);
    g.fillOval(96, 132, 16, 16);
    g.fillOval(116, 132, 16, 16);
    g.fillOval(136, 132, 16, 16);
    g.fillOval(156, 132, 16, 16);
    g.setColor(Color.black);
    // ~~~~row 6~~~~
    g.drawRect(34, 150, 20, 20);
    g.drawRect(54, 150, 20, 20);
    g.drawRect(74, 150, 20, 20);
    g.drawRect(94, 150, 20, 20);
    g.drawRect(114, 150, 20, 20);
    g.drawRect(134, 150, 20, 20);
    g.drawRect(154, 150, 20, 20);
    g.setColor(Color.white);
    g.fillOval(36, 152, 16, 16);
    g.fillOval(56, 152, 16, 16);
    g.fillOval(76, 152, 16, 16);
    g.fillOval(96, 152, 16, 16);
    g.fillOval(116, 152, 16, 16);
    g.fillOval(136, 152, 16, 16);
    g.fillOval(156, 152, 16, 16);
    g.setColor(Color.black);
    g.drawLine(174, 170, 174, 187);
    g.drawLine(34, 170, 34, 187);
    g.drawLine(164, 187, 184, 187);
    g.drawLine(24, 187, 44, 187);



    if (flag1 == true && check == true && btn1Count == 1)
    {
    g.setColor(Color.red);
    g.fillOval(36, 152, 16, 16);
    }
    if (flag1 == true && check2 == true && btn1Count == 1)
    {
    g.setColor(Color.yellow);
    g.fillOval(36, 152, 16, 16);
    }

    if (flag1 == true && check == true && btn1Count == 2)
    {
    g.setColor(Color.red);
    g.fillOval(36, 132, 16, 16);
    }
    if (flag1 == true && check2 == true && btn1Count == 2)
    {
    g.setColor(Color.yellow);
    g.fillOval(36, 132, 16, 16);
    }

    if (flag1 == true && check == true && btn1Count == 3)
    {
    g.setColor(Color.red);
    g.fillOval(36, 112, 16, 16);
    }
    if (flag1 == true && check2 == true && btn1Count == 3)
    {
    g.setColor(Color.yellow);
    g.fillOval(36, 112, 16, 16);
    }

    if (flag1 == true && check == true && btn1Count == 4)
    {
    g.setColor(Color.red);
    g.fillOval(36, 92, 16, 16);
    }
    if (flag1 == true && check2 == true && btn1Count == 4)
    {
    g.setColor(Color.yellow);
    g.fillOval(36, 92, 16, 16);
    }

    if (flag1 == true && check == true && btn1Count == 5)
    {
    g.setColor(Color.red);
    g.fillOval(36, 72, 16, 16);
    }
    if (flag1 == true && check2 == true && btn1Count == 5)
    {
    g.setColor(Color.yellow);
    g.fillOval(36, 72, 16, 16);
    }

    if (flag1 == true && check == true && btn1Count == 6)
    {
    g.setColor(Color.red);
    g.fillOval(36, 52, 16, 16);
    }
    if (flag1 == true && check2 == true && btn1Count == 6)
    {
    g.setColor(Color.yellow);
    g.fillOval(36, 52, 16, 16);
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    if (flag2 == true && check == true && btn2Count == 1)
    {
    g.setColor(Color.red);
    g.fillOval(56, 152, 16, 16);
    }
    if (flag2 == true && check2 == true && btn2Count == 1)
    {
    g.setColor(Color.yellow);
    g.fillOval(56, 152, 16, 16);
    }

    if (flag2 == true && check == true && btn2Count == 2)
    {
    g.setColor(Color.red);
    g.fillOval(56, 132, 16, 16);
    }
    if (flag2 == true && check2 == true && btn2Count == 2)
    {
    g.setColor(Color.yellow);
    g.fillOval(56, 132, 16, 16);
    }

    if (flag2 == true && check == true && btn2Count == 3)
    {
    g.setColor(Color.red);
    g.fillOval(56, 112, 16, 16);
    }
    if (flag2 == true && check2 == true && btn2Count == 3)
    {
    g.setColor(Color.yellow);
    g.fillOval(56, 112, 16, 16);
    }

    if (flag2 == true && check == true && btn2Count == 4)
    {
    g.setColor(Color.red);
    g.fillOval(56, 92, 16, 16);
    }
    if (flag2 == true && check2 == true && btn2Count == 4)
    {
    g.setColor(Color.yellow);
    g.fillOval(56, 92, 16, 16);
    }

    if (flag2 == true && check == true && btn2Count == 5)
    {
    g.setColor(Color.red);
    g.fillOval(56, 72, 16, 16);
    }
    if (flag2 == true && check2 == true && btn2Count == 5)
    {
    g.setColor(Color.yellow);
    g.fillOval(56, 72, 16, 16);
    }

    if (flag2 == true && check == true && btn2Count == 6)
    {
    g.setColor(Color.red);
    g.fillOval(56, 52, 16, 16);
    }
    if (flag2 == true && check2 == true && btn2Count == 6)
    {
    g.setColor(Color.yellow);
    g.fillOval(56, 52, 16, 16);
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    if (flag3 == true && check == true && btn3Count == 1)
    {
    g.setColor(Color.red);
    g.fillOval(76, 152, 16, 16);
    }
    if (flag3 == true && check2 == true && btn3Count == 1)
    {
    g.setColor(Color.yellow);
    g.fillOval(76, 152, 16, 16);
    }

    if (flag3 == true && check == true && btn3Count == 2)
    {
    g.setColor(Color.red);
    g.fillOval(76, 132, 16, 16);
    }
    if (flag3 == true && check2 == true && btn3Count == 2)
    {
    g.setColor(Color.yellow);
    g.fillOval(76, 132, 16, 16);
    }

    if (flag3 == true && check == true && btn3Count == 3)
    {
    g.setColor(Color.red);
    g.fillOval(76, 112, 16, 16);
    }
    if (flag3 == true && check2 == true && btn3Count == 3)
    {
    g.setColor(Color.yellow);
    g.fillOval(76, 112, 16, 16);
    }

    if (flag3 == true && check == true && btn3Count == 4)
    {
    g.setColor(Color.red);
    g.fillOval(76, 92, 16, 16);
    }
    if (flag3 == true && check2 == true && btn3Count == 4)
    {
    g.setColor(Color.yellow);
    g.fillOval(76, 92, 16, 16);
    }

    if (flag3 == true && check == true && btn3Count == 5)
    {
    g.setColor(Color.red);
    g.fillOval(76, 72, 16, 16);
    }
    if (flag3 == true && check2 == true && btn3Count == 5)
    {
    g.setColor(Color.yellow);
    g.fillOval(76, 72, 16, 16);
    }

    if (flag3 == true && check == true && btn3Count == 6)
    {
    g.setColor(Color.red);
    g.fillOval(76, 52, 16, 16);
    }
    if (flag3 == true && check2 == true && btn3Count == 6)
    {
    g.setColor(Color.yellow);
    g.fillOval(76, 52, 16, 16);
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    if (flag4 == true && check == true && btn4Count == 1)
    {
    g.setColor(Color.red);
    g.fillOval(96, 152, 16, 16);
    }
    if (flag4 == true && check2 == true && btn4Count == 1)
    {
    g.setColor(Color.yellow);
    g.fillOval(96, 152, 16, 16);
    }

    if (flag4 == true && check == true && btn4Count == 2)
    {
    g.setColor(Color.red);
    g.fillOval(96, 132, 16, 16);
    }
    if (flag4 == true && check2 == true && btn4Count == 2)
    {
    g.setColor(Color.yellow);
    g.fillOval(96, 132, 16, 16);
    }

    if (flag4 == true && check == true && btn4Count == 3)
    {
    g.setColor(Color.red);
    g.fillOval(96, 112, 16, 16);
    }
    if (flag4 == true && check2 == true && btn4Count == 3)
    {
    g.setColor(Color.yellow);
    g.fillOval(96, 112, 16, 16);
    }

    if (flag4 == true && check == true && btn4Count == 4)
    {
    g.setColor(Color.red);
    g.fillOval(96, 92, 16, 16);
    }
    if (flag4 == true && check2 == true && btn4Count == 4)
    {
    g.setColor(Color.yellow);
    g.fillOval(96, 92, 16, 16);
    }

    if (flag4 == true && check == true && btn4Count == 5)
    {
    g.setColor(Color.red);
    g.fillOval(96, 72, 16, 16);
    }
    if (flag4 == true && check2 == true && btn4Count == 5)
    {
    g.setColor(Color.yellow);
    g.fillOval(96, 72, 16, 16);
    }

    if (flag4 == true && check == true && btn4Count == 6)
    {
    g.setColor(Color.red);
    g.fillOval(96, 52, 16, 16);
    }
    if (flag4 == true && check2 == true && btn4Count == 6)
    {
    g.setColor(Color.yellow);
    g.fillOval(96, 52, 16, 16);
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    if (flag5 == true && check == true && btn5Count == 1)
    {
    g.setColor(Color.red);
    g.fillOval(116, 152, 16, 16);
    }
    if (flag5 == true && check2 == true && btn5Count == 1)
    {
    g.setColor(Color.yellow);
    g.fillOval(116, 152, 16, 16);
    }

    if (flag5 == true && check == true && btn5Count == 2)
    {
    g.setColor(Color.red);
    g.fillOval(116, 132, 16, 16);
    }
    if (flag5 == true && check2 == true && btn5Count == 2)
    {
    g.setColor(Color.yellow);
    g.fillOval(116, 132, 16, 16);
    }

    if (flag5 == true && check == true && btn5Count == 3)
    {
    g.setColor(Color.red);
    g.fillOval(116, 112, 16, 16);
    }
    if (flag5 == true && check2 == true && btn5Count == 3)
    {
    g.setColor(Color.yellow);
    g.fillOval(116, 112, 16, 16);
    }

    if (flag5 == true && check == true && btn5Count == 4)
    {
    g.setColor(Color.red);
    g.fillOval(116, 92, 16, 16);
    }
    if (flag5 == true && check2 == true && btn5Count == 4)
    {
    g.setColor(Color.yellow);
    g.fillOval(116, 92, 16, 16);
    }

    if (flag5 == true && check == true && btn5Count == 5)
    {
    g.setColor(Color.red);
    g.fillOval(116, 72, 16, 16);
    }
    if (flag5 == true && check2 == true && btn5Count == 5)
    {
    g.setColor(Color.yellow);
    g.fillOval(116, 72, 16, 16);
    }

    if (flag5 == true && check == true && btn5Count == 6)
    {
    g.setColor(Color.red);
    g.fillOval(116, 52, 16, 16);
    }
    if (flag5 == true && check2 == true && btn5Count == 6)
    {
    g.setColor(Color.yellow);
    g.fillOval(116, 52, 16, 16);
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    if (flag6 == true && check == true && btn6Count == 1)
    {
    g.setColor(Color.red);
    g.fillOval(136, 152, 16, 16);
    }
    if (flag6 == true && check2 == true && btn6Count == 1)
    {
    g.setColor(Color.yellow);
    g.fillOval(136, 152, 16, 16);
    }

    if (flag6 == true && check == true && btn6Count == 2)
    {
    g.setColor(Color.red);
    g.fillOval(136, 132, 16, 16);
    }
    if (flag6 == true && check2 == true && btn6Count == 2)
    {
    g.setColor(Color.yellow);
    g.fillOval(136, 132, 16, 16);
    }

    if (flag6 == true && check == true && btn6Count == 3)
    {
    g.setColor(Color.red);
    g.fillOval(136, 112, 16, 16);
    }
    if (flag6 == true && check2 == true && btn6Count == 3)
    {
    g.setColor(Color.yellow);
    g.fillOval(136, 112, 16, 16);
    }

    if (flag6 == true && check == true && btn6Count == 4)
    {
    g.setColor(Color.red);
    g.fillOval(136, 92, 16, 16);
    }
    if (flag6 == true && check2 == true && btn6Count == 4)
    {
    g.setColor(Color.yellow);
    g.fillOval(136, 92, 16, 16);
    }

    if (flag6 == true && check == true && btn6Count == 5)
    {
    g.setColor(Color.red);
    g.fillOval(136, 72, 16, 16);
    }
    if (flag6 == true && check2 == true && btn6Count == 5)
    {
    g.setColor(Color.yellow);
    g.fillOval(136, 72, 16, 16);
    }

    if (flag6 == true && check == true && btn6Count == 6)
    {
    g.setColor(Color.red);
    g.fillOval(136, 52, 16, 16);
    }
    if (flag6 == true && check2 == true && btn6Count == 6)
    {
    g.setColor(Color.yellow);
    g.fillOval(136, 52, 16, 16);
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    if (flag7 == true && check == true && btn7Count == 1)
    {
    g.setColor(Color.red);
    g.fillOval(156, 152, 16, 16);
    }
    if (flag7 == true && check2 == true && btn7Count == 1)
    {
    g.setColor(Color.yellow);
    g.fillOval(156, 152, 16, 16);
    }

    if (flag7 == true && check == true && btn7Count == 2)
    {
    g.setColor(Color.red);
    g.fillOval(156, 132, 16, 16);
    }
    if (flag7 == true && check2 == true && btn7Count == 2)
    {
    g.setColor(Color.yellow);
    g.fillOval(156, 132, 16, 16);
    }

    if (flag7 == true && check == true && btn7Count == 3)
    {
    g.setColor(Color.red);
    g.fillOval(156, 112, 16, 16);
    }
    if (flag7 == true && check2 == true && btn7Count == 3)
    {
    g.setColor(Color.yellow);
    g.fillOval(156, 112, 16, 16);
    }

    if (flag7 == true && check == true && btn7Count == 4)
    {
    g.setColor(Color.red);
    g.fillOval(156, 92, 16, 16);
    }
    if (flag7 == true && check2 == true && btn7Count == 4)
    {
    g.setColor(Color.yellow);
    g.fillOval(156, 92, 16, 16);
    }

    if (flag7 == true && check == true && btn7Count == 5)
    {
    g.setColor(Color.red);
    g.fillOval(156, 72, 16, 16);
    }
    if (flag7 == true && check2 == true && btn7Count == 5)
    {
    g.setColor(Color.yellow);
    g.fillOval(156, 72, 16, 16);
    }

    if (flag7 == true && check == true && btn7Count == 6)
    {
    g.setColor(Color.red);
    g.fillOval(156, 52, 16, 16);
    }
    if (flag7 == true && check2 == true && btn7Count == 6)
    {
    g.setColor(Color.yellow);
    g.fillOval(156, 52, 16, 16);
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

}

public Connect_4() {
    setTitle("Connect 4");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 210, 220);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    // getContentPane().setBackground(Color.RED);
    setResizable(false);
    contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 137));

    JPanel panel = new JPanel();
    // panel.setBackground(Color.BLUE);
    contentPane.add(panel, BorderLayout.CENTER);

    JButton btn1 = new JButton();
    btn1.setPreferredSize(new Dimension(15, 10));
    panel.add(btn1);
    btn1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            countClicks++;
            btn1Count++;
            flag = true;
            flag1 = true;
            switch (countClicks) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 9:
            case 11:
            case 13:
            case 15:
            case 17:
            case 19:
            case 21:
            case 23:
            case 25:
            case 27:
            case 29:
            case 31:
            case 33:
            case 35:
            case 37:
            case 39:
            case 41:
                check = true;
                repaint();
                break;
            case 2:
            case 4:
            case 6:
            case 8:
            case 10:
            case 12:
            case 14:
            case 16:
            case 18:
            case 20:
            case 22:
            case 24:
            case 26:
            case 28:
            case 30:
            case 32:
            case 34:
            case 36:
            case 38:
            case 40:
            case 42:
                check2 = true;
                repaint();
                break;

            }
        }

    });

我不知道为什么列中的前一个图块消失了,也不知道如何让它停止。问题是重绘()吗?是paint方法中的if语句吗?请帮忙。

【问题讨论】:

  • repaint() 不是你应该调用的函数
  • 我应该改用什么?

标签: java swing graphics paint


【解决方案1】:

Paint() - 此方法包含绘制此组件的指令。实际上,在 Swing 中,您应该更改paintComponent() 而不是paint(),因为paint 调用paintBorder()paintComponent()paintChildren()。你不应该直接调用这个方法,而应该调用 repaint()。

repaint() - 这个方法不能被覆盖。它控制update() -> paint() 循环。您应该调用此方法来让组件重新绘制自身。如果您对组件的外观做了任何更改,但不是它的大小(例如更改颜色、动画等),然后调用此方法。

validate() - 这告诉组件重新布置自己并重新绘制自己。如果您已更改组件或其任何子组件的大小(添加、删除、调整子组件大小),您应该调用此方法...我认为在 Swing 中调用 revalidate() 优于调用 validate() ,不过……

update() - 该方法负责清除组件并调用paint()。同样,您应该调用 repaint() 而不是直接调用此方法...如果您需要在动画中进行快速更新,您应该重写此方法以仅调用 paint() 方法...

updateUI() - 如果您在组件可见后更改了组件的可插入外观,请调用此方法。

注意:您在程序中使用 switch case 的方式不是一个好的实现,使用变量(计数器)并随着用户点击而增加,然后使用 if/while 条件进行进一步的实现。

【讨论】:

  • 我讨厌挑剔,但我怎样才能在我的代码中实现其中一个,以便在另一个下面绘制的圆圈不会消失?
  • 因为我正在使用 repaint(),我觉得这是问题所在,因为我不想在另一个上面画一个圆圈而不是下面那个,我想画一个所以它添加它。
  • 如果对您有帮助,我已经为您准备了这个stackoverflow.com/questions/20387349/…
  • 我只需要知道使用哪种方法,以使之前的所有回合保持不变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 2017-10-26
相关资源
最近更新 更多