怎么弄都不行,想的方法行,但是没效果。
还没写玩,网络通讯用一个chess类实现。帮忙看看:
服务器端:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
class chess implements Serializable
{
int x;
int y;
int c;

public chess(int x,int y,int z)
{
x=x;
y=y;
c=z;
}

}
class JPanels extends JPanel implements MouseListener
{
 public chess c1;
 public chess c2;
 ObjectOutputStream out;
 ObjectInputStream in;
 Socket socket;
 ServerSocket server;
 public JPanels ()
 { }
 public void startserver()
 {
  this.addMouseListener(this);
  try
  {
  server=new ServerSocket(7777);
  socket=server.accept();
  in=new ObjectInputStream(socket.getInputStream());
  out=new ObjectOutputStream(socket.getOutputStream());
  getInfo();
  }
 catch(Exception e)
 {
System.out.println(" 1");
 }
  /*while(true)
  { 
  try
  {
    c2=(chess)in.readObject();
   draw(c2.x,c2.y,c2.c);
  }
  catch(Exception e)
  {
     System.out.println("no client");
  }
  
  }*/
 
 }public void getInfo()
{
try{
   while(true)
  { 
    c2=(chess)in.readObject();
   draw(c2.x,c2.y,c2.c);
  }
  }
  catch(Exception e)
  {
     System.out.println("no client");
  }
  
  
}
 protected void paintComponent(Graphics g)
 {
  super.paintComponent(g);
  for(int i=0;i<15;i++)
  {
  g.drawLine(20,(i+1)*20,300,(i+1)*20);
        g.drawLine((i+1)*20,20,(i+1)*20,300);
  }
 }
public void  draw(int x,int y,int z)
 {  
    Graphics g1=this.getGraphics();
    if(z==1)
  g1.setColor(Color.BLUE);
  else if(z==2)
  g1.setColor(Color.red);   
  g1.drawOval(x-5,y-5,10,10);
  g1.fillOval(x-5,y-5,10,10);
  g1.dispose();
 }
public void mouseClicked(MouseEvent e)
 {
   int x=e.getX()/20;
   int y=e.getY()/20;
   draw(x*20,y*20,2);
   c1=new chess(x,y,2);
   try
   {
   out.writeObject(c1);
   }
   catch(Exception e1)
   {
System.out.println(" no client");
   } }
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
public class Netserver extends JFrame
{
JPanels p=new JPanels();
public Netserver()
{
getContentPane().add(p);
}
public static void main(String[] args)
{
    Netserver net1=new Netserver();
    net1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        net1.setTitle("Server");
        net1.setSize(400, 400);
        net1.setVisible(true);
        net1.p.startserver();
}

}
客户端:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
class chess implements Serializable//表示坐标
{
int x;
int y;
int c;

public  chess(int x,int y,int z)
{
x=x;
y=y;
c=z;
}

}
class JPanels extends JPanel implements MouseListener//
{
 public chess c1;
 public chess c2;
 private ObjectOutputStream out;
 private ObjectInputStream in;
 private Socket socket;
 public JPanels ()
 {}
 public void startserver()
 {
 this.addMouseListener(this);
 try
 {
 socket=new Socket("127.0.1",7777);
 in=new ObjectInputStream(socket.getInputStream());
 out=new ObjectOutputStream(socket.getOutputStream());
 }
 catch(Exception e)
 {
System.out.println("1");
  }
 }
 public void getInfo()
 {
 try
 {
 while(true)
 {  
 c2=(chess)in.readObject();
 draw(c2.x,c2.y,c2.c);
 }
 }
 catch(Exception e)
    {
     System.out.println("no server");
    }
 }
 protected void paintComponent(Graphics g)
 {
  super.paintComponent(g);
  for(int i=0;i<15;i++)
  {
  g.drawLine(20,(i+1)*20,300,(i+1)*20);
        g.drawLine((i+1)*20,20,(i+1)*20,300);
  }
 }
public void  draw(int x,int y,int z)
 {  
    Graphics g1=this.getGraphics();
    if(z==1)
  g1.setColor(Color.BLUE);
  else if(z==2)
  g1.setColor(Color.red);   
  g1.drawOval(x-5,y-5,10,10);
  g1.fillOval(x-5,y-5,10,10);
  g1.dispose();
 }
public void mouseClicked(MouseEvent e)
 {
   int x=e.getX()/20;
   int y=e.getY()/20;
   draw(x*20,y*20,1);
   c1=new chess(x,y,1);
   try
   {
   out.writeObject(c1);
   }
   catch(Exception e1)
   {
System.out.println(" no server");
   } }
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
public class Net extends JFrame
{
JPanels p=new JPanels();
public Net()
{
getContentPane().add(p);
}
public static void main(String[] args)
{
    Net net1=new Net();
    net1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        net1.setTitle("Net");
        net1.setSize(400, 400);
        net1.setVisible(true);
        net1.p.startserver();
       }

}