Results 1 to 17 of 17
Thread: Can't use SetFont on Eclipse
- 05-27-2010, 05:05 PM #1
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
Can't use SetFont on Eclipse
Hiya,
I wonder why I can't use setFont method on Eclipse.My code is below:
Java Code:import acm.program.*; import acm.graphics.*; import java.awt.*; import java.awt.font.*; import java.awt.color.*; public class chapter5 extends GraphicsProgram{ public void run(){ GLabel label = new GLabel("Hello World"); label.setFont("monospaced"); label.setColor(Color.RED); double x = (getWidth() - label.getWidth()) / 2; double y = (getHeight() + label.getAscent()) / 2; add(label,100,75); } }
- 05-27-2010, 05:07 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
There is are mark underneath setFont method on my eclipse,showing that this method is an error or invalid.I wonder why?
- 05-27-2010, 05:13 PM #3
What is GLabel? The Swing components need a Font: javax.swing.JComponent.setFont(Font font)
not a String.Java Code:jLabel1.setFont(new Font("Monospaced", Font.PLAIN, 12));Last edited by PhHein; 05-27-2010 at 05:16 PM.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 05-27-2010, 05:28 PM #4
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
GLabel helps you to write letters on eclipse.real java developers may not come across this before because it is associated with eclipse and makes things easier for learners of java.
- 05-27-2010, 05:31 PM #5
Please copy and paste the full text of the compiler's error message here.
What parameter(s) does the setFont() method take? What are you giving it?
- 05-27-2010, 05:35 PM #6
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
here is the compilation error:
java.lang.Error: Unresolved compilation problem:
The method setFont(Font) in the type GLabel is not applicable for the arguments (String)
at chapter5.run(chapter5.java:17)
at acm.program.Program.runHook(Program.java:1182)
at acm.program.Program.startRun(Program.java:1169)
at acm.program.Program.init(Program.java:834)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
-
Edit: deleted. I didn't see that setFont had an override that allowed passage of the AWT Font object as a parameter. :(
Last edited by Fubarable; 05-27-2010 at 08:15 PM.
- 05-27-2010, 05:47 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
As the error says (and I suspect as PhHein guessed) this:
setFont(Font)
is the method signature...you need to pass it a Font object, not a string.
- 05-27-2010, 05:47 PM #9
The text of the message shows the parameter should be a Font object.The method setFont(Font)
When all else fails, read the doc.
- 05-27-2010, 05:52 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
The existence of API documentation should be the first thing taught to people learning Java (or any language).
- 05-27-2010, 05:56 PM #11
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
okay
okay.I see.
- 05-27-2010, 06:35 PM #12
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
Yes Phhein is right.
Problem solved.My new code which solved the problem.
import java.awt.Color;
import java.awt.Font;
import acm.program.*;
import acm.graphics.*;
public class chapter5 extends GraphicsProgram{
public void run(){
GLabel label = new GLabel("Hello World");
label.setFont (new Font("Monospaced", Font.PLAIN, 12));
label.setColor(Color.RED);
double x = (getWidth() - label.getWidth()) / 2;
double y = (getHeight() + label.getAscent()) / 2;
add(label,100,75);
}
}
- 05-27-2010, 07:37 PM #13
I was trying to use Microsoft's Visual Studio for a C++ / MFC project and asked some experts on some forum (years ago, don't remember where) and was told there wasn't any doc like we have for java.(or any language)
Learning how to use the doc should definitely be part of any course.
- 05-30-2010, 10:58 AM #14
Senior Member
- Join Date
- May 2010
- Location
- London
- Posts
- 106
- Rep Power
- 0
If you haven't already added the ACM documentation to your bookmarks might be a good idea to do so.
ACM Documentation
But yeah I've used the setFont method as the book taught and it's worked fine for me; and so does your code.
Also, is it necessary for him to import awt.font and awt.color? Doesn't importing awt.* import everything under awt?
- 05-30-2010, 03:05 PM #15
I think only at that level, not deeper. Write a small test program and see what happens.is it necessary for him to import awt.font and awt.color? Doesn't importing awt.* import everything under awt?
Be sure to let us know what you find.
- 05-30-2010, 04:45 PM #16
Senior Member
- Join Date
- May 2010
- Location
- London
- Posts
- 106
- Rep Power
- 0
- 05-30-2010, 04:56 PM #17
Similar Threads
-
What is Eclipse?
By tyang in forum New To JavaReplies: 5Last Post: 01-31-2010, 09:43 PM -
Is it possible to run eclipse plugins without using eclipse?
By ambar in forum EclipseReplies: 2Last Post: 01-27-2009, 02:10 PM -
Eclipse Bug - Can't Read From A File Using Eclipse?
By carlodelmundo in forum New To JavaReplies: 6Last Post: 01-26-2009, 04:25 PM -
Eclipse help
By johnkennykumar in forum EclipseReplies: 2Last Post: 12-31-2008, 03:21 AM -
JDK 1.6 and Eclipse 6.0.1
By Mir in forum New To JavaReplies: 37Last Post: 07-02-2008, 12:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks