请问为什么我在jTextField1输入后不能在jTextArea1显示哦!
我是先运行的客户端再运行的服务器!
谢谢哦
客户端:
package untitled10;import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Applet1 extends Applet implements Runnable{
private boolean isStandalone = false;
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JTextField jTextField1 = new JTextField();
JTextArea jTextArea1 = new JTextArea();
DataInputStream in=null;
Socket socket=null;
DataOutputStream out=null;
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public Applet1() {
} //Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
jButton1.setText("发送");
jButton1.addActionListener(new Applet1_jButton1_actionAdapter(this));
this.setLayout(xYLayout1);
jTextField1.setText("");
jTextArea1.setText("");
this.add(jButton1, new XYConstraints(225, 266, 74, 28));
this.add(jTextField1, new XYConstraints(20, 267, 193, 24));
this.add(jTextArea1, new XYConstraints(17, 19, 278, 227));
} //Get Applet information
public String getAppletInfo() {
return "Applet Information";
} //Get parameter info
public String[][] getParameterInfo() {
return null;
}
public void start(){
try{
Socket socket=new Socket(this.getCodeBase().getHost(),4331);
DataInputStream in=new DataInputStream(socket.getInputStream());
DataOutputStream out=new DataOutputStream(socket.getOutputStream());
}catch(Exception e){}
Thread thread=new Thread();
thread.start();
}
public void run(){
String s1 = " ";
while (true) {
try {
s1=in.readUTF();
}
catch (Exception e) {}
if (s1.equals("bye")) {
try {
socket.close();
break;
}catch(Exception e){}
}
else{
jTextArea1.append(s1+"/n");
}
}
} void jButton1_actionPerformed(ActionEvent e) {
String s = jTextField1.getText();
if (s != null) {
try{
out.writeUTF(s);
}catch(Exception e1){}
}
else{
try{
out.writeUTF("请说话:");
}catch(Exception e1){}
}
}
}class Applet1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Applet1 adaptee; Applet1_jButton1_actionAdapter(Applet1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
服务器:
package untitled11;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class li implements Runnable{
DataInputStream in=null;
DataOutputStream out=null;
public static void main(String args[]){
try{
ServerSocket server = new ServerSocket(4331);
Socket you=server.accept();
DataInputStream in=new DataInputStream(you.getInputStream());
DataOutputStream out=new DataOutputStream(you.getOutputStream());
}catch(Exception e){}
Thread thread1=new Thread();
thread1.start();
}public void run(){
String s=" ";
Socket socket=null;
while(true){
try{
s=in.readUTF();
}catch(Exception e){}
try{
if(s.equals("bye"))
{
socket.close();
break;
}
else{
try{
out.writeUTF("服务器说:"+s);
}catch(Exception e){}
}
}catch(Exception e){}
}}
}
我是先运行的客户端再运行的服务器!
谢谢哦
客户端:
package untitled10;import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Applet1 extends Applet implements Runnable{
private boolean isStandalone = false;
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JTextField jTextField1 = new JTextField();
JTextArea jTextArea1 = new JTextArea();
DataInputStream in=null;
Socket socket=null;
DataOutputStream out=null;
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public Applet1() {
} //Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
} //Component initialization
private void jbInit() throws Exception {
jButton1.setText("发送");
jButton1.addActionListener(new Applet1_jButton1_actionAdapter(this));
this.setLayout(xYLayout1);
jTextField1.setText("");
jTextArea1.setText("");
this.add(jButton1, new XYConstraints(225, 266, 74, 28));
this.add(jTextField1, new XYConstraints(20, 267, 193, 24));
this.add(jTextArea1, new XYConstraints(17, 19, 278, 227));
} //Get Applet information
public String getAppletInfo() {
return "Applet Information";
} //Get parameter info
public String[][] getParameterInfo() {
return null;
}
public void start(){
try{
Socket socket=new Socket(this.getCodeBase().getHost(),4331);
DataInputStream in=new DataInputStream(socket.getInputStream());
DataOutputStream out=new DataOutputStream(socket.getOutputStream());
}catch(Exception e){}
Thread thread=new Thread();
thread.start();
}
public void run(){
String s1 = " ";
while (true) {
try {
s1=in.readUTF();
}
catch (Exception e) {}
if (s1.equals("bye")) {
try {
socket.close();
break;
}catch(Exception e){}
}
else{
jTextArea1.append(s1+"/n");
}
}
} void jButton1_actionPerformed(ActionEvent e) {
String s = jTextField1.getText();
if (s != null) {
try{
out.writeUTF(s);
}catch(Exception e1){}
}
else{
try{
out.writeUTF("请说话:");
}catch(Exception e1){}
}
}
}class Applet1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Applet1 adaptee; Applet1_jButton1_actionAdapter(Applet1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
服务器:
package untitled11;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class li implements Runnable{
DataInputStream in=null;
DataOutputStream out=null;
public static void main(String args[]){
try{
ServerSocket server = new ServerSocket(4331);
Socket you=server.accept();
DataInputStream in=new DataInputStream(you.getInputStream());
DataOutputStream out=new DataOutputStream(you.getOutputStream());
}catch(Exception e){}
Thread thread1=new Thread();
thread1.start();
}public void run(){
String s=" ";
Socket socket=null;
while(true){
try{
s=in.readUTF();
}catch(Exception e){}
try{
if(s.equals("bye"))
{
socket.close();
break;
}
else{
try{
out.writeUTF("服务器说:"+s);
}catch(Exception e){}
}
}catch(Exception e){}
}}
}
不在同一个包这样应该没有问题百