讨论:写个闹钟小程序

uYuOmooo 2008-11-08 07:51:01
想随便写个小闹钟程序,首先嘛,就是要显示当前系统的时间,在写线程的时候不会写了,下面是我的错误源代码,大家帮忙改进下,或者自己写的比较好的代码贴出来。改进后或自己写的代码最好自己先运行下可以后再贴出来哦。谢谢大家的讨论,have a nice day ^ - ^


import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;


public class Clock{
public SimpleDateFormat sdf;
public String timeString;

public static void main(String[] args){


JFrame frame=new JFrame("yuzhenghzong clock!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);

new Thread(new startClock()).start();

JLabel label=new JLabel(timeString,JLabel.CENTER);

frame.add(label,BorderLayout.CENTER);

frame.setVisible(true);
}

class startClock implements Runnable{

public void run() {
// TODO Auto-generated method stub
while(true){
sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
timeString = sdf.format(new Date());

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
}
...全文
324 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lizhi200404520 2009-03-08
  • 打赏
  • 举报
回复
路过
nj_dobetter 2009-02-21
  • 打赏
  • 举报
回复
import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class b{
static public SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");;
static public String timeString;
static JLabel label;
static java.util.Date date;
public static void main(String[] args){
JFrame frame=new JFrame("yuzhenghzong clock!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
startClock sc=new startClock();
label=new JLabel(timeString,JLabel.CENTER);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(label,BorderLayout.CENTER);
frame.setVisible(true);
new Thread(sc).start();
}

static class startClock implements Runnable{

public void run() {
// TODO Auto-generated method stub
while(true){
date = null;
date=new Date();
timeString = sdf.format(date);
try {
label.setText(timeString);
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
}
一洽客服系统 2009-02-20
  • 打赏
  • 举报
回复
run方法不要实例全局变量~~~~~~~sdf.
使用timer类更好点
zhangzj27 2009-02-20
  • 打赏
  • 举报
回复
新手学习,谢谢。
chaner2002 2009-02-20
  • 打赏
  • 举报
回复
明显只是个时钟。。。。
猪乐乐1618 2009-02-19
  • 打赏
  • 举报
回复
谢谢楼主分享。。。
fangfangff 2008-11-20
  • 打赏
  • 举报
回复
是不是真的可以闹的啊???
CTea321 2008-11-17
  • 打赏
  • 举报
回复
是不是就是时钟啊
到某个时间的时候,触发一个事件,打开一个音频文件,响几声
呵呵
ZhaoRNash 2008-11-17
  • 打赏
  • 举报
回复
楼主,你那也叫闹钟啥,还真是奇怪了哈。。。。。。。。。。。。。
-box- 2008-11-11
  • 打赏
  • 举报
回复
关注中!
showde123 2008-11-11
  • 打赏
  • 举报
回复
上边是个老版本的
showde123 2008-11-11
  • 打赏
  • 举报
回复
帖子要沉了哦
给LZ参考一个

import java.awt.Color;
import java.util.*;
import java.awt.*;
import java.applet.*;

public class Clock extends Applet implements Runnable {

private static final long serialVersionUID = 1L;
Thread timer = null;
Label label;
int lastxs = 50, lastys = 30, lastxm = 50, lastym = 30, lastxh = 50,
lastyh = 30;

public void init() {
label = new Label(" ");

setBackground(Color.white);
add(label);
}

@SuppressWarnings("deprecation")
public void paint(Graphics g) {
int xh, yh, xm, ym, xs, ys, s, m, h, xcenter, ycenter;
Date rightnow = new Date();
String today = rightnow.toLocaleString();
label.setText(today);
s = rightnow.getSeconds();
m = rightnow.getMinutes();
h = rightnow.getHours();
xcenter = 100;
ycenter = 80;

xs = (int) (Math.cos(s * 3.14f / 30 - 3.14f / 2) * 45 + xcenter);
ys = (int) (Math.sin(s * 3.14f / 30 - 3.14f / 2) * 45 + ycenter);
xm = (int) (Math.cos(m * 3.14f / 30 - 3.14f / 2) * 45 + xcenter);
ym = (int) (Math.sin(m * 3.14f / 30 - 3.14f / 2) * 45 + ycenter);
xh = (int) (Math.cos((h * 30 + m * 2) * 3.14f / 180 - 3.14f / 2) * 30 + xcenter);
yh = (int) (Math.sin((h * 30 + m * 2) * 3.14f / 180 - 3.14f / 2) * 30 + ycenter);

g.setFont(new Font("TimesToman", Font.PLAIN, 14));
g.setColor(Color.orange);
g.fill3DRect(xcenter - 50, ycenter - 50, 100, 100, true);
g.setColor(Color.darkGray);
g.drawString("12", xcenter - 5, ycenter - 37);
g.drawString("3", xcenter + 40, ycenter + 3);
g.drawString("6", xcenter - 3, ycenter + 45);
g.drawString("9", xcenter - 45, ycenter + 3);

g.setColor(Color.orange);
if (xs != lastxs || ys != lastys) {
g.drawLine(xcenter, ycenter, lastxs, lastys);
}
if (xm != lastxm || ym != lastym) {
g.drawLine(xcenter, ycenter - 1, lastxm, lastym);
g.drawLine(xcenter - 1, ycenter, lastxm, lastym);
}
if (xh != lastxh || yh != lastyh) {
g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);
g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);
}
g.setColor(Color.red);

g.drawLine(xcenter, ycenter, xs, ys);
g.drawLine(xcenter, ycenter - 1, xm, ym);
g.drawLine(xcenter - 1, ycenter, xm, ym);
g.drawLine(xcenter, ycenter - 1, xh, yh);
g.drawLine(xcenter - 1, ycenter, xh, yh);
lastxs = xs;
lastys = ys;
lastxm = xm;
lastym = ym;
lastxh = xh;
lastyh = yh;
}

public void start() {
if (timer == null) {
timer = new Thread(this);
timer.start();
}
}

public void stop() {
timer = null;
}

public void run() {
while (timer != null) {
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
repaint();
}
timer = null;
}

public void update(Graphics g) {
paint(g);
}

}
刘葛曦 2008-11-09
  • 打赏
  • 举报
回复
看看去
showde123 2008-11-09
  • 打赏
  • 举报
回复
时钟有数字式 也有图形式的哦
阳二快跑 2008-11-09
  • 打赏
  • 举报
回复

import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;


public class Clock{
private SimpleDateFormat sdf;
private JLabel label;

public Clock(){
JFrame frame=new JFrame("yuzhenghzong clock!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);

new Thread(new startClock()).start();

label=new JLabel("",JLabel.CENTER);

frame.add(label,BorderLayout.CENTER);

frame.setVisible(true);
}

public static void main(String[] args){

Clock cl=new Clock();

}

class startClock implements Runnable{

public void run() {
// TODO Auto-generated method stub
while(true){
sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
label.setText( sdf.format(new Date()));

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}
}
这叫时钟不叫闹钟吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