Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 10th, 2009, 10:51 PM
jaysonjohnson jaysonjohnson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 13 jaysonjohnson New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Question 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.

-Jay

Reply With Quote
  #2  
Old November 10th, 2009, 11:25 PM
gimp's Avatar
gimp gimp is offline
<?PHP user_title("gimp"); ?>
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jan 2005
Location: Internet
Posts: 7,652 gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 2540 Folding Title: Novice Folder
Time spent in forums: 3 Months 6 Days 1 h 35 m 4 sec
Reputation Power: 6083
Send a message via AIM to 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?
__________________
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.

Reply With Quote
  #3  
Old November 11th, 2009, 08:11 AM
jaysonjohnson jaysonjohnson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 13 jaysonjohnson New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Angry 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.

-Jay

Reply With Quote
  #4  
Old November 11th, 2009, 09:40 AM
Daren Daren is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 119 Daren Negative: is most likely a SPAMMER and a traitor to the cause. 
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);

Reply With Quote
  #5  
Old November 11th, 2009, 01:00 PM
jaysonjohnson jaysonjohnson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 13 jaysonjohnson New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Red face 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.

java Code:
Original - java Code
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.datatransfer.*;
  5. import java.text.DecimalFormat;
  6. import javax.swing.JOptionPane;
  7.  
  8. public class Calculator extends Frame implements ActionListener
  9. {   //Declares Private Variables
  10.     private Button keys[];
  11.     private Panel keypad;
  12.     private TextField lcd;
  13.     private double op1;
  14.     private boolean first;
  15.     private boolean foundKey;
  16.     private boolean clearText;
  17.     private int last0p;
  18.     private DecimalFormat calcPattern;
  19.  
  20.     public Calculator()
  21.     {
  22.         //create an instance of the menu
  23.         MenuBar mnuBar = new MenuBar();
  24.         setMenuBar(mnuBar);
  25.  
  26.         //construct and populate the File menu
  27.         Menu mnuFile = new Menu("File", true);
  28.         mnuBar.add(mnuFile);
  29.             MenuItem mnuFileExit = new MenuItem("Exit");
  30.             mnuFile.add(mnuFileExit);
  31.  
  32.         //construct and populate the Edit menu
  33.         Menu mnuEdit = new Menu("Edit", true);
  34.         mnuBar.add(mnuEdit);
  35.             MenuItem mnuEditClear = new MenuItem("Clear");
  36.             mnuEdit.add(mnuEditClear);
  37.             mnuEdit.insertSeparator(1);
  38.             MenuItem mnuEditCopy = new MenuItem("Copy");
  39.             mnuEdit.add(mnuEditCopy);
  40.             MenuItem mnuEditPaste = new MenuItem("Paste");
  41.             mnuEdit.add(mnuEditPaste);
  42.  
  43.         //construct and populate the About Menu
  44.         Menu mnuAbout = new Menu("About", true);
  45.             mnuBar.add(mnuAbout);
  46.             MenuItem mnuAboutCalculator = new MenuItem("About Calculator");
  47.             mnuAbout.add(mnuAboutCalculator);
  48.  
  49.         //add the ActionListener to each menu item
  50.         mnuFileExit.addActionListener(this);
  51.         mnuEditClear.addActionListener(this);
  52.         mnuEditCopy.addActionListener(this);
  53.         mnuEditPaste.addActionListener(this);
  54.         mnuAboutCalculator.addActionListener(this);
  55.  
  56.         //assign an ActionCommand to each menu item
  57.         mnuFileExit.setActionCommand("Exit");
  58.         mnuEditClear.setActionCommand("Clear");
  59.         mnuEditCopy.setActionCommand("Copy");
  60.         mnuEditPaste.setActionCommand("Paste");
  61.         mnuAboutCalculator.setActionCommand("About");
  62.  
  63.         //construct components and initialize beginning values
  64.         lcd = new TextField(20);
  65.             lcd.setEditable(false);
  66.         keypad = new Panel();
  67.         keys = new Button[20];
  68.         first = true;
  69.         op1 = 0.0;
  70.         clearText = true;
  71.         last0p = 0;
  72.         calcPattern = new DecimalFormat("########.########");
  73.  
  74.         //construct and assign captions to the Buttons
  75.         for (int i=0; i<=9; i++)
  76.             keys[i] = new Button(String.valueOf(i));
  77.  
  78.         keys[10] = new Button("/");
  79.         keys[11] = new Button("*");
  80.         keys[12] = new Button("-");
  81.         keys[13] = new Button("+");
  82.         keys[14] = new Button("=");
  83.         keys[15] = new Button(".");
  84.         keys[16] = new Button("M+");
  85.         keys[17] = new Button("MR");
  86.         keys[18] = new Button("Backspace");
  87.         keys[19] = new Button("Clear");
  88.  
  89.         //sets Frame and keypad layout to grid layout
  90.         setLayout(new BorderLayout());
  91.         keypad.setLayout(new GridLayout(5,5,10,10));
  92.  
  93.         for (int i=7; i<=10; i++)   //7, 8, 9, divide - Adds the first row of the grid
  94.             keypad.add(keys[i]);    //Adds the first row to the frame
  95.  
  96.         for (int i=4; i<=6; i++)    //4, 5, 6 - adds the second row of the grid
  97.             keypad.add(keys[i]);    //Adds the second row to the frame
  98.  
  99.         keypad.add(keys[11]);   //Adds the multiply button to the second row
  100.  
  101.         for (int i=1; i<=3; i++)    //Adds 1, 2, 3 or the third row of the grid
  102.             keypad.add(keys[i]);    //Adds the third row to the frame
  103.  
  104.         keypad.add(keys[12]);   //Adds the Subtract button to the third row
  105.  
  106.         keypad.add(keys[0]);    //Adds the "0" key to fourth and final row
  107.  
  108.         for (int i=15; i>=13; i--)  //Adds the '.', '=', and '+' to the fourth row
  109.             keypad.add(keys[i]);    //Adds the Fourth row to the frame
  110.  
  111.         for (int i=19; i>=16; i--)  //Adds Copy, Paste, Backspace, and clear buttons
  112.             keypad.add(keys[i]);    //Adds the Fifth/Final row to the frame
  113.  
  114.         for (int i=0; i<keys.length; i++)
  115.             keys[i].addActionListener(this);
  116.  
  117.         add(lcd, BorderLayout.NORTH);
  118.         add(keypad, BorderLayout.CENTER);
  119.  
  120.         addWindowListener(
  121.                 new WindowAdapter()
  122.                 {
  123.                     public void windowClosing(WindowEvent e)
  124.                     {
  125.                         System.exit(0);
  126.                     }
  127.                 }
  128.             );
  129.  
  130.     }   //End of the constructor Method
  131.  
  132.             public void actionPerformed(ActionEvent e)
  133.             {
  134.                 //test for menu item clicks
  135.                 String arg = e.getActionCommand();
  136.                 if (arg == "Exit")
  137.                     System.exit(0);
  138.  
  139.                 if (arg == "Clear")
  140.                 {
  141.                     clearText = true;
  142.                     first = true;
  143.                     op1 = 0.0;
  144.                     lcd.setText("");
  145.                     lcd.requestFocus();
  146.                 }
  147.  
  148.                 if (arg == "Copy")
  149.                 {
  150.                     Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
  151.                     StringSelection contents = new StringSelection(lcd.getText());
  152.                     cb.setContents(contents, null);
  153.                 }
  154.  
  155.                 if (arg == "Paste")
  156.                 {
  157.                     Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
  158.                     Transferable content = cb.getContents(this);
  159.                     try
  160.                     {
  161.                         String s = (String)content.getTransferData(DataFlavor.stringFlavor);
  162.                         lcd.setText(calcPattern.format(Double.parseDouble(s)));
  163.                     }
  164.                     catch (Throwable exc)
  165.                     {
  166.                         lcd.setText("");
  167.                     }
  168.                 }
  169.  
  170.             if (arg == "About")
  171.             {
  172.                 String message = "Calculator ver. 1.0\n Basic Software\nCopyright 2009\nAll rights reserved";
  173.                 JOptionPane.showMessageDialog(null, message, "About Calculator", JOptionPane.INFORMATION_MESSAGE);
  174.             }
  175.  
  176.             //test for button clicks
  177.             foundKey = false;
  178.  
  179.             //search for the clicked key
  180.             for (int i=0; i<keys.length && !foundKey; i++)
  181.             {
  182.                 if(e.getSource() == keys[i])
  183.                 {
  184.                     foundKey = true;
  185.                     switch(i)
  186.                     {
  187.                         //number and decimal point buttons
  188.                         case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 15:    //# buttons 0-9 and button 15 which is the '.' colon
  189.                             if(clearText)
  190.                             {
  191.                                 lcd.setText("");
  192.                                 clearText = false;
  193.                             }
  194.                             lcd.setText(lcd.getText() + keys[i].getLabel());
  195.                             break;
  196.  
  197.                             //operator buttons
  198.                             case 10: case 11: case 12: case 13: case 14:    //(+ - / * =) buttons
  199.                                 clearText = true;
  200.  
  201.                                 if (first)
  202.                                 {
  203.                                     if(lcd.getText().length()==0) op1 = 0.0;
  204.                                     else op1 = Double.parseDouble(lcd.getText());
  205.  
  206.                                     first = false;
  207.                                     clearText = true;
  208.                                     last0p = i;
  209.                                 }
  210.                                 else
  211.                                 {
  212.                                     switch(last0p)
  213.                                     {
  214.                                         case 10:    //divide button
  215.                                             op1 /= Double.parseDouble(lcd.getText());
  216.                                             break;
  217.                                         case 11:    //multiply button
  218.                                             op1 *= Double.parseDouble(lcd.getText());
  219.                                             break;
  220.                                         case 12:    //minus button
  221.                                             op1 -= Double.parseDouble(lcd.getText());
  222.                                             break;
  223.                                         case 13:    //plus button
  224.                                             op1 += Double.parseDouble(lcd.getText());
  225.                                             break;
  226.                                     }   //end of switch (Open-220)
  227.                                     lcd.setText(calcPattern.format(op1));
  228.                                     clearText = true;
  229.  
  230.                                     if(i==14) first = true//Equals button.
  231.                                     else last0p = i;
  232.                                 }   //ends else (Open-218)
  233.                                 break//Break (Open-234)
  234.  
  235.                                 case 16:    //Add to memory Button (M+)
  236.                                 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
  237.                                 StringSelection contents = new StringSelection(lcd.getText());
  238.                                 cb.setContents(contents, null);
  239.                                 break;
  240.  
  241.                                 case 17:    //Memory Recall Button (MR)
  242.                                 Clipboard cb2 = Toolkit.getDefaultToolkit().getSystemClipboard();
  243.                                 Transferable content = cb2.getContents(this);
  244.                                 try
  245.                                 {
  246.                                     String s = (String)content.getTransferData(DataFlavor.stringFlavor);
  247.                                     lcd.setText(calcPattern.format(Double.parseDouble(s)));
  248.                                 }
  249.                                 catch (Throwable exc)
  250.                                 {
  251.                                     lcd.setText("");
  252.                                 }
  253.                                 break;
  254.  
  255.                                 case 18:    //Backspace Button
  256.                                 lcd.setText(lcd.getText().substring(0,lcd.getText().length()-1));
  257.                                 break;
  258.  
  259.                                 case 19:    //Clear screen button
  260.                                 clearText = true;
  261.                                 lcd.setText("");
  262.                                 lcd.requestFocus();
  263.                                 break;
  264.  
  265.  
  266.                         }   //end of the switch(i) statement (Open-186)
  267.                 }   //end of if statement (Open-183)
  268.         }   //end of for statement (Open-181)
  269.     }//end of actionPerformed (Open-133)
  270.  
  271.     public static void main(String args[])
  272.     {
  273.         //set frame properties
  274.         Calculator f = new Calculator();
  275.         f.setTitle("  Basic Calculator Application");
  276.         f.setBounds(200,200,300,300);
  277.         f.setVisible(true);
  278.  
  279.         //set image properties and add to frame
  280.         Image icon = Toolkit.getDefaultToolkit().getImage("jj.jpg");
  281.         f.setIconImage(icon);
  282.  
  283.     }   //end of main() method (Open-240)
  284. }   //end of Calculator class (Open-16)
  285.  

-Jay

Reply With Quote
  #6  
Old November 11th, 2009, 01:40 PM
gimp's Avatar
gimp gimp is offline
<?PHP user_title("gimp"); ?>
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jan 2005
Location: Internet
Posts: 7,652 gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)gimp User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 2540 Folding Title: Novice Folder
Time spent in forums: 3 Months 6 Days 1 h 35 m 4 sec
Reputation Power: 6083
Send a message via AIM to 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.

Reply With Quote
  #7  
Old November 11th, 2009, 03:28 PM
jaysonjohnson jaysonjohnson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 13 jaysonjohnson New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Wink 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.

-Jay
Comments on this post
gimp disagrees: (-10) Programming ability doesn't matter. What annoys me is that you don't know how to think, how to
search to fix your problem. Also, learn to spell.

Reply With Quote
  #8  
Old November 11th, 2009, 04:29 PM
Daren Daren is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 119 Daren Negative: is most likely a SPAMMER and a traitor to the cause. 
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.

Reply With Quote
  #9  
Old November 11th, 2009, 05:51 PM
jaysonjohnson jaysonjohnson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 13 jaysonjohnson New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Question 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.

java Code:
Original - java Code
  1.  
  2. lcd = new TextField(20);
  3.             lcd.setFont(new Font("Dialog", Font.BOLD, 30));
  4.             lcd.setEditable(false);
  5.         keypad = new Panel();
  6.             keypad.setForeground(Color.red);
  7.             keypad.setFont(new Font("Dialog", Font.BOLD, 22));


