Results 1 to 17 of 17
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default 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);
    		 
    		 
    	 }
    	 
     }

  2. #2
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    There is are mark underneath setFont method on my eclipse,showing that this method is an error or invalid.I wonder why?

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    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 06:16 PM.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    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.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    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?

  6. #6
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    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)

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

    Default

    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 09:15 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default

    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.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    The method setFont(Font)
    The text of the message shows the parameter should be a Font object.

    When all else fails, read the doc.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default

    The existence of API documentation should be the first thing taught to people learning Java (or any language).

  11. #11
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default okay

    okay.I see.

  12. #12
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    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);


    }

    }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    (or any language)
    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.

    Learning how to use the doc should definitely be part of any course.

  14. #14
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    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?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    is it necessary for him to import awt.font and awt.color? Doesn't importing awt.* import everything under awt?
    I think only at that level, not deeper. Write a small test program and see what happens.
    Be sure to let us know what you find.

  16. #16
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    I think only at that level, not deeper. Write a small test program and see what happens.
    Be sure to let us know what you find.
    Works fine for me; I can change the font and the font color without importing awt.font and awt.color.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    Yes those two classes are in package: java.awt
    I was thinking of: java.awt.event.FocusAdapter
    I don't think import java.awt.*;
    works for this class

    It depends what "under awt" means. I read it as all lower branches, not just the one,

Similar Threads

  1. What is Eclipse?
    By tyang in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 10:43 PM
  2. Replies: 2
    Last Post: 01-27-2009, 03:10 PM
  3. Eclipse Bug - Can't Read From A File Using Eclipse?
    By carlodelmundo in forum New To Java
    Replies: 6
    Last Post: 01-26-2009, 05:25 PM
  4. Eclipse help
    By johnkennykumar in forum Eclipse
    Replies: 2
    Last Post: 12-31-2008, 04:21 AM
  5. JDK 1.6 and Eclipse 6.0.1
    By Mir in forum New To Java
    Replies: 37
    Last Post: 07-02-2008, 01:15 PM

Posting Permissions

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