我遇到了这个相当奇怪的问题——单击按钮后,程序会初始化一个新的 JPanel 对象并尝试立即对其进行绘制。在 repaint() 之后,它将打开一个到服务器的套接字并执行一些交互。我遇到的问题是 repaint() 在套接字交互完成之前不会绘制屏幕。这是代码。
创建 GameGUI JPanel 对象
this.startmenu.setVisible(false);
//startmenu is what the user looks at prior to clicking the button
this.game = new GameGUI(this, this.saver,
new Player(this.startmenu.getPlayerDataSelection()), in);
this.game.run();//performs the socket interactions
构造 GameGUI JPanel 对象
this.game = new PlayingFieldGUI();
//this is a panel inside the panel
this.setLayout(new FlowLayout());
//just need game panel and users panel side by side
this.add(this.game);
this.client.add(this);//client is the jpanel that holds this
this.setVisible(true);
this.game.setVisible(true);
this.client.repaint();//attempting to repaint
该run()
函数为GameGUI
. 完成套接字调用后,它会正确绘制背景。如果我要在交互过程中终止与套接字交互的服务器,则会发生异常抛出以及应该在 GameGUI 构造中发生的绘制。