用服务器和客户端实现单词的翻译的
但是为什么在客户端程序中无法再显示时间呢
客户端程序如下:
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class PC extends Applet implements Runnable,ActionListener
{
Button query;
TextField danci,hanyi;
Socket socket=null;
DataInputStream in = null;
DataOutputStream out = null;
Thread thread1,thread2;
public void init()
{
removeAll();
repaint();
validate();
query = new Button("查询");
danci = new TextField(10);
hanyi = new TextField(25);
add(new Label("输入查询单词:"));add(danci);
add(query);
add(new Label("含义是:"));add(hanyi);
query.addActionListener(this);
}
public void start()
{
try
{
socket = new Socket("localhost",4331);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){}
if(thread1 == null)
{
thread1 = new Thread(this);
thread1.start();
}
if(thread2 == null)
{
thread2 = new Thread(this);
thread2.start();
}
}
public void run()
{
String s=null;
while(true)
{
if(Thread.currentThread()==thread1)
{
try
{
s = in.readUTF();
thread1.sleep(3);
}
catch(InterruptedException e)
{
hanyi.setText("与服务器已断开!");
break;
}
catch(IOException e)
{
hanyi.setText("与服务器已断开!");
break;
}
hanyi.setText(s);
}
else if(Thread.currentThread()==thread2)
{
try
{
Date date=new Date();
SimpleDateFormat m=new SimpleDateFormat("yyyy年MM月dd日 HH :mm :ss");
showStatus(m.format(date));
thread2.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==query)
{
String s=danci.getText();
if(s.trim()!=null)
{
try
{
out.writeUTF(s);
}
catch(IOException e1){}
}
}
}
}