help with setting background color!!!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 11
Reputation: psodhi is an unknown quantity at this point 
Solved Threads: 1
psodhi psodhi is offline Offline
Newbie Poster

help with setting background color!!!

 
0
  #1
May 2nd, 2006
Hi Friends,

i made a window with a Panel and painted a text on it.
Now i want to set a background color for the panel but it does not reflect in the window..

i am posting the code here..
Please let me know if anything is wrong with the code!!

Thanks a lot!!

CODE..!!!

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. class MyPan extends JPanel
  6. {
  7. public void paintComponent(Graphics g)
  8. {
  9. g.drawString("Hello India",50,50);
  10. }
  11. }
  12.  
  13. class MyWin extends JFrame
  14. {
  15. MyWin()
  16. {
  17. MyPan Pan=new MyPan();
  18. Pan.setBackground(Color.DARK_GRAY);
  19.  
  20. Container Con=getContentPane();
  21. Con.add(Pan);
  22.  
  23. setTitle("Window With Panel");
  24. setLocation(100,100);
  25. setSize(300,300);
  26. setResizable(false);
  27. setDefaultCloseOperation(EXIT_ON_CLOSE);
  28. }
  29. }
  30.  
  31. class WinPanel
  32. {
  33. public static void main(String arg[])
  34. {
  35. MyWin w=new MyWin();
  36. w.setVisible(true);
  37. }
  38. }
Last edited by cscgal; May 5th, 2006 at 9:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 55
Reputation: apcxpc is an unknown quantity at this point 
Solved Threads: 0
apcxpc's Avatar
apcxpc apcxpc is offline Offline
Junior Poster in Training

Re: help with setting background color!!!

 
0
  #2
May 2nd, 2006
The problem is in the paintComponent method of your MyPan class.
It should look like this:

  1. public void paintComponent(Graphics g)
  2. {
  3. super.paintComponent(g);
  4. g.drawString("Hello India",50,50);
  5. }

:cheesy:
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: help with setting background color!!!

 
0
  #3
May 2nd, 2006
You should call setBackground() on the container rather than the frame.
Conn.setBackground
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 11
Reputation: psodhi is an unknown quantity at this point 
Solved Threads: 1
psodhi psodhi is offline Offline
Newbie Poster

Re: help with setting background color!!!

 
0
  #4
May 5th, 2006
Thanks a lot apcxpc

Your advice helped me a lot..
today i read the same in a book too!!
Thanks again!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC