简体   繁体   English

JAVA:在使用 Actionlistener 时遇到问题

[英]JAVA: Having trouble with Actionlistener

I'm a beginner to Java and to this website.我是 Java 和这个网站的初学者。 I'm having an issue with an applet I'm writing in JCreator for school.我在 JCreator 中为学校编写的小程序出现问题。 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! actionlistener 中的“if”和“else”语句应该使按钮在被点击时改变背景颜色,但它们不会......这里的任何帮助将不胜感激!

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.除了在程序开始时,您永远不会在任何地方更改 Rainbow[8]。 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.仅更改 r、g 或 b 不会神奇地更改颜色,而是必须在操​​作侦听器中编写代码以在按下按钮后设置颜色。

So in other words, call所以换句话说,调用

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

at the end of your ActionListener after changing r, g, or b.更改 r、g 或 b 后,在 ActionListener 的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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