大虾请进!急急急!

chaseismydream 2001-07-21 09:33:54
那位大虾会编计数器,编好的也可以!
...全文
101 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pearl_091278 2001-07-23
  • 打赏
  • 举报
回复
光说谢谢啊 ~~~~~~~~~~~~~~~~~`恩~~~~~~~~~~~加分呀~~~西西!
chaseismydream 2001-07-23
  • 打赏
  • 举报
回复
谢谢!
pearl_091278 2001-07-23
  • 打赏
  • 举报
回复
我抄给你落!自己看拉!可花了我不少时间打字哦!不过小M总不食言的!!!!

程序如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


public class TimerTest{
public static void main(String[] args){
JFrame f=new TimerTestFrame();
f.show();

}
}



class TimerTestFrame extends JFrame{

public TimerTestFrame(){
setSize(450,300);
setTitle("TimerTest");


addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.exit(0);
}
});


Container c=getContentPane();
c.setLayout(new GridLayout(2,3));
c.add(new ClockCanvas("San Jose","GMT-8"));
c.add(new ClockCanvas("Taipei","GMT+8"));
c.add(new ClockCanvas("Berlin","GMT+1));
c.add(new ClockCanvas("New York","GMT-5"));
c.add(new ClockCanvas("Cairo","GMT+2"));
c.add(new ClockCanvas("Bombay","GMT+5));
}
}


interface TimerListener{
void timeElapsed(Timer t);
}


class Timer extends Thread{

private TimerListener target;
private int interval;

public Timer(int i,TimerListenr t){
target=t;
interval=i;
setDaemon(true);
}




public void run(){

try{

while(!interrupted){
sleep(interval);
target.timeElapsed(this);
}

}catch(InterruptedException e){}


}



}//end class





class ClockCanvas extends JPanel implements TimerListener{

private int seconds=0;
private String city;
private int offset;
private GregorianCalendar calendar;

private final int LOCAL=16;




public ClockCanvas(String c,String tz){

city=c;
calendar=new GregorianCalendar(TimeZone.getTimeZone(tz));
Timer t=new Timer(1000,this);
t.start();
setSize(125,125);
}


public void paintComponent(Graphics g){


super.paintComponent(g);
g.drawOval(0,0,100,100);
double hourAngle=2*Math.PI*(seconds-3*60*60)/(12*60*60);
double minteAngle=2*Math.PI*(seconds-15*60)/(60*60);
double secondAngle=2*Math.PI*(seconds-15)/60;


g.drawLine(50,50,50+(int)(30*Math.cos(hourAngle)),50+(int)(30*Math.sin(hourAngle)));
g.drawLine(50,50,50+(int)(40*Math.cos(minuteAngle)),50+(int(40*Math.sin(minuteAngle)));
g.drawLine(50,50,50_(int)(45*Math.cos(secondAngle)),50+(int)(45*Math.sin(secondAngle)));


g.drawString(city,0,115);


}




public void timeElapsed(Timer t){

calendar.setTime(new Date());
second=calendar.get(Calendar.HOUR)*60*60+calendar.get(Calendar.MINUTE)*60+calendar.get(Calendar.SECOND);
repaint();

}


}//end class





tiangou 2001-07-22
  • 打赏
  • 举报
回复
这是我的所写的程序中的计数器模块,真接调用的,你先仔细看一下:
private int count(HttpServletRequest request,HttpServletResponse response)
   {//no12
int counter = 0;
boolean needreadfile=true;
String HomeDirectory="";
String HomeTemp="";

try{

HomeDirectory=request.getRealPath("/");
HomeDirectory+="//";

boolean create=true;
HttpSession CountSession=request.getSession(create);

Integer iNumber=(Integer)CountSession.getValue("SessionCounter.Counter");

try
{
if (iNumber==null)
{
needreadfile=true;
}else {
needreadfile=false;
counter=iNumber.intValue();
}

if(needreadfile)
{ //no11
//read from file
int tmpNumber=0;
DataInputStream is;
String b1="";
try
{
is=new DataInputStream(new FileInputStream(HomeDirectory+"counter.txt"));
try {
b1=is.readLine();
tmpNumber=Integer.parseInt(b1);
}catch (IOException e) {
System.out.println("Read from file error!");
}
}catch (FileNotFoundException e) {
tmpNumber=0;
}

counter=tmpNumber+1;

//write to file
try
{
RandomAccessFile countFile=new RandomAccessFile(HomeDirectory+"counter.txt","rw");
long len=0;
countFile.seek(len);
b1=Integer.toString(counter);
countFile.writeBytes(b1);
countFile.close();
}
catch(IOException e){
System.out.println("Write to file error!");
}
}//yes11
}catch (NumberFormatException e)
{
System.out.println(e.toString());
counter=98;
}

String sTemp=""+counter;
iNumber=Integer.decode(sTemp);
CountSession.putValue("SessionCounter.Counter",iNumber);


}
catch(NumberFormatException e)
{System.out.println("000000000000804error");}
return counter;
}//yes12
pearl_091278 2001-07-21
  • 打赏
  • 举报
回复
哎呀,去买本JAVA2 高级特性那本书上有的。在交大昂立书店有买的。如果你在上海的话~
你要是真的想要,我星期一到公司,把那本书上的计数器抄给你?如何?MM只能帮到这了~~~回见!
xmvigour 2001-07-21
  • 打赏
  • 举报
回复
网上搜索一下!

62,614

社区成员

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

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