http://download.csdn.net/source/2030968这是源程序,不要资源分的麻烦帮忙看看哈
各位大侠们,是酱紫的,我写了一个局域网下的石头剪子布小程序,为了方便大家理解我简单解释一下:
MainJFrame.java是主界面
Server.java是建立服务器线程
Client.java是客户端线程
用socket连接,其通信方式是ObjectOutputStream和ObjectInputStream,传输数据为Message类。
下面是
mainjframe在建立server时将重要的几个按钮这样传过去:
Server server = new Server(port, this.clothjButton, this.stonejButton, this.mainjTextArea, this.scissorsjButton, this.exitjButton);这几个按钮
public Server(int port, JButton clothjButton, JButton stonejButton, JTextArea mainjTextArea, JButton scissorsjButton, JButton exitjButton) {
this.port = port;
this.clothjButton = clothjButton;
this.scissorsjButton = scissorsjButton;
this.mainjTextArea = mainjTextArea;
this.stonejButton = stonejButton;
this.exitjButton = exitjButton;
}之后,在其中一个按钮的监听是这样的:
protected void clothjButton1ActionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
this.clothjButton.setSelected(true);
this.stonejButton.setEnabled(false);
this.scissorsjButton.setEnabled(false);
this.mainjTextArea.setText("你出了 -----------布! 等待对方出拳!");
Repaint();
try {
Message mess = (Message) this.obinStream.readObject();
if (mess.control == Message.Control.cloth) {
oboutStream.writeObject(new Message(Message.Control.equal));
this.clothjButton.setSelected(false);
this.stonejButton.setEnabled(true);
this.scissorsjButton.setEnabled(true);
this.mainjTextArea.setText("对方也出了 -------布! 平手了~请继续出拳:");
Repaint();
}
if (mess.control == Message.Control.scissors) {
oboutStream.writeObject(new Message(Message.Control.win));
this.clothjButton.setSelected(false);
this.stonejButton.setEnabled(true);
this.scissorsjButton.setEnabled(true);
this.mainjTextArea.setText("对方出了 -------剪刀! 你输了!请继续出拳:");
Repaint();
}
if (mess.control == Message.Control.stone) {
oboutStream.writeObject(new Message(Message.Control.lose));
this.clothjButton.setSelected(false);
this.stonejButton.setEnabled(true);
this.scissorsjButton.setEnabled(true);
this.mainjTextArea.setText("对方出了 -------石头! 你赢了!请继续出拳:");
Repaint();
} } catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("输出错误");
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("类型无法转换");
e.printStackTrace();
} }现在的问题是每次按下按钮后感觉这几行代码根本没执行过:
this.clothjButton.setSelected(true);
this.stonejButton.setEnabled(false);
this.scissorsjButton.setEnabled(false);
this.mainjTextArea.setText("你出了 -----------布! 等待对方出拳!");状况是按下按钮后按钮保持为被按下状态,其他按钮根本没变化整个程序就不动了,但你要是点按钮系统会记录你的操作,但表面上没有任何反应,等另一方相应了本次操作之后程序又活了,就是说保证了顺序上的正确但是没有有效的锁死按钮,我设置的锁死过程都不能用……大家帮我看看为啥啊?源程序我
各位大侠们,是酱紫的,我写了一个局域网下的石头剪子布小程序,为了方便大家理解我简单解释一下:
MainJFrame.java是主界面
Server.java是建立服务器线程
Client.java是客户端线程
用socket连接,其通信方式是ObjectOutputStream和ObjectInputStream,传输数据为Message类。
下面是
mainjframe在建立server时将重要的几个按钮这样传过去:
Server server = new Server(port, this.clothjButton, this.stonejButton, this.mainjTextArea, this.scissorsjButton, this.exitjButton);这几个按钮
public Server(int port, JButton clothjButton, JButton stonejButton, JTextArea mainjTextArea, JButton scissorsjButton, JButton exitjButton) {
this.port = port;
this.clothjButton = clothjButton;
this.scissorsjButton = scissorsjButton;
this.mainjTextArea = mainjTextArea;
this.stonejButton = stonejButton;
this.exitjButton = exitjButton;
}之后,在其中一个按钮的监听是这样的:
protected void clothjButton1ActionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
this.clothjButton.setSelected(true);
this.stonejButton.setEnabled(false);
this.scissorsjButton.setEnabled(false);
this.mainjTextArea.setText("你出了 -----------布! 等待对方出拳!");
Repaint();
try {
Message mess = (Message) this.obinStream.readObject();
if (mess.control == Message.Control.cloth) {
oboutStream.writeObject(new Message(Message.Control.equal));
this.clothjButton.setSelected(false);
this.stonejButton.setEnabled(true);
this.scissorsjButton.setEnabled(true);
this.mainjTextArea.setText("对方也出了 -------布! 平手了~请继续出拳:");
Repaint();
}
if (mess.control == Message.Control.scissors) {
oboutStream.writeObject(new Message(Message.Control.win));
this.clothjButton.setSelected(false);
this.stonejButton.setEnabled(true);
this.scissorsjButton.setEnabled(true);
this.mainjTextArea.setText("对方出了 -------剪刀! 你输了!请继续出拳:");
Repaint();
}
if (mess.control == Message.Control.stone) {
oboutStream.writeObject(new Message(Message.Control.lose));
this.clothjButton.setSelected(false);
this.stonejButton.setEnabled(true);
this.scissorsjButton.setEnabled(true);
this.mainjTextArea.setText("对方出了 -------石头! 你赢了!请继续出拳:");
Repaint();
} } catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("输出错误");
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("类型无法转换");
e.printStackTrace();
} }现在的问题是每次按下按钮后感觉这几行代码根本没执行过:
this.clothjButton.setSelected(true);
this.stonejButton.setEnabled(false);
this.scissorsjButton.setEnabled(false);
this.mainjTextArea.setText("你出了 -----------布! 等待对方出拳!");状况是按下按钮后按钮保持为被按下状态,其他按钮根本没变化整个程序就不动了,但你要是点按钮系统会记录你的操作,但表面上没有任何反应,等另一方相应了本次操作之后程序又活了,就是说保证了顺序上的正确但是没有有效的锁死按钮,我设置的锁死过程都不能用……大家帮我看看为啥啊?源程序我
Message mess = (Message) this.obinStream.readObject();
这句上去了,你可以加个断点测试下我的正确与否
一般不要用socket直接这样操作,因为缓存的问题,你在写数据后并没有立即发送出去,同样可以因为缓存的问题在read的时候不能立即读到数据。为了证明我的观点你可以在write的时候连续write上n多次试试,此时readObject肯定能返回
一般在网络通信的时候很少用带缓冲的IO,切忌!
this.clothjButton.setSelected(true);
this.stonejButton.setEnabled(false);
this.scissorsjButton.setEnabled(false);
this.mainjTextArea.setText("你出了 -----------布! 等待对方出拳!");
Repaint();
根本没有反应,这是为什么呢??
应该用一个新的线程处理this.obinStream.readObject();
那样的话你试一下把界面更新的操作都放到SwingUtilities.invokeLater中。SwingUtilities.invokeLater(new Runnable(){
public void run(){
this.stonejButton.setEnabled(false);
this.scissorsjButton.setEnabled(false);
this.mainjTextArea.setText("你出了 -----------布! 等待对方出拳!");
}
});