Results 1 to 9 of 9
Thread: Applet to change text color
- 02-02-2011, 04:01 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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:
The error I am getting at compilation isJava 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(""); } }
"AnimateColor.java:10: Invalid method declaration; return type required"
Any help would be appreciated!!
- 02-02-2011, 05:18 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
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)
- 02-02-2011, 07:01 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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:
And if I change the 4th line to:AnimateColor.java:4: AnimateColor is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public abstract class AnimateColor extends Applet implements ActionListener
The code compiles but the applet doesn't run in html!
-
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!
- 02-03-2011, 02:50 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
But how do I do that? I'm sorry but I'm a real rookie at this!
- 02-03-2011, 03:00 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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?
- 02-03-2011, 04:15 AM #7
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
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 problemAnd now the code compiles and runs in html. But the string "Font Animation" isn't appearing!! What could the reason be?
- 02-03-2011, 01:22 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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!
- 02-19-2011, 06:56 PM #9
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Can't change the color of a button...
By mrbeast87 in forum AWT / SwingReplies: 5Last Post: 11-16-2010, 09:22 PM -
Change text color on type
By corbokhan in forum New To JavaReplies: 2Last Post: 11-01-2010, 07:30 PM -
change syntax text color
By dejos456 in forum New To JavaReplies: 5Last Post: 12-14-2009, 11:09 AM -
Change the color in my program
By carl in forum New To JavaReplies: 5Last Post: 04-03-2009, 12:20 PM -
How to use TextAttribute class to change color and font of a text
By Java Tip in forum java.awtReplies: 0Last Post: 06-25-2008, 10:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks