Results 1 to 11 of 11
- 04-05-2009, 03:26 PM #1
Fonts (Changing Fonts and Color's)
I am trying to make it so that the following code changes fonts and color
The lineJava Code:if (outputBmi < 18.5) { txtResults.setText("Underweight"); //Needs to be orange txtResults.setFont(new Font("Serif", BOLD, 18)); } else{ if (outputBmi > 24.9){ txtResults.setText("Overweight"); //Needs to be red } else txtResults.setText("OK"); //needs to be greengives me an error, it doesnt like BOLD, I thought that the import java.awt.*; contained the library for fonts so surely BOLD should not casue this problem?Java Code:txtResults.setFont(new Font("Serif", BOLD, 18));
I am also looking to change the colour as per comments under each statement.
Thanks in advance for any assistance
- 04-05-2009, 06:31 PM #2
After searching the web and going through books I have tried several things, the latest is
this doesnt like Font, yet when I haveJava Code:Font font3 = new Font("Papyrus",Color.ORANGE, Font.BOLD,20);
it accepts thisJava Code:Font font2 = new Font("Ariel", Font.BOLD, 17);
can someone advise as to whether I am making this more difficult than it needs to be?
Thanks
-
Have you reviewed the Font API? That's where I'd start. Reviewing this shows no constructor that accepts a Color parameter.After searching the web and going through books I have tried several things,...
What is "txtResults", a JLabel? If so, then perhaps you want to set its foreground color via setForeground(Color.YELLOW)?
- 04-05-2009, 07:15 PM #4
txtResults is the text field that displays 'ok', 'underweight' or 'overweight'
I am currently looking at the link you suggested , ermm ok, i admit it, im lost, I dont see anything to tell me what I want to do.
- 04-05-2009, 07:21 PM #5
Member
- Join Date
- Apr 2008
- Posts
- 42
- Rep Power
- 0
looool b4 any1 posted a reply to this thread i was answering your question, but my laptop ran out of battery and it closed :P .. Anyway ,,
to change the font you need Font.BOLD instead of just BOLD but i see you figuered that out .
To change the color of the text :
or u canJava Code:txtResults.setForeground(Color.RED); // or any other color
Java Code:Color c = new Color(int,int,int); // and define the values
- 04-05-2009, 07:40 PM #6
Thats great, thanks for your help, glad your battery didnt take to long to recharge...lol
- 04-05-2009, 07:51 PM #7
They are working apart from the overweight, this is printing out in green rather than red.
Java Code:if (outputBmi < 18.5) { txtResults.setText("Underweight"); txtResults.setForeground(Color.ORANGE); txtResults.setBackground(Color.BLACK); } else{ if (outputBmi > 24.9){ txtResults.setText("Overweight"); txtResults.setForeground(Color.RED); txtResults.setBackground(Color.BLACK); } else txtResults.setText("OK"); txtResults.setForeground(Color.GREEN); txtResults.setBackground(Color.BLACK); //needs to be green }
- 04-05-2009, 10:29 PM #8
Member
- Join Date
- Apr 2008
- Posts
- 42
- Rep Power
- 0
Green !!!? huh .. i have never encountered anything like that :P .. BUT try putting curly brackets in the last else statement . coz usually when u dont put brackets after an if or an else statement in java, java only reads the next line .. so ...
Java Code:else Statement1 // only statement1 executes Statement2
I hope u understood what I meant :)Java Code:else{ // here both statement1 & 2 are executed Statement1 Statement2 }
lol just to be clear ill show u in ur example
u 4got these bracketsJava Code:if (outputBmi < 18.5) { txtResults.setText("Underweight"); txtResults.setForeground(Color.ORANGE); txtResults.setBackground(Color.BLACK); } else{ if (outputBmi > 24.9){ txtResults.setText("Overweight"); txtResults.setForeground(Color.RED); txtResults.setBackground(Color.BLACK); } else [COLOR="Lime"]{[/COLOR] txtResults.setText("OK"); txtResults.setForeground(Color.GREEN); txtResults.setBackground(Color.BLACK); //needs to be green [COLOR="lime"]}[/COLOR] }
- 04-05-2009, 11:15 PM #9
lol, thanks for that, those braces get me every time
-
With practice, you will find the API to be your best friend, and if you are going to be coding in Java, you need this friendship. I strongly suggest that you start studying the API daily so that you will learn how to use it. If you do this, you will get better at using it, and you will be able to answer your own questions much quicker and more easily than you can get answers here.
Best of luck.
- 04-06-2009, 01:32 AM #11
So having the colors displaying as required, I have a further question
I have a optionPane that if the user clicks Help will get the instruction of the use of the app
Within those instructions from the information that has already been given here can someone tell me if it is possible to format the text within this optionPane?
I am looking to center & color headings etc
I would think it is but before I try I just want to know if it is possible.
I am still trying to digest the link you gave me fubarable, thanks but at the moment its over my head, I will keep reading it though
Thanks
Similar Threads
-
how to get raw image data for fonts
By Nicholas Jordan in forum Java 2DReplies: 8Last Post: 04-05-2009, 06:15 AM -
its not changing bgcolor
By javanoobita in forum Java AppletsReplies: 1Last Post: 02-21-2009, 02:29 PM -
Listing all available fonts provided in the system
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:06 PM -
no of fonts and cells exceeding error in excel sheet.
By iimasd in forum AWT / SwingReplies: 0Last Post: 11-06-2007, 06:58 AM -
OS 10.4 - Help with Java in Yahoo! Games - Fonts Unreadable?
By acleme1 in forum Java AppletsReplies: 3Last Post: 07-16-2007, 11:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks