我正在用 Java 开发游戏。在服务器端,在将信息发送给客户端之前,我在一个地方更改了 gui。当我在没有网络部分(代码中的标记 1)的情况下运行它时,它运行良好。但是当我做网络部分时(注意它是在 gui 部分之后完成的,我也编码了一个 5 秒的睡眠线程部分), gui挂了。
这是我们代码的一小段:
public void startgame()
{
Main.setx=Start.setx;
Main.sety=Start.sety;
gui1 = new Main(); // Bind it
gui1.setOpaque(true);
container.add(gui1);
container.remove(hostpanel);
container.setSize(Start.setx,Start.sety);
container.setPreferredSize(new Dimension(Start.setx,Start.sety));
container.setVisible(true);
container.repaint();
gui=(Main)gui1; // 1
try {
System.out.println("Thread sleeping");
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread awake");
if(vector.get(0)!=null)
{
pl1=new Human_player(vector.get(0));
pl1.send(1);
}
else
{
}
if(vector.get(1)!=null)
{
pl2=new Human_player(vector.get(1));
pl2.send(2);
}
else
{
}
if(vector.get(2)!=null)
{
pl3=new Human_player(vector.get(2));
pl3.send(3);
}
else
{
}
ArrayList<Integer> cards=new ArrayList<Integer>();
int i;
for(i=0;i<=51;i++)
{
cards.add(i,i);
}
Collections.shuffle(cards);
table=new Table(cards.get(0),cards.get(1),cards.get(2),cards.get(3),4);
Vector<Integer> temp=new Vector<Integer>();
for(i=4;i<16;i++)
{
temp.add(cards.get(i));
}
pl1.set_cards(temp);
if(pl1.type=="human")
{
pl1.send(temp);
pl1.send(table.heaps.get(0).get(0));
pl1.send(table.heaps.get(1).get(0));
pl1.send(table.heaps.get(2).get(0));
pl1.send(table.heaps.get(3).get(0));
}
temp.clear();
for(i=16;i<28;i++)
{
temp.add(cards.get(i));
}
pl2.set_cards(temp);
if(pl2.type=="human")
{
pl2.send(temp);
pl2.send(table.heaps.get(0).get(0));
pl2.send(table.heaps.get(1).get(0));
pl2.send(table.heaps.get(2).get(0));
pl2.send(table.heaps.get(3).get(0));
}
System.out.println("Making Initial arrangements");
temp.clear();
for(i=28;i<40;i++)
{
temp.add(cards.get(i));
}
System.out.println("Making Initial arrangements");
pl3.set_cards(temp);
if(pl3.type=="human")
{
System.out.println("Making Initial arrangements");
pl3.send(temp);
pl3.send(table.heaps.get(0).get(0));
pl3.send(table.heaps.get(1).get(0));
pl3.send(table.heaps.get(2).get(0));
pl3.send(table.heaps.get(3).get(0));
System.out.println("Initial arrangement made");
}
for(i=40;i<52;i++)
{
mycards.add(cards.get(i));
}
playgame();
}