JAVA计时器,怎么写
需求如下:*第二题:从键盘接受一个数字,打印该数字表示的时间,最大单位到天,例如:*键盘输入6,打印6秒;*键盘输入60,打印1分;*键盘输入66,打印1分6秒;*键盘输...
需求如下:
*第二题:从键盘接受一个数字,打印该数字表示的时间,最大单位到天,例如:
*键盘输入6,打印6秒;
*键盘输入60,打印1分;
*键盘输入66,打印1分6秒;
*键盘输入666,打印11分6秒;
*键盘输入3601,打印1小时1秒 展开
*第二题:从键盘接受一个数字,打印该数字表示的时间,最大单位到天,例如:
*键盘输入6,打印6秒;
*键盘输入60,打印1分;
*键盘输入66,打印1分6秒;
*键盘输入666,打印11分6秒;
*键盘输入3601,打印1小时1秒 展开
2个回答
展开全部
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TimeCount extends JFrame implements ActionListener{
ThreadCount tc=new ThreadCount(this);
Thread thread=new Thread(tc);
JPanel panelN=new JPanel(),panelC=new JPanel();
JLabel label=new JLabel("计时器");
JButton butnStart=new JButton("开始");
boolean toEnd;
public TimeCount() {
setBounds(100,100,300,300);
setVisible(true);
label.setFont(new Font(null,Font.BOLD,22));
panelN.add(label);
add(panelN,BorderLayout.NORTH);
panelC.add(butnStart);
add(panelC,BorderLayout.CENTER);
butnStart.addActionListener(this);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==butnStart){
if(!thread.isAlive()){
thread=new Thread(tc);
thread.start();
}else {
toEnd=true;
}
}
}
public static void main(String[] args) {
new TimeCount();
}
}
class ThreadCount implements Runnable{
TimeCount lc;
public ThreadCount(TimeCount lc) {
super();
this.lc = lc;
}
public void run() {
int i=1;
while(true){
if(lc.toEnd){
lc.toEnd=false;
lc.butnStart.setText("开始");
return;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO: handle exception
}
i+=2;
int min=i/60000;
int second=(i%60000)/1000;
int mm=i%1000;
String show="";
if(min>0)
show+=min+":";
if(second>0)
show+=second+".";
show+=mm;
lc.label.setText(show);
}
}
}
满意请采纳。
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TimeCount extends JFrame implements ActionListener{
ThreadCount tc=new ThreadCount(this);
Thread thread=new Thread(tc);
JPanel panelN=new JPanel(),panelC=new JPanel();
JLabel label=new JLabel("计时器");
JButton butnStart=new JButton("开始");
boolean toEnd;
public TimeCount() {
setBounds(100,100,300,300);
setVisible(true);
label.setFont(new Font(null,Font.BOLD,22));
panelN.add(label);
add(panelN,BorderLayout.NORTH);
panelC.add(butnStart);
add(panelC,BorderLayout.CENTER);
butnStart.addActionListener(this);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==butnStart){
if(!thread.isAlive()){
thread=new Thread(tc);
thread.start();
}else {
toEnd=true;
}
}
}
public static void main(String[] args) {
new TimeCount();
}
}
class ThreadCount implements Runnable{
TimeCount lc;
public ThreadCount(TimeCount lc) {
super();
this.lc = lc;
}
public void run() {
int i=1;
while(true){
if(lc.toEnd){
lc.toEnd=false;
lc.butnStart.setText("开始");
return;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO: handle exception
}
i+=2;
int min=i/60000;
int second=(i%60000)/1000;
int mm=i%1000;
String show="";
if(min>0)
show+=min+":";
if(second>0)
show+=second+".";
show+=mm;
lc.label.setText(show);
}
}
}
满意请采纳。
展开全部
可以了
int second=secondNum%60;
secondNum = secondNum/60;
int min = secondNum%60;
secondNum = secondNum/60;
int hour = secondNum%12;
secondNum = secondNum/12;
int day = secondNum;
String str = "";
if(day!=0){
str+=day+"天" ;
}
if(hour!=0){
str+=hour+"小时" ;
}
if(min!=0){
str+=min+"分" ;
}
if(second!=0){
str+=second+"秒" ;
}
System.out.println(str);
更多追问追答
追问
好像不行耶
追答
前面加一句这个!
int secondNum = 100;
把这个做为输入的秒数,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询