GUI Calculator - How do I change the text color & set a background image?
Discuss GUI Calculator - How do I change the text color & set a background image? in the Java Help forum on Dev Shed. GUI Calculator - How do I change the text color & set a background image? Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
Posts: 13
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
GUI Calculator - How do I change the text color & set a background image?
Hello! I had a project in my Java class which worked out great. We had to create a GUI Calculator, I got it running fine. The problem is that it is ugly. I have been trying to find some information on setting some of the keys with a different text color and maybe a background image. The keys are in rows/columns 5x5 with 10x10 pixels between them. I was wanting to add an image to the background that would be seen between the keys. If anyone knows anything about adding the colored text and background image please let me know. I can post the code if requested. (It is about 250+ lines) Thanks in advance.
Posts: 7,652
Time spent in forums: 3 Months 6 Days 1 h 35 m 4 sec
Reputation Power: 6083
Sally and Bobby go to the store. It is seven o'clock and might rain soon, but is still light out. How much money did they spend at the store?
Notice how this had absolutely no relevant information? Neither did your post. You never specified what kind of text you're changing and what classes control that text. Nor have you said what kind of Object you're looking to give a background image to.
Now let's do one thing more, which is pretend we have a valuable asset called a "search engine" at out disposal. Let's take a wild guess: What were to happen if you used this "search engine" to find out how to add a background by searching for whatever object you want to add a background to, along with the words "background image"? Or the same for text. What kind of results do you think you'd get?
__________________ Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
“Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
-Partial Credit: Sun
If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
Ches Koblents
Last edited by gimp : November 10th, 2009 at 11:28 PM.
Posts: 13
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Search Engine? Whats that?
Quote:
Originally Posted by gimp
Sally and Bobby go to the store. It is seven o'clock and might rain soon, but is still light out. How much money did they spend at the store?
Notice how this had absolutely no relevant information? Neither did your post. You never specified what kind of text you're changing and what classes control that text. Nor have you said what kind of Object you're looking to give a background image to.
Now let's do one thing more, which is pretend we have a valuable asset called a "search engine" at out disposal. Let's take a wild guess: What were to happen if you used this "search engine" to find out how to add a background by searching for whatever object you want to add a background to, along with the words "background image"? Or the same for text. What kind of results do you think you'd get?
Been there done that, couldn't find what I wanted. Thats why I got on here, just for the sarcasm. It is a bit lenghty, so I will post in a bit after I get up and get my head together. Until then maybe go read a childrens story to hold you over, maybe call Sally and Bobby and ask if you can go with them, maybe they will buy you a lolly pop. Bring your rain coat.
Posts: 119
Time spent in forums: 13 h 52 m 11 sec
Reputation Power: 0
Quote:
Originally Posted by jaysonjohnson
Hello! I had a project in my Java class which worked out great. We had to create a GUI Calculator, I got it running fine. The problem is that it is ugly. I have been trying to find some information on setting some of the keys with a different text color and maybe a background image. The keys are in rows/columns 5x5 with 10x10 pixels between them. I was wanting to add an image to the background that would be seen between the keys. If anyone knows anything about adding the colored text and background image please let me know. I can post the code if requested. (It is about 250+ lines) Thanks in advance.
-Jay
use the setColor method()
or the setBackground method()
Code:
// Make a text area, set its font and color, then add it to the frame
TextArea text = new TextArea();
Font font = new Font("Serif", Font.ITALIC, 20);
text.setFont(font);
text.setForeground(Color.blue);
Posts: 13
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Calculator Code
Ok, I know the TextArea() method. My question is, where would I implement it in the program? I want to change the text that is inside the buttons, i.e. the numbers. And between the buttons display an image. One big image would be best that is in the background and can be seen between the buttons. I have included the code. Thanks in advance.
Posts: 13
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Yeah... I was just kidding...
Quote:
Originally Posted by gimp
Are you freaking kidding me? java.awt.Button has a setFont() method.
As for setting background images, this tutorial was found from five minutes of googling.
WOW!? 5 minutes, you are so ELITE! Thanks, anyways. Theres nothing like constructive sarcasm, works every time. I'll follow your advice like one would follow the word of god. Ive been programming Java for 8 weeks now, so you know that I know everything, Im trying to follow in your foot steps gimpy. You do realize that if it personally hurts or annoys you to answer "dumb" questions asked by people who havent been doing Java long, that YOU DONT HAVE TO REPLY!! Go figure!? I thought that was the reason for this site!? Its not like this is a "Genious Elite Coders" website. I thought this was an OPEN FORUM for SHARING and LEARNING. You may want to re-evaluate the reason you are here. Anyways, thanks for the advice.
Posts: 119
Time spent in forums: 13 h 52 m 11 sec
Reputation Power: 0
you add it after every button that you want to change the color and the background image you add it before the background tag
simple program that changes the background color to what the user types in. try red, blue, or white and you will notice where it is placed in the code
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Colors extends JApplet implements ActionListener //make the class
{
private GridLayout layout = new GridLayout(3, 1);
JPanel panel02 = new JPanel(new FlowLayout()); //make panels
JPanel panel03 = new JPanel(new FlowLayout());
JPanel panel04 = new JPanel(new FlowLayout());
Container con = getContentPane();
FlowLayout Lay = new FlowLayout(FlowLayout.CENTER);
JLabel head = new JLabel("Please enter a color");
JTextField choice = new JTextField("",10);
JButton enter = new JButton("Press After Typing");
JLabel error = new JLabel("");
JPanel pane = new JPanel() ; //make a new panel
public void init()
{
con.setLayout(layout);//set the layout
con.add(panel02); //add panels
con.add(panel03);
con.add(panel04);
panel02.add(head); //add other goodies
panel03.add(choice);
panel03.add(enter);
enter.addActionListener(this);
panel04.add(error);
}
public void actionPerformed(ActionEvent e) //make the actions
{
Object source = e.getSource(); //get the button workings
String cn = choice.getText(); //get the info
try
{
if(cn.equals("red")) //see if color is red
{
panel02.setBackground(Color.RED); //change the back ground colors
panel03.setBackground(Color.RED);
panel04.setBackground(Color.RED);
}
else
if(cn.equals("blue")) //see if it is blue
{
panel02.setBackground(Color.BLUE); //change the back ground colors
panel03.setBackground(Color.BLUE);
panel04.setBackground(Color.BLUE);
}
else
if(cn.equals("white")) //see if it is blue {
{
panel02.setBackground(Color.WHITE); //change the back ground colors
panel03.setBackground(Color.WHITE);
panel04.setBackground(Color.WHITE);
}
else
throw new Exception("an invalid color"); //throw an exception
{
error.setText( ""); //clear label if its right
}
}
catch(Exception ex) //catch the exception
{
choice.setText(""); //erase color choice
error.setText( "an invalid color"); //change the label to show an error
}
}
}
Last edited by Daren : November 11th, 2009 at 04:33 PM.
Posts: 13
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Text Color Change
Ok, thanks Daren. These are the changes I have made with good results. Pretty easy. I gave up on the idea of the background image. It looks even better if I just take the space out that is between the buttons. One other thing that I was wondering, in the GridLayout my buttons are 5x4 I want the operator buttons a different color then the number keys. http://bestinlifeproductions.angelf...vaquestion.html Here is a link to a picture that best describes what im talking about. I have tried a few things as far as playing around with the gridlayout but with no success. Thanks.
Posts: 119
Time spent in forums: 13 h 52 m 11 sec
Reputation Power: 0
Quote:
Originally Posted by jaysonjohnson
Ok, thanks Daren. These are the changes I have made with good results. Pretty easy. I gave up on the idea of the background image. It looks even better if I just take the space out that is between the buttons. One other thing that I was wondering, in the GridLayout my buttons are 5x4 I want the operator buttons a different color then the number keys. http://bestinlifeproductions.angelf...vaquestion.html Here is a link to a picture that best describes what im talking about. I have tried a few things as far as playing around with the gridlayout but with no success. Thanks.
then add this during your button placement to the frame.
button name.setForeground(color);
button name will be the button you want the color of the text to change --- color is the color from above which in this case is red
so if you have lets say buttons called button1, button 2, button 3 then you would code like this
button1.setForeground(color);
(add button to panel)
button2.setForeground(color);
(add button to panel)
button3.setForeground(color);
(add button to panel)
Posts: 13
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Quote:
Originally Posted by Daren
create a variable like this for red letters
COLOR color = new COLOR(255, 0 , 0);
then add this during your button placement to the frame.
button name.setForeground(color);
button name will be the button you want the color of the text to change --- color is the color from above which in this case is red
so if you have lets say buttons called button1, button 2, button 3 then you would code like this
button1.setForeground(color);
(add button to panel)
button2.setForeground(color);
(add button to panel)
button3.setForeground(color);
(add button to panel)
Thanks again Daren. Worked like a charm. It now looks great.
Posts: 11
Time spent in forums: 8 h 35 m 5 sec
Reputation Power: 0
Something I have found that is usually helpful is to Google "Java API". It'll show you the main classes and everything under them. If you can't find what you're looking for, just Google "Java API (the class you're looking for)". If you are looking for the Cursor Class, which is java.awt.Cursor and is under java.lang.object, you could Google "Java API Cursor" and it's the first option.
Posts: 13
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Open/Save text file
Hello again everyone. Since I have this Calculator looking GREAT, I wanted to add some stuff to it. I thought it would be nice to have a place for notes, so I added a TextArea. I created menus for 'SAVE' and 'OPEN'. I would like it to be able to OPEN simple text files and/or SAVE the ones created in my TextArea.
NOW, I have been doing some 'Googling' and looking on the java.sun.com API page and think I have found what I need, Scanner class (java.util.Scanner). The only problem I have is implementing it. Im not sure what 'syntax' is needed and 'where' to put 'said' syntax. I have found quite a bit of examples of its use MOSTLY in a command line. I want to use it in the JOptionPane windows, does it work the same for both? Is this the correct class to use? What would be a good class for OPENING a text file? Thanks for any help.