Possible Duplicate:
paintComponent () never executes on a JFrame
I am using the following code to dispaly two strings and i’m drawing them directly on jfame instead of adding them as component or to a jpanel.But Why am i getting a blank window instead of getting Strings.Where am i wrong?
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class SimpleAttributes extends JFrame{
SimpleAttributes()
{
super("Simple Attributes");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
//setUndecorated(true);
Container cp=this.getContentPane();
cp.setBackground(new Color(0,200,0,0));
setVisible(true);
}
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g.create();
g2.setColor(Color.RED);
g2.drawString("One", 10, 10);
g.drawString("Two", 10,40);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable(){public void run(){new SimpleAttributes();}});
}
}
JFrame is not a component, therefor there’s no
paintComponent()function for it. See the API documentation.