重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
简单代码如下:
成都创新互联公司主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、营销型网站建设、网站程序开发、HTML5响应式网站建设公司、手机网站制作、微商城、网站托管及成都企业网站维护、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为服务器租用行业客户提供了网站建设服务。
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class Timers extends JFrame {
final Label lab = new Label();
Date now = new Date();
@SuppressWarnings("deprecation")
public Timers() {
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
setBounds(550, 270, 200, 150);
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");
lab.setText(formatter.format(now));
}
});
Button b1 = new Button("开始");
Button b2 = new Button("停止");
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();
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Button b = (Button) e.getSource();
b.setLabel("停止");
timer.stop();
}
});
this.setLayout(new FlowLayout());
this.add(b2);
this.add(b1);
this.add(lab);
this.setSize(300, 200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Timers t = new Timers();
t.lab.setText("00:00:00");
}
}
不知是否帮到你,如满意请采纳!
input type="button" value="开始" onclick="startTime() "
input type="button" value="停止" onclick="stopTime() "
script type="text/javascript"
var clock_obj = document.getElementById("clock");
var time_now = 0;
var count = 0;
var time_id = window.setInterval("showTime()",1000);
//点击开始按钮事件
function startTime(){
//重复点击开始事件将上一次的timer清除
clearInterval(time_id);
time_id = window.setInterval("showTime()",1000);
}
//点击停止按钮事件
function stopTime() {
clearInterval(time_id);
}
function showTime( )
{
var time_now = new Date();
time_now = time_now.toLocaleString();
clock_obj.innerHTML = time_now;
count++;
}
/script
如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.geom.*;
import java.util.*;
class Clock extends Canvas
implements ActionListener{
static JButton jb=new JButton("开始");
static JButton jb1=new JButton("暂停");
Date date;
Timer secondTime;
int hour,munite,second;
Line2D secondLine,muniteLine,hourLine;
int a,b,c;
double pointSX[]=new double[60],//用来表示秒针端点坐标的数组
pointSY[]=new double[60],
pointMX[]=new double[60], //用来表示分针端点坐标的数组
pointMY[]=new double[60],
pointHX[]=new double[60], //用来表示时针端点坐标的数组
pointHY[]=new double[60];
Clock()
{ secondTime=new Timer(1000,this);
pointSX[0]=0; //12点秒针位置
pointSY[0]=-100;
pointMX[0]=0; //12点分针位置
pointMY[0]=-90;
pointHX[0]=0; //12点时针位置
pointHY[0]=-70;
double angle=6*Math.PI/180; //刻度为6度
for(int i=0;i59;i++) //计算出各个数组中的坐标
{ pointSX[i+1]=pointSX[i]*Math.cos(angle)-Math.sin(angle)*pointSY[i];
pointSY[i+1]=pointSY[i]*Math.cos(angle)+pointSX[i]*Math.sin(angle);
pointMX[i+1]=pointMX[i]*Math.cos(angle)-Math.sin(angle)*pointMY[i];
pointMY[i+1]=pointMY[i]*Math.cos(angle)+pointMX[i]*Math.sin(angle);
pointHX[i+1]=pointHX[i]*Math.cos(angle)-Math.sin(angle)*pointHY[i];
pointHY[i+1]=pointHY[i]*Math.cos(angle)+pointHX[i]*Math.sin(angle);
}
for(int i=0;i60;i++)
{ pointSX[i]=pointSX[i]+120; //坐标平移
pointSY[i]=pointSY[i]+120;
pointMX[i]=pointMX[i]+120; //坐标平移
pointMY[i]=pointMY[i]+120;
pointHX[i]=pointHX[i]+120; //坐标平移
pointHY[i]=pointHY[i]+120;
}
secondLine=new Line2D.Double(0,0,0,0);
muniteLine=new Line2D.Double(0,0,0,0);
hourLine=new Line2D.Double(0,0,0,0);
secondTime.start(); //秒针开始计时
}
public void paint(Graphics g)
{ for(int i=0;i60;i++) //绘制表盘上的小刻度和大刻度
{ int m=(int)pointSX[i];
int n=(int)pointSY[i];
if(i%5==0)
{ g.setColor(Color.red);
g.fillOval(m-4,n-4,8,8);
}
else
{ g.setColor(Color.cyan);
g.fillOval(m-2,n-2,4,4);
}
}
g.fillOval(115,115,10,10); //钟表中心的实心圆
Graphics2D g_2d=(Graphics2D)g;
g_2d.setColor(Color.red);
g_2d.draw(secondLine);
BasicStroke bs=
new BasicStroke(3f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);
g_2d.setStroke(bs);
g_2d.setColor(Color.blue);
g_2d.draw(muniteLine);
bs=new BasicStroke(6f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);
g_2d.setStroke(bs);
g_2d.setColor(Color.green);
g_2d.draw(hourLine);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==secondTime){
date=new Date();
String s=date.toString();
hour=Integer.parseInt(s.substring(11,13));
munite=Integer.parseInt(s.substring(14,16));
second=Integer.parseInt(s.substring(17,19)); //获取时间中的秒
int h=hour%12;
a=second; //秒针端点的坐标
b=munite; //分针端点的坐标
c=h*5+munite/12; //时针端点的坐标
secondLine.setLine(120,120,(int)pointSX[a],(int)pointSY[a]);
muniteLine.setLine(120,120,(int)pointMX[b],(int)pointMY[b]);
hourLine.setLine(120,120,(int)pointHX[c],(int)pointHY[c]);
repaint();
} if(e.getSource()==jb){
secondTime.start();
}if(e.getSource()==jb1){
secondTime.stop();
}
}
public static void main(String args[]){
JFrame win=new JFrame("时钟");
JPanel jp=new JPanel();
jp.add(jb);
jp.add(jb1);
Clock clock=new Clock();
jb.addActionListener(clock);
jb1.addActionListener(clock);
win.add(clock,BorderLayout.CENTER);
win.add(jp,"South");
win.setVisible(true);
win.setSize(246,300);
win.setDefaultCloseOperation(3) ;
win.validate();
}
}
运行截图:
有问题就追问,满意请采纳。
下面这个只能实现每按下一次按钮显示一次时间,动态的要用到Timer,本人新手那个做不来
import java.applet.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends Applet implements ActionListener{
private Label l;
public String times;
private Button b1;
public Date d;
public void init(){
l=new Label("");
b1=new Button("showtime");
d=new Date();
SimpleDateFormat from = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
times = from.format(d);
add(b1);
add(l);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1)
{
l.setText(times);
}
}
}
你没贴代码呢,
给你个思路.要用到线程.
点开始的时候,运行线程.
定义三个状态变量,可以用bool型的.
run()
{
Thread.sleep(1000);
if(a1)
....
if(a2)
...
if(a3)
...
}
在点击时间1的时候,就将a1设置为false.这样虽然线程没有停止,但数据不会改变了,就达到了你的目的.