-Jay

Reply With Quote
  #10  
Old November 11th, 2009, 07:46 PM
Daren Daren is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 119 Daren Negative: is most likely a SPAMMER and a traitor to the cause. 
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.

java Code:
Original - java Code
  1.  
  2. lcd = new TextField(20);
  3.             lcd.setFont(new Font("Dialog", Font.BOLD, 30));
  4.             lcd.setEditable(false);
  5.         keypad = new Panel();
  6.             keypad.setForeground(Color.red);
  7.             keypad.setFont(new Font("Dialog", Font.BOLD, 22));


-Jay


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)

Reply With Quote
  #11  
Old November 11th, 2009, 09:22 PM
jaysonjohnson jaysonjohnson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 13 jaysonjohnson New User: is a brand new recruit and a unknown entity at this point. 
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.

-Jay

Reply With Quote
  #12  
Old November 12th, 2009, 06:08 PM
Daren Daren is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 119 Daren Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 13 h 52 m 11 sec
Reputation Power: 0
Quote:
Originally Posted by jaysonjohnson
Thanks again Daren. Worked like a charm. It now looks great.

-Jay


any time

Reply With Quote
  #13  
Old November 12th, 2009, 06:37 PM
wythe25 wythe25 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Location: Fairview, NC
Posts: 11 wythe25 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #14  
Old November 13th, 2009, 03:54 PM
jaysonjohnson jaysonjohnson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 13 jaysonjohnson New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 6 h 42 m 33 sec
Reputation Power: 0
Question 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.

