Results 1 to 7 of 7
Thread: how can I change font size?
- 10-13-2011, 11:37 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
how can I change font size?
In a JPanel component, I can draw text:
JPanel myPanel = new JPanel()
myPanel.getGraphics().drawString("hallo world", 50, 50);
But I can't see how to change the *size* of the text (which is point size 12).
There is no 'setFontSize(...)' method.
There *is* a setFont() method. But that looks like you have to define a new font of your own.
So I'm thinking along these lines:
myPanel.getGraphics().setFont(new Font("myFont", PLAIN, 14));
I hope it's clear what I'm *trying* to do - something very trivial but nevertheless defeating me; and I hope you can help.
- 10-14-2011, 12:04 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: how can I change font size?
It'd be easier to get a reference to the graphics object and then use something similar to what you have done.
Java Code:Graphics g = myPanel.getGraphics(); Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 20); g.setFont(f); g.drawString(...); //other graphics stuff
- 10-14-2011, 12:42 AM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: how can I change font size?
Never call getGraphics() on a JComponent...do your drawing by overriding the paintComponent(Graphics g) method and use the parameter Graphics object. To change the font, call setFont on the component, passing the new font as the parameter. If you want to mix fonts, in the paintComponent method, set the font of the graphics object. The Font class has a deriveFont method to get a new reference to a Font object with the appropriate derived font (for example to change the font size - see the API for more information)
- 10-28-2011, 12:43 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
Re: how can I change font size?
You know you can actually use html tags on strings. Put the <html> tag at the beginning of the string you want to format.
For example:
String text = "<html><font size=4 color=blue> This is a string";
Closing tags are optional, but you'll need it in some cases.
Works in all JComponents.
- 10-28-2011, 02:11 PM #5
- 10-29-2011, 10:21 AM #6
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
- 11-10-2011, 02:42 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
Change the Font Size and Text Position
By stayfierce in forum New To JavaReplies: 1Last Post: 12-03-2009, 05:17 AM -
How to change font/ font color etc in a graphic object using JCombobox?
By JavaInLove in forum AWT / SwingReplies: 5Last Post: 04-25-2009, 08:00 PM -
what is that method to change the size of a font in an applet?
By javanoobita in forum New To JavaReplies: 4Last Post: 02-22-2009, 06:31 AM -
changing font size
By diggitydoggz in forum New To JavaReplies: 1Last Post: 12-25-2008, 07:48 AM -
how to set font size for a xml value
By bala_kj in forum Advanced JavaReplies: 3Last Post: 04-01-2008, 11:00 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
.gif)

Bookmarks