|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Newbie java hello world question
The setFont method in the line with winner.setFont("Helvetica-24"); is not working. In the line setFont is underlined in red and the error messege is "The method setFont(Font) in the type GLabel is not applicable for the arguments (String)". I thought I did this perfectly correct but the editor says that there's an error. The setColor method is working, so I don't know why the setFont method won't work. Can anyone help me fix this?
I'm using Eclipse, if that means anything. /* * File: HelloProgram.java * ----------------------- * This program displays "hello, world" on the screen. * It is inspired by the first program in Brian * Kernighan and Dennis Ritchie's classic book, * The C Programming Language. */ import acm.graphics.*; import acm.program.*; import java.awt.Color; public class HelloProgram extends GraphicsProgram { public void run() { GLabel winner = new GLabel("hello, world", 0, 100); winner.setFont("Helvetica-24"); winner.setColor(Color.ORANGE); add(winner); } } |
|
#2
|
|||
|
|||
|
You probably need to input a java.awt.Font object. Go to (URL address blocked: See forum rules) and search "font" for instructions about the constructor.
|
|
#3
|
|||
|
|||
|
The setFont(Font f) method takes the argument 'Font f' , that is, an object type Font.
Your code does not give a Font object to the method, instead, you give a type String object: "Helvetica-24". So what you should do, is create a new object type font: Code:
/*
* Creates a new object type Font: myfont
*/
Font myFont = new Font("Helvetica", Font.PLAIN, 24);
Then you can add that object to the setFont method: Code:
winner.setFont(myFont); |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Newbie java hello world question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|