Java 如何更新单击处理程序,使其在三种颜色之间循环:红色、绿色和蓝色?

Java 如何更新单击处理程序,使其在三种颜色之间循环:红色、绿色和蓝色?,java,swing,colors,Java,Swing,Colors,所以这个程序的目的是创建一个椭圆和一个按钮,当你点击它时,椭圆会改变颜色。颜色的顺序应该是红色,然后是绿色,然后是蓝色。如何创建此循环?我使用for循环吗?这是我的代码,没有循环: public class ButtonButton implements java.awt.event.ActionListener{ private NscWindow win; private NscEllipse oval; public ButtonButton() { win

所以这个程序的目的是创建一个椭圆和一个按钮,当你点击它时,椭圆会改变颜色。颜色的顺序应该是红色,然后是绿色,然后是蓝色。如何创建此循环?我使用for循环吗?这是我的代码,没有循环:

public class ButtonButton implements java.awt.event.ActionListener{

   private NscWindow win;
   private NscEllipse oval;

   public ButtonButton() {
     win = new NscWindow();
     win.setTitle("ButtonButton");
     oval = new NscEllipse(100, 70, 200, 150);
     oval.setFilled(true);
     oval.setBackground(java.awt.Color.blue);

     javax.swing.JButton btn;
     btn = new javax.swing.JButton("click me");
     win.add(oval);
     btn.setSize(170, 35);
     btn.setLocation(110, 10);
     win.add(btn);
     win.repaint();
     btn.addActionListener(this);

  }
   public void actionPerformed(java.awt.event.ActionEvent e) {
     win.setTitle("Thanks, I needed that");
     javax.swing.JButton btn;
     btn = (javax.swing.JButton)e.getSource();
     btn.setText("Thanks, I needed that");
     oval.setBackground(java.awt.Color.green);
     win.repaint();
  }

   public static void main(String[] args) {
    new ButtonButton();
 }
}

要循环完成每个步骤,请执行以下操作:

 public void actionPerformed(java.awt.event.ActionEvent e) {
     win.setTitle("Thanks, I needed that");
     javax.swing.JButton btn;
     btn = (javax.swing.JButton)e.getSource();
     btn.setText("Thanks, I needed that");

     if (oval.getBackground().equals(java.awt.Color.red))
         oval.setBackground(java.awt.Color.green);
     else if (oval.getBackground().equals(java.awt.Color.green))
         oval.setBackground(java.awt.Color.blue);
     else if (oval.getBackground().equals(java.awt.Color.blue))
         oval.setBackground(java.awt.Color.red);
     win.repaint();
}

从创建颜色数组开始

public static final Color COLORS[] = new Color[]{Color.RED, Color.GREEN, Color.BLUE};
提供某种类型的值来跟踪您使用的颜色

private int colorIndex = -1;
添加一个方便的方法来更改颜色

public void applyNextColor() {
    colorIndex++;
    if (colorIndex >= COLORS.length) {
        colorIndex = 0;
    }
    oval.setBackground(COLORS[colorIndex]);
}
然后在构造函数中初始化初始颜色

public ButtonButton() {
    win = new NscWindow();
    win.setTitle("ButtonButton");
    oval = new NscEllipse(100, 70, 200, 150);
    oval.setFilled(true);
    nextColor();

    //...
}
然后,在你的动作监听器中,只需应用下一种颜色

public void actionPerformed(java.awt.event.ActionEvent e) {
    win.setTitle("Thanks, I needed that");
    // This is a NullPointerException waiting to happen...
    //javax.swing.JButton btn;
    btn = (javax.swing.JButton)e.getSource();
    btn.setText("Thanks, I needed that");
    applyNextColor();
    win.repaint();
}

您可以使用
enum
和/或模块化数学做同样的事情,但这使它保持简单;)

您想在每次单击时或在短暂延迟后更改颜色?每次单击。我们的想法是,每次你点击这个按钮,它就会进入一个循环。欢迎你,如果我回答了你的问题,请点击论坛上其他开发者答案旁边的不透明复选标记,将其标记为已解决