java计时器问题

2024-12-03 05:12:23
推荐回答(2个)
回答1:

点开始计时就从0开始,并在文本框中显示.点停止计时就停止.
你说的面板上是什么面板!!代码发来看看!!!!是用什么做的!!!VS吗??










请点击上面的“开始计时”按钮来启动计时器。输入框会一直进行计时,从 0 开始。点击“停止计时”按钮可以终止计时。



回答2:

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

class Go extends Thread,JFrame,Runable{
public void run(){
for(;;sec++){
if(sec==60){sec++;sec=0;}
if(sec==60){minute++;sec=0;}
if(minute==60){hour++;minute=0;}
try
{
Thread.sleep(1);
}
catch(Exception f){}
TF.setText(""+hour+":"+minute+":"+sec);
}

}
}

public class a implements ActionListener{
static int hour,minute=0,sec=0;
JButton BT1;
static int temp=0;
JTextField TF;

public static void main(String []args){
a b=new a();
b.go();
}

public void go(){

JFrame frame=new JFrame("计时器");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100,100);
frame.setVisible(true);
TF=new JTextField("00:00:00");BT1=new JButton("开始");BT1.addActionListener(this);

JPanel Jp=new JPanel();Jp.add(BT1);Jp.add(TF);
Container container=frame.getContentPane();container.add(Jp);

}
public void actionPerformed(ActionEvent e){
JButton bt16=(JButton)e.getSource();
if(bt16==BT1) go1();
}

public void go1(){for(;;sec++){
if(sec==60){sec++;sec=0;}
if(sec==60){minute++;sec=0;}
if(minute==60){hour++;minute=0;}
try
{
Thread.sleep(1);
}
catch(Exception f){}
TF.setText(""+hour+":"+minute+":"+sec);
}

}

}