Java Swing绘制的形状消失了

时间:2016-05-30 04:47:12

标签: java swing graphics paint

我正在使用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语句吗?请帮忙。

1 个答案:

答案 0 :(得分:1)

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

repaint() - 无法覆盖此方法。它控制update() -> paint()周期。您应该调用此方法来获取重新绘制自身的组件。如果您已经做了任何改变组件外观的事情,但不是它的大小(如改变颜色,动画等),那么请调用此方法。

validate() - 这告诉组件再次出现并重新绘制自己。如果您已经做了任何改变组件或其任何子组件(添加,删除,调整子级大小)的大小,您应该调用此方法...我认为调用revalidate()比调用{{1}更受欢迎但是在Swing中......

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

paint() - 如果您更改了可插拔外观,请调用此方法。在组件可见后感觉到它。

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