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

时间:2016-12-23 03:00:05

标签: java swing jbutton

我正在为Uni做一个Coursework任务,分配是用JPanel创建一个RectanglesGUI,但我对我创建的按钮有问题。

buttonSOUTH应该执行以下操作:

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

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();
      }
}

1 个答案:

答案 0 :(得分:1)

这部分for (int i = 0; i < 100; i++) 对检查无用。
部分内部条件检查将始终执行。这意味着color2和color1都会改变。因为i%2将返回0和1

将此属性添加到R​​ectangleGui类boolean status=false;
然后将类RandomColorListener中的方法actionPerformed中的代码更改为:

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