JAVA:遇到Actionlistener问题

时间:2016-05-30 03:14:11

标签: java eclipse applet actionlistener jcreator

我是Java和本网站的初学者。我有一个applet的问题我在JCreator上学写作。 mouselistener工作正常,但actionlistener没有。 '如果'和'其他' actionlistener中的语句应该使按钮在被点击时改变背景的颜色,但是他们不会...这里的任何帮助都会受到赞赏!

谢谢! (参见下文)

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) { }




   }  

1 个答案:

答案 0 :(得分:0)

你永远不会在程序开头的任何地方改变彩虹[8]。只更改r,g或b不会神奇地改变颜色,而是必须在动作监听器中编写代码,以便在按下按钮后设置颜色。

换句话说,请致电

rainbow[ 8 ] = new Color(r, g, b );
更改r,g或b之后

在ActionListener的末尾。