-Jay

Reply With Quote
  #15  
Old November 13th, 2009, 06:12 PM
Daren Daren is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 119 Daren Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 13 h 52 m 11 sec
Reputation Power: 0
create a phone list example

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class createPhoneList extends JFrame implements ActionListener //make the class
{
	private JLabel headTitle = new JLabel("Phone List");
	private Font headFont = new Font("Mistral", Font.BOLD, 24);
	private JLabel enterName = new JLabel("Please enter a name");
	private JLabel name = new JLabel("Name\n");
	private JTextField inputName = new JTextField(10);
	private JLabel phone = new JLabel("Phone Number");
	private JTextField inputPhone = new JTextField(10);
	private JButton saveData = new JButton("Save");
	private Container con = getContentPane();
	DataOutputStream outstream;
	
	public createPhoneList()
		{
			super("Create Phone List");
			
			try
			{
				outstream = new DataOutputStream(new FileOutputStream("PhoneList.dat"));
			}
			catch(IOException ex)
			{
				System.err.println("File not opened");
				System.exit(1);
			}
			
			setSize(150,250);
			con.setLayout(new FlowLayout());
			headTitle.setFont(headFont);
			con.add(headTitle);
			con.add(enterName);
			con.add(name);
			con.add(inputName);
			con.add(phone);
			con.add(inputPhone);
			con.add(saveData);
			saveData.addActionListener(this);
			setVisible(true);
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
		}
			
			public void actionPerformed(ActionEvent e1)
			{
				try
				{
					outstream.writeUTF(inputName.getText());
					outstream.writeUTF(inputPhone.getText());
					
					inputName.setText("");
					inputPhone.setText("");
				}
				catch(IOException e2)
				{
					System.err.println("Error writing file");
					System.exit(1);
				}
			}	
			public static void main(String[] args)
			{
				createPhoneList cpf = new createPhoneList();
			}
}  





simple reading of a phone list that you created above

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class readPhoneList extends JFrame implements ActionListener //make the class
{
	private JLabel headTitle = new JLabel("Phone List");
	private Font headFont = new Font("Mistral", Font.BOLD, 24);
	private JLabel enterName = new JLabel("Click view to see a name");
	private JLabel name = new JLabel("Name");
	private JTextField inputName = new JTextField(10);
	private JLabel phone = new JLabel("Phone Number");
	private JTextField inputPhone = new JTextField(10);
	private JButton openData = new JButton("View");
	private Container con = getContentPane();
	DataInputStream instream;
	
	public readPhoneList()
		{
			super("Create Phone List");
			
			try
			{
				instream = new DataInputStream(new FileInputStream("PhoneList.dat"));
			}
			catch(IOException ex)
			{
				System.err.println("File not opened");
				System.exit(1);
			}
			
			setSize(160,250);
			con.setLayout(new FlowLayout());
			headTitle.setFont(headFont);
			con.add(headTitle);
			con.add(enterName);
			con.add(name);
			con.add(inputName);
			con.add(phone);
			con.add(inputPhone);
			con.add(openData);
			openData.addActionListener(this);
			setVisible(true);
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
		}
			
			public void actionPerformed(ActionEvent e1)
			{
				String theName, theNumber;
				try
				{
					theName = instream.readUTF();
					theNumber = instream.readUTF();
								
					inputName.setText(theName);
					inputPhone.setText(theNumber);
				}
				catch(EOFException e2)
				{
					closeFile();
				}
				catch(IOException e3)
				{
					System.err.println("Error reading file");
					System.exit(1);
				}
			}	
			public void closeFile()
			{
				try
				{
					instream.close();
					System.exit(0);
				}
				catch(IOException e)
				{
					System.err.println("Error reading file");
					System.exit(1);
				}
			}
			public static void main(String[] args)
			{
				readPhoneList cpf = new readPhoneList();
			}
}  



hope this helps

Last edited by Daren : November 13th, 2009 at 06:16 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > GUI Calculator - How do I change the text color & set a background image?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap