1、代码如下/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package timer1;/**
*
* @author Administrator
*/
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class HandleTime extends JFrame{JLabel lbl=new JLabel();
Date now=new Date();
public HandleTime() {now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);Timer timer=new Timer(1000,new ActionListener(){public void actionPerformed(ActionEvent e) {Date now2=new Date(now.getTime()+1000);
now=now2;
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");lbl.setText(formatter.format(now));
}});
timer.start();this.setLayout(new FlowLayout());
this.add(lbl);this.setSize(300,200);
this.setVisible(true);
}public static void main(String[] args) {
HandleTime t=new HandleTime();
}
} 我想设一个按钮,按下按钮,屏幕计时停止。
可是怎么试都不成功。
请问该如何操作?ps主要用于显示,一个程序的运行时间。比如,连接数据库之后,开始计时,屏幕可以实时显示00:00:00--->00:00:01,就像秒表一样,程序结束之后,这个秒表停止,屏幕显示总共的运行时间。
2、还有一个小问题,是识别文件后缀的。
我用filename.endsWith(".0A1")来判断,但是总是说,找不到该文件。如果重新复制粘贴一下该文件,就能找到了。这是为什么啊?
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package timer1;/**
*
* @author Administrator
*/
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class HandleTime extends JFrame{JLabel lbl=new JLabel();
Date now=new Date();
public HandleTime() {now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);Timer timer=new Timer(1000,new ActionListener(){public void actionPerformed(ActionEvent e) {Date now2=new Date(now.getTime()+1000);
now=now2;
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");lbl.setText(formatter.format(now));
}});
timer.start();this.setLayout(new FlowLayout());
this.add(lbl);this.setSize(300,200);
this.setVisible(true);
}public static void main(String[] args) {
HandleTime t=new HandleTime();
}
} 我想设一个按钮,按下按钮,屏幕计时停止。
可是怎么试都不成功。
请问该如何操作?ps主要用于显示,一个程序的运行时间。比如,连接数据库之后,开始计时,屏幕可以实时显示00:00:00--->00:00:01,就像秒表一样,程序结束之后,这个秒表停止,屏幕显示总共的运行时间。
2、还有一个小问题,是识别文件后缀的。
我用filename.endsWith(".0A1")来判断,但是总是说,找不到该文件。如果重新复制粘贴一下该文件,就能找到了。这是为什么啊?
解决方案 »
- 知网中文词库(即HowNet)怎样使用?
- jasperreport中怎么取出所有数据?
- 请问能否像VC#那样设置java的界面?
- java中怎么调用dll啊,而且java程序能编译成dll么
- [swing]问个基础问题,swing里的鼠标双击是怎么判断的?
- 想做一个java定时器,大家有这样的源代码吗?高分重谢
- 请教static和线程的问题??
- 不是高手不要进,紧急求助,请问如何在BYTE[]中查询指定的字串
- POI中如何设置电子表格中的URL, 急!!!!!在线等待!!!
- [求大神]Swing中有没有一个获取当前获得焦点窗体的方法
- eclipse运行问题
- 纠结中.....关于Socket类的小问题
/**
*
* @author Administrator
*/
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.JButton;
public class HandleTime extends JFrame
{
JLabel lbl=new JLabel();
JButton jButton = new JButton("stop");
JButton jButton2 = new JButton("restart");
Timer timer; Date now=new Date();
public HandleTime() {
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
timer=new Timer(1000,new ActionListener(){ public void actionPerformed(ActionEvent e)
{
Date now2=new Date(now.getTime()+1000);
now=now2;
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); lbl.setText(formatter.format(now));
}
});
timer.start();
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
timer.stop();
}
});
jButton2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
timer.restart();
}
});
this.setLayout(new FlowLayout());
this.add(lbl);
this.add(jButton);
this.add(jButton2); this.setSize(300,200);
this.setVisible(true);
}
public static void main(String[] args)
{
HandleTime t=new HandleTime();
}
}