| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1438 人关注过本帖
标题:为什么不能显示时间
取消只看楼主 加入收藏
gail
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-3-29
收藏
 问题点数:0 回复次数:13 
为什么不能显示时间

用服务器和客户端实现单词的翻译的
但是为什么在客户端程序中无法再显示时间呢
客户端程序如下:
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){}
}
}
}
}

搜索更多相关主题的帖子: 时间 
2007-01-13 21:19
gail
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-3-29
收藏
得分:0 


可是我的不可以

2007-01-13 21:42
gail
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-3-29
收藏
得分:0 
再问一个很白痴的问题
我觉得可能是我的工程建的有问题
我不会建
是不是要把客户端和服务器端的放在一个工程中
而且如何在一个工程中加入应用程序和小应用程序
我的现在都不能运行了
可能是我哪里弄错了

2007-01-13 21:47
gail
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-3-29
收藏
得分:0 
就是我在建了工程后
我要加入applet
它不是也属于工程中的怎么添加呢

2007-01-13 21:53
gail
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-3-29
收藏
得分:0 

import java.sql.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class PS
{
public static void main(String[] a)
{
ServerSocket server = null;
server_thread thread;
Socket you = null;
System.out.println("服务器已经启动!");
while(true)
{
try
{
server = new ServerSocket(4331);
}
catch(IOException e){}

try
{
you=server.accept();
}
catch(IOException e){}

if(you!=null)
{
System.out.println("接收到来自客户端的一个请求!");
new server_thread(you).start();//是下面两条命令之和
//server_thread qq = new server_thread(you);
//qq.start();
}
else
{
continue;
}
}
}

}


class server_thread extends Thread
{
Socket socket;
Connection con = null;
Statement stmt = null;
DataInputStream in = null;
DataOutputStream out = null;
int n = 0;
String s = null;

server_thread(Socket t)
{
socket = t;
try
{
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1){}
try
{
con = DriverManager.getConnection("jdbc:odbc:san");
stmt = con.createStatement();
}
catch(SQLException e2){}
}

public void run()
{
while(true)
{
try
{
s = in.readUTF();
}
catch(IOException e){}

try
{
if(!(s.equalsIgnoreCase("Quit")))
{
n=0;
ResultSet rs = stmt.executeQuery("select * from danci");
while(rs.next())
{
String d1=rs.getString(2);
String d2=rs.getString(1);
if(s.equals(d2))//字符串比较
{
out.writeUTF(d1);
n=1;
break;

}
else if(s.equals(d1))//字符串比较
{
out.writeUTF(d2);
n=1;
break;

}
}
if(n==0)
{
out.writeUTF("没有该单词!");
}
}
else if(s.equalsIgnoreCase("Quit"))
{
out.writeUTF("你选择了退出!");
con.close();
//Thread.currentThread().yield();
break;
}
sleep(1);
}
catch(InterruptedException e1){}
catch(IOException e2){}
catch(SQLException e3){}
}
}
}










2007-01-13 21:55
gail
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-3-29
收藏
得分:0 
已经放上去了
哪个就是服务器端啊
是不是还要数据库中的表呢

2007-01-13 22:00
gail
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-3-29
收藏
得分:0 
我刚刚试过了
服务器的都不能运行了

2007-01-13 22:13
gail
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-3-29
收藏
得分:0 
是access
我开始都可以的
后来不知道被我怎么弄的
不可以了

2007-01-13 22:19
gail
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-3-29
收藏
得分:0 
我是用JCreator写的

2007-01-13 22:20
gail
Rank: 1
等 级:新手上路
帖 子:73
专家分:0
注 册:2006-3-29
收藏
得分:0 
我开始没有弄显示时间时 都可以运行的
不过当时是因为我知道我不会建立工程就把直接放在一个已经存在的工程中
当时一切都运行正常
后来加了显示时间,我怕出错,就另外弄了一个工程,
结果就不可以了
我想可能是我的工程有问题

2007-01-13 22:23
快速回复:为什么不能显示时间
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016952 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved