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

标签 java swing jbutton

我正在为大学做一个类(class)作业,作业是使用 JPanel 创建一个 RectanglesGUI,但我创建的按钮有问题。

buttonSOUTH 应该执行以下操作:

当用户单击 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 将返回 0 和 1

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

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

关于java - 用JButton改变颜色,设置for循环终止条件为无限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41294624/

相关文章:

java - 关闭框架java后重新启用按钮

java - 为什么我的按钮无法执行正确的操作?

java - 测试调用 : how to do set up common to all test suites

java - SolrQuery 不返回少于 3 个字符的结果

Java Swing JButton 未显示在 JPanel 上

java - Thread.sleep() 在其他语句之前暂停 JFrame

跨时区的 Java Quartz-Scheduler

java - 在 webview 上添加 pull to refresh 以进行刷新

java - 在没有 getValueAt() 并发症的情况下修剪 AbstractTableModel

java - 如何在图片上添加按钮?