Java - Screen turns black, when setting a JFrame to Fullscreen

给你一囗甜甜゛ 提交于 2019-12-01 14:09:57
mKorbel

1) you have three general issues

  • never block EDT by using Thread.sleep(5000); use Swing Timer instead, demonstrations here

  • (if aren't there really important reasons) don't mixing AWT with Swing the rest is here, and use JPanel instead of Canvas (for Canvas is there paint method, for JPanel is there paintComponent)

  • your public void paint(Graphics g){ is to the JFrame instead of Canvas and locked by Thread.sleep(5000);

2) Swing GUI rellated should be wrapped into invokeLater() meaning

public static void main(String[] args){

more in the Initial Thread

3) in linked code example you can find out demostrations how to use background thread in the Swing

I agree with mKorbel (actually, I have your code working with corrections he suggest). Just one hint to achieve more predictable results further: take control on colors in paint() method. Default color for background may vary on different systems. On my system it draws white text on light-red background. But if it will draw black text on black background, test will look like "not working".

hey i had he same problem and the screen turns black every time i run the program. in the part of the paint method , you wrote, which i think that it is from Bucky tutorial which is amazing by the way :

public void paint(Graphics g){      
        g.drawString("This is some testtext", 200, 200);
    }

all you have to do is to use "super"

public void paint(Graphics g){
        super.paint(g);
        g.drawString("This is some testtext", 200, 200);
    }

I tried it myself and it is working just fine.

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