日期:2014-05-20  浏览次数:20824 次

实现一个简单的自发自收信息的功能的程序,但运行出错,不知道哪里出错,求大虾帮忙纠正
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class kehuduan extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTextField jTextField = null;
private JScrollPane jScrollPane = null;
private JTextArea jTextArea = null;
private JButton jButton = null;
PrintWriter writer;
Socket sock; // @jve:decl-index=0:
BufferedReader reader; // @jve:decl-index=0:
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(90, 108, 164, 22));
}
return jTextField;
}

/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(65, 2, 204, 84));
jScrollPane.setViewportView(getJTextArea());
}
return jScrollPane;
}

/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
}
return jTextArea;
}

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(150, 145, 68, 24));
jButton.setText("发送");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try {
writer.println(jTextField.getText());
writer.flush();
jTextField.setText("");
jTextField.requestFocus();
Thread readerThread=new Thread(new IncomingReader());
readerThread.start();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


kehuduan thisClass = new kehuduan();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
};
/**
* This is the default constructor
*/
private void setUpNetworking(){
try {
sock=new Socket("127.0.0.1",5000);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
InputStreamReader steamReader=new InputStreamReader(sock.getInputStream());
reader=new BufferedReader(steamReader);
writer=new PrintWriter(sock.getOutputStream());
System.out.println("networking established");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public kehuduan() {