求帮忙用Java 写一个秒表的图 效果奉上 秒表要等点
2个回答
展开全部
public class window extends JFrame implements Runnable{
final private time jp = new time();
final private JButton start = new JButton("start");
final private JButton stop = new JButton("stop");
private Thread t = new Thread(this);
private boolean isRun = true;
private boolean isWait = true;
private long starttime = 0;
private long savetime = 0;
public window(){
this.setBounds(50, 50, 400, 300);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
jp.setBounds(0, 0, 400, 200);
jp.setLayout(null);
this.add(jp);
start.setBounds(100, 220, 70, 50);
start.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
synchronized(window.this){
if(starttime==0) starttime = System.currentTimeMillis();
isWait = false;
window.this.notify();
}
}
});
stop.setBounds(230, 220, 70, 50);
stop.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
isWait = true;
}
});
this.add(start);
this.add(stop);
t.start();
}
class time extends JPanel{
public int fen = 0;
public int miao = 0;
public int haomiao = 0;
@Override
public void paint(Graphics g){
g.clearRect(0, 0, 400, 200);
g.setColor(new Color(0,0,255));
g.drawString(getString(), 170, 100);
}
public void up(long hm){
haomiao = (int)(hm%1000);
miao = (int)(hm/1000%60);
fen = (int)(hm/1000/60);
this.repaint();
}
private String getString(){
return ""+(fen>=10? fen:"0"+fen)+":"+(miao>=10? miao:"0"+miao)+":"+(haomiao>=100? haomiao:(haomiao>=10? "0"+haomiao:"00"+haomiao));
}
}
@Override
public synchronized void run() {
while(isRun){
try {
if(isWait) this.wait();
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
long zt = System.currentTimeMillis() - starttime;
if(zt!=savetime){
savetime = zt;
jp.up(zt);
}
}
}
}
//测试
public class test {
public static void main(String args[]){
window w = new window();
}
}
至于你要的那种画面,自己稍微改改吧
2016-06-12
展开全部
显示耗秒,可以使用 java.text.SimpleDateFormat 的。。。。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询