RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4553 | Replies: 3
Reply
Join Date: Mar 2006
Posts: 11
Reputation: psodhi is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
psodhi psodhi is offline Offline
Newbie Poster

Help help with setting background color!!!

  #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..!!!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
class MyPan extends JPanel
{
public void paintComponent(Graphics g)
{
g.drawString("Hello India",50,50);
}
}
 
class MyWin extends JFrame
{
MyWin()
{
MyPan Pan=new MyPan();
Pan.setBackground(Color.DARK_GRAY);
 
Container Con=getContentPane();
Con.add(Pan);
 
setTitle("Window With Panel");
setLocation(100,100);
setSize(300,300);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
 
class WinPanel
{
public static void main(String arg[])
{
MyWin w=new MyWin();
w.setVisible(true);
}
}
Last edited by cscgal : May 5th, 2006 at 9:59 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Location: Durban, South Africa
Posts: 55
Reputation: apcxpc is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
apcxpc's Avatar
apcxpc apcxpc is offline Offline
Junior Poster in Training

Troubleshooting Re: help with setting background color!!!

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

public void paintComponent(Graphics g)
{
	super.paintComponent(g);
	g.drawString("Hello India",50,50);
}

:cheesy:
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: help with setting background color!!!

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

Re: help with setting background color!!!

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:42 pm.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC