问题
When I run this code in Eclipse I get a the string and line printing in a new windows, but instead of a blank window it simply is a screenshot of my current window, set to the size I declare.
http://i.imgur.com/nWFN9YJ.png
Any suggestions on what I'm doing wrong?
import java.awt.Graphics;
import javax.swing.JFrame;
public class EmptyFrame2 extends JFrame
{
public static void main(String args[])
{
EmptyFrame2 JF=new EmptyFrame2();
JF.setSize(1000,500);
JF.setVisible(true);
}
public void paint(Graphics g)
{
g.drawString("Hello",50,50);
//g.drawLine(50-2, 50+2, 50+70, 50+2);
}
If this is too vague let me know and I'll change it or add whatever else you need.
回答1:
- Avoid overriding
paint
of top level components - Avoid breaking the paint chain, making super you call
super.paintXxx
- Override
paintComponent
of something likeJPanel
instead
See Performing Custom Painting
回答2:
Try adding super.paint(g)
to your paint()
method.
来源:https://stackoverflow.com/questions/21146397/drawstring-prints-string-over-a-screenshot-of-my-current-window