各位大哥!我问个简单的问题!请不要不屑回答!

yuzhi130 2008-04-07 03:10:10
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class Client {

DataOutputStream dos;
Socket client = null;
public TextField typeIn;
public TextArea output;
DataInputStream dis = null;
boolean connected = false;

public static void main(String[] args) {
Client c = new Client();
c.run();
}

public void run() {

frameClient clientDialogbox = new frameClient();
connect();
new Thread(new getserverInformation()).start();
}

public void connect() {
try {
client = new Socket("169.254.64.49", 8888);
dos = new DataOutputStream(client.getOutputStream());
dis = new DataInputStream(client.getInputStream());
connected = true;
} catch (UnknownHostException e) {
System.out.println("Wrong host!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("There's no server!");
//e.printStackTrace();
System.exit(0);
}
}

public void disconnect() {
try {
dos.close();
dis.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void sentToserver(String s) {
try {
dos.writeUTF(s);
dos.flush();
//System.out.println(s);

} catch (IOException e) {
e.printStackTrace();
}
}

private class frameClient extends Frame {

frameClient() {
this.setLocation(300, 300);
this.setSize(300, 200);
this.setBackground(Color.green);
this.setLayout(new BorderLayout(2, 1));

typeIn = new TextField();
typeIn.setBackground(Color.orange);
output = new TextArea();
output.setBackground(Color.green);

this.add(typeIn, BorderLayout.SOUTH);
this.add(output, BorderLayout.NORTH);

this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent w) {
disconnect();
System.out.println("Bye!");
System.exit(0);
}
});

typeIn.addActionListener(new listener());

this.setVisible(true);
}

private class listener implements ActionListener {

public void actionPerformed(ActionEvent e) {
String in = typeIn.getText();
//output.setText(in);
typeIn.setText("");
sentToserver(in);

}
}
}

public class getserverInformation implements Runnable {

public void run() {
String readIn;
try {

while(connected) {
readIn = dis.readUTF();
String ins = typeIn.getText();
output.setText(ins + "\n" +readIn);
//System.out.println(readIn);
}

} catch (IOException e) {
try {
connected = false;
dis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Nothing to read!");
}
}
}
}



这里面有一个public class getserverInformation implements Runnable方法

在这一段里
while(connected) {
readIn = dis.readUTF();
String ins = typeIn.getText();//这个为什么是空的但是却并没有出错!
output.setText(ins + "\n" +readIn);
//System.out.println(readIn);
}
...全文
101 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangk_105 2008-04-07
  • 打赏
  • 举报
回复
以下是API的说明:
/***********
getText
public String getText()返回此文本组件表示的文本。默认情况下,此文本是一个空字符串。

返回:
此 TextComponent 的值
另请参见:
setText(java.lang.String)
************/

你可以看出,默认情况下,此文本是一个空字符串,所以返回后不会报错
yuzhi130 2008-04-07
  • 打赏
  • 举报
回复
我想在窗口中显示所有输入的东西!不是输出一行就把上一次输出的盖掉!
照理说我这样取值不会是空啊
yuzhi130 2008-04-07
  • 打赏
  • 举报
回复
这样取值怎么会是空呢?
不懂编程 2008-04-07
  • 打赏
  • 举报
回复
没取到值,空是正常的,你应当自己捕获
不懂编程 2008-04-07
  • 打赏
  • 举报
回复
你说的出错是指没抛出异常??
hp5212000 2008-04-07
  • 打赏
  • 举报
回复

String java.awt.TextComponent.getText()
Returns the text that is presented by this text component. By default, this is an empty string.

See Also:
java.awt.TextComponent.setText
Returns:
the value of this TextComponent

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