用JButton改变颜色,设置for循环终止条件为无限

Change color with JButton, set the for loop terminating condition to infinite

我正在为 Uni 做一个课程作业,作业是用 JPanel 创建一个 RectanglesGUI,但是我创建的按钮有问题。

SOUTH 按钮应该执行以下操作:

当用户点击 SOUTH 区域的 JButton 时, 用 color1 填充的矩形应该全部更改为随机颜色, 而用 color2 填充的矩形不应该改变。一秒 单击 JButton 应该使矩形全部填充 color2 更改为随机颜色,而矩形填充随机 第一次点击的颜色应该保持相同的颜色。用户应该是 能够无限期地继续点击按钮,并且每次点击 一组矩形将填充随机颜色。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class RectanglesGUI {

    JFrame frame;
    RectangleDrawPanel drawPanel;
    Color color1 = Color.orange;
    Color color2 = Color.blue;


    public static void main (String[] args) {
        RectanglesGUI gui = new RectanglesGUI();
        gui.go();
    }

    //this method sets up the JFrame
    public void go() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        drawPanel = new RectangleDrawPanel();
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
        frame.getContentPane().setBackground(Color.black);

        frame.setSize(600,600);
        frame.setVisible(true);

        CreateButton newButton = new CreateButton();
        newButton.CreateButton();
        frame.repaint();

    }

    // To Create a new Button
    public class CreateButton {

        JButton buttonSOUTH;
        JButton buttonNORTH;

        public void CreateButton () {
            buttonSOUTH = new JButton("Change Colors");
            buttonSOUTH.setPreferredSize(new Dimension(100, 50));
            buttonSOUTH.setVisible(true);
            frame.add(buttonSOUTH, BorderLayout.SOUTH);
            buttonSOUTH.addActionListener(new RandomColorListener());

            buttonNORTH = new JButton("Reset Colors");
            buttonNORTH.setPreferredSize(new Dimension(100, 50));
            buttonNORTH.setVisible(true);
            frame.add(buttonNORTH, BorderLayout.NORTH);
            buttonNORTH.addActionListener(new ResetListener());
        }

        // ActionListener for buttonSOUTH
        private class ResetListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                color1 = Color.orange;
                color2 = Color.blue;
                frame.repaint();
            }
        }

        // ActionListener for buttonNORTH
        private class RandomColorListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                ChangeColor c = new ChangeColor();

                for (int i = 0; i < 100; i++){
                    if (i % 2 == 0) {
                        color1 = c.getColor1();
                        frame.repaint();
                    } else {
                        color2 = c.getColor2();
                        frame.repaint();
                    }
                }
            }
        }


        // Change Color Class
        private class ChangeColor {

            private Color getColor1(){
                Random fill1 = new Random();
                color1 = new Color (fill1.nextInt(256), fill1.nextInt(256),
                        fill1.nextInt(256));
                return color1;
            }

            private Color getColor2(){
                Random fill2 = new Random();
                color2 = new Color (fill2.nextInt(256), fill2.nextInt(256),
                        fill2.nextInt(256));
                return color2;
            }
        }
    }

    class RectangleDrawPanel extends JPanel {
        public void paintComponent (Graphics g){
            super.paintComponent(g);
            Graphics2D g2=(Graphics2D)g;
            int width = getWidth();
            int height = getHeight();

            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    int x = (getWidth() / 5) * i;
                    int y = (getHeight() / 5) * j;
                    if ((i % 2) == (j % 2)) {
                        g2.setColor(color1);
                    } else
                        g2.setColor(color2);
                    g2.fill3DRect(x,y,width,height,true);
                }
            }
        }
    }
}

但是我不知道怎么设置为无限

for (int i = 0; i < 100; i++){
     if (i % 2 == 0) {
         color1 = c.getColor1();
         frame.repaint();
      } 
      else {
          color2 = c.getColor2();
          frame.repaint();
      }
}

这部分for (int i = 0; i < 100; i++) 对检查没有用。
条件检查中的两个部分将始终执行。这意味着 color2 和 color1 都会改变。因为 i%2 将 return 0 和 1

将此 属性 添加到 RectangleGui class boolean status=false;
然后将 class RandomColorListener 中方法 actionPerformed 内的代码更改为:

 if (status) {
      color1 = c.getColor1();
      frame.repaint();
 } else {
      color2 = c.getColor2();
      frame.repaint();
 }
  status=!status;