简体   繁体   中英

JAVA: Having trouble with Actionlistener

I'm a beginner to Java and to this website. I'm having an issue with an applet I'm writing in JCreator for school. The mouselistener works okay, but the actionlistener does not. The 'if' and 'else' statements in the actionlistener should make the buttons change the background's colour upon being clicked, but they don't... Any help here would be appreciated!

Thanks! (SEE CODE BELOW)

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class MilestoneTwo extends Applet
                      implements MouseListener, ActionListener {

private int x = 50, y = 50;

int n = 10;
Color[] rainbow; 
int c = 0;
Button redButton;
Button blueButton;
Button greenButton;
int r = 50;
int g = 50;
int b = 50;




   public void init() {


  redButton = new Button ( "MAKE BG MORE RED" );
  blueButton = new Button ( "MAKE BG MORE BLUE" );
  greenButton = new Button ( "MAKE BG MORE GREEN" );
 add ( redButton );
  redButton.addActionListener ( this );
 add ( blueButton );
  blueButton.addActionListener ( this );
  add ( greenButton );
  greenButton.addActionListener ( this );





 // RGB ARRAY
 rainbow = new Color[ n ];

//loading array cells with colours

rainbow[ 0 ] = new Color( 90, 150, 110 ); 
rainbow[ 1 ] = new Color( 50, 250, 100 ); 
rainbow[ 2 ] = new Color( 250, 200, 10 ); 
rainbow[ 3 ] = new Color(4, 60, 123 );
rainbow[ 4 ] = new Color(230, 70, 15 );
rainbow[ 5 ] = new Color(20, 30, 230 );
rainbow[ 6 ] = new Color(255, 35, 179 );
rainbow[ 7 ] = new Color(110, 10, 40 );
rainbow[ 8 ] = new Color(r, g, b );





    // Registering MouseListener 
   addMouseListener(this);

}


public void paint(Graphics g) {

   //if statements for circle colour



    setBackground(rainbow[ 8 ]);

     if (c == 0){ g.setColor(rainbow[ 0 ]); 
     }
  else if (c==1){ g.setColor(rainbow[ 1 ]);
   }

   else if (c==2){ g.setColor(rainbow[ 2 ]);
   }

  else if (c==3) { g.setColor(rainbow[ 3 ]);
   }

  else if (c==4){ g.setColor (rainbow[ 4 ]);
   }

   else if (c==5){ g.setColor (rainbow[ 5 ]);
   }

    else if (c==6){ g.setColor (rainbow[ 6 ]);
   }

    else if (c==7){ ;
    g.setColor (rainbow[ 7 ]);
   }

  g.fillOval( x, y, 75,75 );



    }


public void actionPerformed(ActionEvent z) { 


    if (z.getSource() == redButton) 
    {

if (r==250){ 
r=0;
}

         else  {
          r = (r+50);
          }

      } 

     else if (z.getSource() == blueButton) {

         if (b==250){ 
          b=0;
          }

         else  {
          b = (b+50);
          }


 }

    else if (z.getSource() == greenButton) {

        if (g==250){ 
        g=0;
        }

        else  {
         g = (g+50);
         }

 }


      repaint();
 }






    // what's executed upon click.
 public void mouseClicked(MouseEvent e) {

    x = e.getX();
    y = e.getY();

    if (c > 7) { c = 0; //reset of c if needed
    }
    else { c = (c+1);
    }
    repaint();

 }
   // useless methods

  public void mouseExited(MouseEvent e) { }

  public void mouseEntered(MouseEvent e) { }

 public void mousePressed(MouseEvent e) { }

public void mouseReleased(MouseEvent e) { }




   }  

You never change rainbow[8] anywhere but at the beginning of your program. Just changing r, g, or b will not magically change the color, but instead you must write code within the action listener to set the color after the button's been pressed.

So in other words, call

rainbow[ 8 ] = new Color(r, g, b );

at the end of your ActionListener after changing r, g, or b.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM