Results 1 to 9 of 9
  1. #1
    sak_java is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Applet to change text color

    Hello guys, I'm sort of a beginner to java and they gave us this program in school which I can't seem to compile. It's basically an applet to change color of text as the user wishes. Here's the code:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class AnimateColor extends Applet implements ActionListener
    {
    Button b;
    Label l;
    TextField tf;
    String msg="Font animation";
    public Animatecolor()
    {
    super();
    }
    public void destroy()
    {
    }
    public void init()
    {
    l=new Label("Enter color");
    tf=new TextField(15);
    b=new Button("CHANGE");
    add(l);
    add(tf);
    add(b);
    setLayout(null);
    l.setBounds(40,40,150,20);
    tf.setBounds(200,40,200,40);
    b.setBounds(100,80,100,40);
    b.addActionListener(this);
    }
    public void start()
    {
    }
    public void paint(Graphics g)
    {
    g.drawString(msg,100,100);
    }
    public void stop()
    {
    }
    public void actionperformed(ActionEvent ae)
    {
    String st=ae.getActionCommand();
    String s=tf.getText();
    if(s.equalsIgnoreCase("red"))
    setForeground(Color.red);
    else
    if(s.equalsIgnoreCase("yellow"))
    setForeground(Color.yellow);
    else
    if(s.equalsIgnoreCase("blue"))
    setForeground(Color.blue);
    else
    if(s.equalsIgnoreCase("black"))
    setForeground(Color.black);
    else
    if(s.equalsIgnoreCase("cyan"))
    setForeground(Color.cyan);
    tf.setText("");
    }
    }
    The error I am getting at compilation is
    "AnimateColor.java:10: Invalid method declaration; return type required"
    Any help would be appreciated!!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Java is case sensitive, check the definition of the constructor defined in your code...if its not the same (both in spelling and case) as the class the compiler thinks its a method declaration (which requires a return type)

  3. #3
    sak_java is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Great spot there I must say! Didn't realize that little mistake till you pointed it out. But even after I've made the change there's still an error. But this time it says:
    AnimateColor.java:4: AnimateColor is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
    And if I change the 4th line to:
    public abstract class AnimateColor extends Applet implements ActionListener
    The code compiles but the applet doesn't run in html!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    No, you're correcting the wrong thing. That error message is not telling you to make your class abstract but rather to implement all the methods of any interface that the class implements. Solution: implement the methods, in this case one -- and the error message even tells you which one!

  5. #5
    sak_java is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    But how do I do that? I'm sorry but I'm a real rookie at this!

  6. #6
    sak_java is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Ok I made this change:

    public void actionPerformed(ActionEvent ae)

    And now the code compiles and runs in html. But the string "Font Animation" isn't appearing!! What could the reason be?

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    And now the code compiles and runs in html. But the string "Font Animation" isn't appearing!! What could the reason be?
    We have no clue about your project and changes you made, so you must provide context...preferably a Short, Self Contained, Correct Example that demonstrates the problem

  8. #8
    sak_java is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Got it... turns out I had to just increase the width and height of the applet window in the html file. Program works fine now! Thanks for all the help anyways!

  9. #9
    Awais261 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    That error message is not telling you to make your class abstract but rather to implement all the methods of any interface that the class implements. Solution: implement the methods, in this case one.

Similar Threads

  1. Can't change the color of a button...
    By mrbeast87 in forum AWT / Swing
    Replies: 5
    Last Post: 11-16-2010, 09:22 PM
  2. Change text color on type
    By corbokhan in forum New To Java
    Replies: 2
    Last Post: 11-01-2010, 07:30 PM
  3. change syntax text color
    By dejos456 in forum New To Java
    Replies: 5
    Last Post: 12-14-2009, 11:09 AM
  4. Change the color in my program
    By carl in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 12:20 PM
  5. Replies: 0
    Last Post: 06-25-2008, 10:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •