944,168 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 32953
  • Java RSS
May 2nd, 2006
0

help with setting background color!!!

Expand Post »
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..!!!

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
psodhi is offline Offline
11 posts
since Mar 2006
May 2nd, 2006
0

Re: help with setting background color!!!

The problem is in the paintComponent method of your MyPan class.
It should look like this:

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

:cheesy:
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
apcxpc is offline Offline
55 posts
since Sep 2004
May 2nd, 2006
0

Re: help with setting background color!!!

You should call setBackground() on the container rather than the frame.
Conn.setBackground
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
May 5th, 2006
0

Re: help with setting background color!!!

Thanks a lot apcxpc

Your advice helped me a lot..
today i read the same in a book too!!
Thanks again!!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
psodhi is offline Offline
11 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Final GUI JFrame problem with actionlistner method
Next Thread in Java Forum Timeline: Need help tweaking my program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC