How to clear/ reset a JFrame

巧了我就是萌 提交于 2019-12-22 12:37:20

问题


Me and my friend have decided to work on a card game which cycles between 3 screens (Player1HandScreen, Player2HandScreen and FightScreen).

Once Player1 has chosen their card from Player1HandScreen, Player1HandScreen leads to Player2HandScreen where Player2 does the same. then Player2HandScreen leads to FightScreen where the two cards are compared and one player is declared the winner of that round.

The problem we are having is that once the round ends we want to clear Player1HandScreen as well as the Player2HandScreen once refreshing our parameters we are having trouble updating the GUI successfully.

We have have tried to use removeAll(), but we can't get it to repaint() or validate().

We have also looked into super.paintComponents(g) and updateUI() but both have left us with crushed hopes and dreams.


回答1:


if you remove any JComponent(s) from Container (only once on the end), is needed tell to Container for,

removeAll()//or remove(JComponent)
revalidate();
repaint();



回答2:


Try to clear you Jpanel using:

paper.clearRect(0, 0, (int)panel.getSize().getWidth(), (int)panel.getSize().getHeight

where paper is something like: Graphics paper = panel.getGraphics();



来源:https://stackoverflow.com/questions/6260855/how-to-clear-reset-a-jframe

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