drawString prints string over a screenshot of my current window

浪尽此生 提交于 2020-01-06 12:58:35

问题


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 like JPanel 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!