首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

用Java做个单线聊天室,为什么总是接收不到对方发出的消息。跪求

2012-01-28 
用Java做个单线聊天室,为什么总是接收不到对方发出的消息。跪求高手指点服务器端:package HomeWorkimport

用Java做个单线聊天室,为什么总是接收不到对方发出的消息。跪求高手指点
服务器端:
package HomeWork;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;



import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class singlechat extends JFrame {
  JTextArea textarea,textarea1;
  //JTextField textfield;
  JButton button,button1,button2,button3,button4,button5;
  JPanel panel1 = new JPanel();
  JPanel panel2 = new JPanel();
  JPanel panel3 = new JPanel();
  JPanel panel4 = new JPanel();
  JScrollPane scrollpane;
  public DataInputStream din;
public DataOutputStream dout;  
  public Socket socket ;

  public static void main(String[] args) {
  new singlechat();
  }

  public singlechat() {
  super("聊天室");
  addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
  try{
  din.close();
  dout.close();
  socket.close();
  System.exit(0);
  }
  catch (IOException ex){
  System.out.println("client closing: "+ ex.toString());
  }
  }
  });
  //this.setLayout();
  Font largeFont = new Font("TimesRoman", Font.BOLD, 20);
  Border lineBorder = new LineBorder(Color.BLACK, 2);
  button = new JButton("发送");
  button1 = new JButton("老大");
  button2 = new JButton("老二");
  button3 = new JButton("老三");  
  button4 = new JButton("老四");
  button5 = new JButton("老五");
  textarea = new JTextArea(6, 30);
  textarea1 = new JTextArea(3,30);
  textarea.setForeground(Color.blue);
  textarea.setFont(largeFont);
  textarea.setBorder(lineBorder);
  textarea1.setForeground(Color.red);
  textarea1.setFont(largeFont);
  textarea1.setBorder(lineBorder);
  textarea1.setBackground(Color.cyan );
  scrollpane = new JScrollPane(textarea);
  //scrollpane.setPreferredSize(new Dimension(200, 200));
  //textfield = new JTextField(20);//textfield要先new出来
  //textfield.setForeground(Color.red);
   
  // setContentPane(panel1);
  panel1.setPreferredSize(new Dimension(400, 300));
  //panel1.setBackground(Color.white);
  // panel1.setForeground(Color.blue);
  panel1.setVisible(true);
  panel1.setBorder(new TitledBorder("显示区域"));
  panel1.setLayout(new BorderLayout());// 设置边界布局
  panel1.add(scrollpane, BorderLayout.CENTER);
  //this.add(panel1);
  // setContentPane(panel2);
   
  //panel2.setBackground(Color.white);
  //panel2.setForeground(Color.blue);
  panel2.setVisible(true);
  panel2.setPreferredSize(new Dimension(400, 100));
  panel2.setBorder(new TitledBorder("输入区域"));
  panel2.setLayout(new GridLayout(2,1,10,10));// 设置边界布局


  panel2.add(textarea1);
  panel2.add(button);
   
  //this.add(panel2,BorderLayout.SOUTH);
  // setContentPane(panel3);
  //panel3.setBackground(Color.red);
  //panel3.setForeground(Color.blue);
   
  panel3.setVisible(true);
  panel3.setLayout(new BorderLayout());
  panel3.setPreferredSize(new Dimension(400, 600));
  panel3.setBorder(new TitledBorder("聊天区域"));
  panel3.add(panel1,BorderLayout.CENTER);
  panel3.add(panel2,BorderLayout.SOUTH);
   
   
   
  panel4.setVisible(true);
  panel4.setPreferredSize(new Dimension(200, 600));
  panel4.setBorder(new TitledBorder("群成员"));
  panel4.setLayout(new GridLayout(20,1,10,0));
  panel4.add(button1);
  panel4.add(button2);
  panel4.add(button3);
  panel4.add(button4);
  panel4.add(button5);
   
   
  this.setTitle("聊天室");  
  this.setVisible(true);
  this.setResizable(false);  
  this.setBounds(100,100,600,600);//这个函数 设置 窗口大小 及 显示位置的
  this.add(panel3,BorderLayout.WEST);
  this.add(panel4,BorderLayout.EAST);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  // button.addActionListener(new ButtonListener());
 
   
  try {
  ServerSocket serverSocket = new ServerSocket(8000);
textarea.append("Server started at " + new Date() + 
"\n server port is "+serverSocket.getInetAddress().getHostAddress()+
":"+serverSocket.getLocalPort() + "\n");

// Listen for a connection request
Socket socket = serverSocket.accept();
System.out.println("a client connected!");
DataInputStream din = new DataInputStream(socket.getInputStream());
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
while (true) {
button.addActionListener(new ListenerClass());
String ss;
ss = din.readUTF();
textarea.append("\n" + ss + "\n" );
}
}
catch(IOException ex) {
System.err.println("server:"+ex);

  } 
   
  private class ListenerClass implements ActionListener {
  public void actionPerformed(ActionEvent e) { 
  try{
  String s;
  s = textarea1.getText().toString();
  textarea.append("\n" + s + "\n");
  dout.writeChars(s); 
  }
  catch (Exception ex) {
  System.err.println("textarea exception: "+ex);
  }
   
  }
  }
}

   
   
   
 



[解决办法]
有没有错误信息?
[解决办法]
空指针有对象为null引起的,你看看抛异常的那几行代码中有没有对象可能是null的

热点排行