java秒表代码_秒表 - java代码库 - 云代码

[java]代码库package Day0822;

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

public class HomeWork implements ActionListener,Runnable{

private int x = 0;

private int y = 0;

private int z = 0;

private int num = 0;

private JFrame jf;

private JPanel jp;

private JButton jb;

private JButton jb1;

private JButton jb2;

private JTextField jtf;

private boolean flag = true;

private Thread thread = new Thread(this);

public void init(){

jf = new JFrame("*********秒表*********");

jp = new JPanel();

jb = new JButton("开始");

jb1 = new JButton("暂停");

jb2 = new JButton("重置");

jtf = new JTextField(9);

jf.setSize(300,150);

jf.setLocationRelativeTo(null);

jf.setLayout(new FlowLayout());

jf.setResizable(false);

jtf.setFont(new Font("楷体",1,50));

jtf.setHorizontalAlignment(JTextField.CENTER);

jtf.setText(setTime());

jb.addActionListener(this);

jb1.addActionListener(this);

jb2.addActionListener(this);

jf.add(jtf);

jf.add(jb);

jf.add(jb1);

jf.add(jb2);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setVisible(true);

}

public void actionPerformed(ActionEvent e){

if(e.getSource().equals(jb)){

flag = true;

new Thread(this).start();

}

if(e.getSource().equals(jb1)){

flag = false;

}

if(e.getSource().equals(jb2)){

flag = false; //线程终止

num = 0;

x = 0;

y = 0;

z = 0;

jtf.setText("0:0:0");

}

}

public void run(){

while(flag){

try {

Thread.sleep(1000);

num = num+1;

z=(num)%60;

y = (num/60)%60;

x = (num/3600)%24;

} catch (InterruptedException e) {

e.printStackTrace();

}finally{

jtf.setText(setTime());

}

}

}

public String setTime(){

return x+":"+y+":"+z;

}

public String setTime1(){

return "0"+x+":0"+y+":0"+z;

}

public static void main(String[] arge){

HomeWork hw = new HomeWork();

hw.init();

}

}

694748ed64b9390909c0d88230893790.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值