当前位置: 代码迷 >> Java相关 >> JAVA有关问题 求教
  详细解决方案

JAVA有关问题 求教

热度:7896   发布时间:2013-02-25 21:49:15.0
JAVA问题 求教
下面的代码,让他运行发送信息后文本内容换行显示出来;我是初学的:import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class ChatClient extends Frame{

TextField tf = new TextField();
TextArea ta = new TextArea();

public static void main(String[] args) {
new ChatClient().launchFrame();

}

public void launchFrame(){
setLocation(400,300);
setSize(300,300);
add(tf,BorderLayout.SOUTH);
add(ta,BorderLayout.NORTH);
pack();
setVisible(true);
connect();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
tf.addActionListener(new TFlistener());

}

public void connect(){
try {
Socket s = new Socket("127.0.0.1",8181);
System.out.println("已连接服务器!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


private class TFlistener implements ActionListener{
public void actionPerformed(ActionEvent e){
String s = tf.getText().trim();
ta.setText(s);
tf.setText("");
}
}

}

------解决方案--------------------------------------------------------
private class TFlistener implements ActionListener{
 public void actionPerformed(ActionEvent e){
 //String s = tf.getText().trim();
 //ta.setText(s);

 String s = tf.getText().trim() + "\n";
 ta.append(s);
 tf.setText("");
 }
 }
------解决方案--------------------------------------------------------
Java code
java.awt.*;import java.awt.event.*;import java.net.*;import java.io.IOException;import java.io.*;public class ChatClient extends Frame{Socket ss = null;  TextField tf=new TextField();  TextArea ta=new TextArea();public static void main(String[] args) {new ChatClient().laFrame();}public void laFrame(){this.setLocation(500,300);this.setSize(500,400);add(tf,BorderLayout.SOUTH);add(ta,BorderLayout.NORTH);pack();setVisible(true);connect();this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});tf.addActionListener(new TFlistener());}public void connect(){try {ss= new Socket("127.0.0.1",8091);System.out.print("已经成功连接服务器!!");} catch (UnknownHostException e) {// TODO 自动生成 catch 块e.printStackTrace();} catch (IOException e) {// TODO 自动生成 catch 块e.printStackTrace();}}private class TFlistener implements ActionListener{public void actionPerformed(ActionEvent event){//String str=tf.getText().trim();//ta.setText(str);  String s = tf.getText().trim() + "\n";  ta.append(s);tf.setText("");try {DataOutputStream dos= new DataOutputStream(ss.getOutputStream());dos.writeUTF(s);dos.flush();dos.close();} catch (IOException e) {// TODO 自动生成 catch 块e.printStackTrace();}  }    }}import java.net.*;import java.io.*;public class ChatServer {public static void main(String[] args) {try {System.out.print("服务器端启动成功!!");ServerSocket s= new ServerSocket(8091);while(true){Socket ss=s.accept();System.out.print("客户端已连接成功!!");DataInputStream dis=new DataInputStream(ss.getInputStream());String str=dis.readUTF();System.out.println(str);dis.close();}} catch (IOException e) {e.printStackTrace();}}}
  相关解决方案