简体   繁体   English

如何清除/重置 JFrame

[英]How to clear/ reset a JFrame

Me and my friend have decided to work on a card game which cycles between 3 screens ( Player1HandScreen, Player2HandScreen and FightScreen ).我和我的朋友决定开发一款在 3 个屏幕( Player1HandScreen、Player2HandScreen 和 FightScreen )之间循环的纸牌游戏。

Once Player1 has chosen their card from Player1HandScreen , Player1HandScreen leads to Player2HandScreen where Player2 does the same.一旦 Player1 从Player1HandScreen中选择了他们的卡, Player1HandScreen导致Player2HandScreen ,其中 Player2 执行相同的操作。 then Player2HandScreen leads to FightScreen where the two cards are compared and one player is declared the winner of that round.然后Player2HandScreen导致FightScreen比较两张牌,其中一名玩家被宣布为该轮的获胜者。

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.我们遇到的问题是,一旦回合结束,我们想要清除Player1HandScreen以及Player2HandScreen ,一旦刷新我们的参数,我们就无法成功更新 GUI。

We have have tried to use removeAll() , but we can't get it to repaint() or validate() .我们曾尝试使用removeAll() ,但无法将其用于repaint()validate()

We have also looked into super.paintComponents(g) and updateUI() but both have left us with crushed hopes and dreams.我们还研究了super.paintComponents(g)updateUI()但两者都给我们留下了破碎的希望和梦想。

if you remove any JComponent(s) from Container (only once on the end), is needed tell to Container for,如果您从 Container 中删除任何 JComponent(s)(最后仅一次),则需要告知 Container,

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

Try to clear you Jpanel using:尝试使用以下命令清除 Jpanel:

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

where paper is something like: Graphics paper = panel.getGraphics();其中纸张类似于: Graphics paper = panel.getGraphics();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM