JFrame issue

Alright, so I’m a beginner in Java programming and I’m not quite sure what do about this problem I’m having. So I hope someone will be able to help. Anyways, when I make a Jframe, it seems to be working properly.

As you can see, the Jframe appears and has its default grey background. You may also notice


         public void paint(Graphics g){
		g.drawString("Testing", 320, 240);
	}

is commented out.

When it isn’t commented out and I run it, the Jframe loses it’s background color, becomes transparent. Then freezes with what ever it was over as the background, but as you can see it still draws my string “Testing” (red box around it).

Alright, so I commented out drawString and it goes transparent, but does not freeze a background.

Any suggestions?

A JFrame and other Component’s do not automatically refresh unless you tell them to, or some event like resizing the window causes them to. Right now you’re creating your frame but never having it repaint so nothing gets updated. Assumedly showing the text the first time causes a repaint or something, not sure about that. Try doing this:


javax.swing.Timer timer = new javax.swing.Timer(1000 / 60.0f, new javax.swing.event.ActionListener() {public void ActionPerformed(javax.swing.event.ActionEvent e) {frame.repaint();}}}

PS I don’t remember if that’s the exact right syntax. The key is just call repaint() on your Frame.

I see, thanks for the reply bud. I appreciate it.

Eli, no that is not the reason. The reason is that he does not call super.paint(g) inside paint(Graphics).

Oh yes, duh. Thank you for pointing that out. :slight_smile: Still, he needs to repaint too.

You might want to pack(), and setVisible(true)

Mads, no point for pack() since he hasn’t added any components. And he already has used setVisible(true). Look closer at the constructor :wink: