java程序计时器_求完整简单java计时器小程序代码,急!!

8b6a0d1fd8dd3fdab5a2096fcd237d6b.png

放肆丶小侽人

回答时间:2019-12-05

向TA提问

import java.awt.Button;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.PopupMenu;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.Timer;

public class jishiqi extends JFrame {

final TextField txt = new TextField(10);

JLabel lbl = new JLabel();

// JLabel lb2 = new JLabel();

Date now = new Date();

public jishiqi() {

now.setHours(0);

now.setMinutes(0);

now.setSeconds(0);

final 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));

}

});

Button b1 = new Button("开始计时");

Button b2 = new Button("停止计时");

Button b3 = new Button("记录时间");

b3.setBounds(50, 50, 50, 50);

b2.setBounds(40, 40, 40, 40);

b1.setBounds(30, 30, 30, 30);

b1.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

Button b = (Button) e.getSource();

b.setLabel("开始计时");

timer.start();

b.setBackground(Color.yellow);

}

});

b2.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

Button b = (Button) e.getSource();

b.setLabel("停止计时");

timer.stop();

b.setBackground(Color.yellow);

}

});

b3.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// Button b = (Button) e.getSource();

Date now3 = new Date(now.getTime() + 1000);

now = now3;

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

// String s=(new

// SimpleDateFormat("yyyy-MM-dd")).format(Calendar.getInstance().getTime());

String s = formatter.format(now).toString();

txt.setText(s);

Button b = (Button) e.getSource();

b.setLabel("记录时间");

b.setBackground(Color.yellow);

}

});

this.setLayout(new FlowLayout());

this.add(txt);

this.add(b3);

this.add(b2);

this.add(b1);

this.add(lbl);

this.setSize(300, 200);

this.setVisible(true);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String[] args) {

jishiqi t = new jishiqi();

}

}

差不多了,界面你自己优化一下吧。

收起

6f93e2c92fc5e58e2ec18df1db0b9f08.png

阅读全文

9ded402a52cdd2b9481e864a5093252c.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值