用JAVA编一个小闹钟import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class Clock extends JFrame implements ActionListener {
public final int HEIGTH = 200, L0 = 50, T0 = 50,N=8;
public final double RAD = Math.PI / 180.0;
int x, y, old_X, old_Y, r, x0, y0, w, h, ang;
int sdo, mdo, hdo, old_M, old_H, hh, mm, ss;
int delay = 1000;
Calendar now;
String st, alarm, Items1, Items2,str[];
JButton jb;
JComboBox jc1, jc2, jc3;
JLabel jl1, jl2, jl3, jl4;
JMenu jm1, jm2, jm3, jm4;
JMenuBar jmb;
JMenuItem jmi1, jmi2, jmi3, jmi4, jmi5, jmi6, jmi7, jmi8, jmi9,jmi10;
JTextField jtf1, jtf2, time;
JPanel jp1, jp2, jp3;
Timer timer;
TimeZone tz = TimeZone.getTimeZone("JST");
Toolkit toolkit=Toolkit.getDefaultToolkit();;
/**
* br
* 方法说明:实现ActionListener类必须过载的方法
*/
public static void main(String[] args) {
Clock cp = new Clock();
cp.setVisible(true);
}
Clock() {
super("Java闹钟!");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(550, 700);
setVisible(true);
Container contentPane = getContentPane();
jp2 = new JPanel();
jmb = new JMenuBar();
jm1 = new JMenu("背景颜色设置", true);
jmi1 = new JMenuItem("外圈颜色");
jmi1.addActionListener(this);
jmi1.setActionCommand("color1");
jm1.add(jmi1);
jmi2 = new JMenuItem("闹钟边线颜色");
jmi2.addActionListener(this);
jmi2.setActionCommand("color2");
jm1.add(jmi2);
jmi3=new JMenuItem("底盘颜色");
jmi3.addActionListener(this);
jmi3.setActionCommand("color3");
jm1.add(jmi3);
jmi4=new JMenuItem("系统时间背静颜色");
jmi4.addActionListener(this);
jmi4.setActionCommand("color4");
jm1.add(jmi4);
jmb.add(jm1);
jm2 = new JMenu("指针颜色设置", true);
jmi5 = new JMenuItem("秒针颜色");
jmi5.addActionListener(this);
jmi5.setActionCommand("color5");
jm2.add(jmi5);
jmi6 = new JMenuItem("分针颜色");
jmi6.addActionListener(this);
jmi6.setActionCommand("color6");
jm2.add(jmi6);
jmi7 = new JMenuItem("时针颜色");
jmi7.addActionListener(this);
jmi7.setActionCommand("color7");
jm2.add(jmi7);
jmb.add(jm2);
jm3 = new JMenu("闹铃声音设置", true);
jmi8 = new JMenuItem("响铃1");
jmi8.addActionListener(this);
jmi8.setActionCommand("ring1");
jm3.add(jmi8);
jmi9 = new JMenuItem("静音");
jmi9.addActionListener(this);
jmi9.setActionCommand("ring2");
jm3.add(jmi9);
jmb.add(jm3);
jm4 = new JMenu("帮助", true);
jmi10=new JMenuItem("使用说明");
jmi10.addActionListener(this);
jmi10.setActionCommand("help");
jm4.add(jmi10);
jmb.add(jm4);
jp2.add(jmb);
contentPane.add(jp2, BorderLayout.NORTH);
jp3 = new JPanel();
jl1 = new JLabel("闹铃时间");
jl1.setFont(new Font("楷体_GB2312", Font.BOLD, 18));
time = new JTextField("00:00", 5);
alarm = time.getText();
jb = new JButton("修改闹铃时间");
jb.addActionListener(this);
jb.setActionCommand("CC");
jp3.add(jl1);
jp3.add(time);
jp3.add(jb);
contentPane.add(jp3, BorderLayout.SOUTH);
ClockPanel clock = new ClockPanel();
contentPane.add(clock, BorderLayout.CENTER);
// 窗体添加事件监听,监听秒表的触发
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
repaint();
}
};
new Timer(delay, taskPerformer).start();
}
/**
* br
* 方法说明:绘制图形
*/
Color C1 = Color.lightGray;// 外圈颜色
Color C2 = Color.black;// 边线颜色
Color C3 = Color.magenta;// 内盘颜色
Color C4 = Color.blue;// 背景颜色
Color C5 = Color.yellow;// 秒针颜色
Color C6 = Color.green;// 分针颜色
Color C7 = Color.red;//时针颜色
public class ClockPanel extends JPanel {
public void paint(Graphics g) {
h = getSize().height - 200;
// 绘制圆形
g.setColor(C1);
g.fillOval(L030, T030, h - 60, h - 60);
g.setColor(C2);
g.drawOval(L031, T031, h - 62, h - 62);
g.setColor(C3);
g.fillOval(L050, T050, h - 100, h - 100);
g.setColor(C2);
g.drawOval(L051, T051, h - 102, h - 102);
r = h / 2 - 30;
x0 = 30r - 5L0;
y0 = 30r - 5 - T0;
ang = 60;
for (int i = 1; i = 12; i) {
x = (int) ((r - 10) * Math.cos(RAD * ang)x0);
y = (int) ((r - 10) * Math.sin(RAD * ang)y0);
g.drawString(""i, x, h - y);
ang -= 30;
}
x0 = 30rL0;
y0 = 30rT0;
g.drawString("指针式时钟", 215, 200);
// 获取时间
now = Calendar.getInstance();
hh = now.get(Calendar.HOUR_OF_DAY);// 小时
mm = now.get(Calendar.MINUTE);// 分钟
ss = now.get(Calendar.SECOND);// 秒
g.setColor(C4);
g.fillRect(5, 550, 150, 30);// 填充的矩形
g.setColor(C6);
if (hh10)
st = "0"hh;
else
st = ""hh;
if (mm10)
st = st":0"mm;
else
st = st":"mm;
if(alarm.equals(st))
{
if(toolkit!=null)
toolkit.beep();
else{}
}
if (ss10)
st = st":0"ss;
else
st = st":"ss;
{
g.setFont(new Font("华文楷体", Font.BOLD, 16));
g.drawString("系统时间:"st, 10, 570);
}
// 计算时间和图形的关系
sdo = 90 - ss * 6;
mdo = 90 - mm * 6;
hdo = 90 - hh * 30 - mm / 2;
// 擦除秒针
if (old_X0) {
g.setColor(C3);
} else {
old_M = mdo;
old_H = hdo;
}
// 绘制秒针
g.setColor(C5);
x = (int) ((r - 26) * Math.cos(RAD * sdo)x0);
y = (int) ((r - 26) * Math.sin(RAD * sdo)y0) - 2 * T0;
g.drawLine(x0, y0, x, (h - y));
old_X = x;
old_Y = y;
// 擦除分针和时针
if (mdo != old_M) {
g.setColor(C3);
old_M = mdo;
}
if (hdo != old_H) {
g.setColor(C3);
old_H = hdo;
}
// 绘制分针
g.setColor(C6);
x = (int) ((r - 50) * Math.cos(RAD * mdo)x0);
y = (int) ((r - 50) * Math.sin(RAD * mdo)y0) - 2 * T0;
g.drawLine(x0, y0, x, (h - y));
// 绘制时针
g.setColor(C7);
x = (int) ((r - 90) * Math.cos(RAD * hdo)x0);
y = (int) ((r - 90) * Math.sin(RAD * hdo)y0) - 2 * T0;
g.drawLine(x0, y0, x, (h - y));
} // end paint
}
// 闹铃时间的判断及实现
// 闹铃声音的实现
public void actionPerformed(ActionEvent e) {
//JMenuItem m = (JMenuItem) e.getSource();
if (e.getActionCommand() == "CC") {
int newHou, newMin;
char c;
String getTime = JOptionPane.showInputDialog(this, "请输入闹铃时间格式如:", "00:00");
repaint();
//如果撤消设置时间,就什么打印null
if(getTime==null)
System.out.println(getTime);
//dispose();
judge: if (getTime != null) {
//打印输入的设置的时间
System.out.println(getTime);
// 判断输入的是不是5位字符
if (getTime.length() != 5) {
JOptionPane.showMessageDialog(time, "格式错误\n请按格式输入5位数字", "Error",
JOptionPane.ERROR_MESSAGE);
repaint();
break judge;
}
// 判断输入的是不是数字
for (int i = 0; i(getTime.length()); i) {
c = getTime.charAt(i);
if (i == 2!Character.isDigit(c))
continue;
// 判断当前字符,如果不是数字则跳出该事件
if (i != 2!Character.isDigit(c)) {
JOptionPane.showMessageDialog(this, "格式错误\n请按格式输入5位数字",
"Error",JOptionPane.ERROR_MESSAGE);
repaint();
break judge;
}
}
char[] hour = { getTime.charAt(0), getTime.charAt(1) };
char[] minute = { getTime.charAt(3), getTime.charAt(4) };
newHou = Integer.parseInt(String.valueOf(hour));
newMin = Integer.parseInt(String.valueOf(minute));
if (newHou = 24 || newHou0) {
JOptionPane.showMessageDialog(this, "格式错误\n小时应该是不小于0不大于23的正数",
"Error", JOptionPane.ERROR_MESSAGE);
repaint();
break judge;
}
if (newMin = 60 || newHou0) {
JOptionPane.showMessageDialog(this, "格式错误\n分钟应该是小于60的正数", "Error",
JOptionPane.ERROR_MESSAGE);
repaint();
break judge;
}
new SetTime(newHou, newMin);
}
}
if (e.getActionCommand() == "ring1") {
toolkit=Toolkit.getDefaultToolkit();
}
if(e.getActionCommand() == "ring2"){
System.out.println("静音");
toolkit=null;
}
if (e.getActionCommand() == "color1") {
String color;
Color c;
System.out.println("color1");
color = JOptionPane.showInputDialog(this, "请输入喜欢的外圈颜色(0--255)", "128");
if (color == null) {
} else {
if (Integer.parseInt(color)0
|| Integer.parseInt(color)255)
JOptionPane.showInputDialog(this, "请输入喜欢的外圈颜色(0--255)", "128");
else {
c = new Color(Integer.parseInt(color));
C1 = c;
}
}
}
if(e.getActionCommand() == "color2"){
String color;
Color c;
System.out.println("color2");
color = JOptionPane.showInputDialog(this, "请输入喜欢的边线颜色(0--255)", "128");
if(color==null){}
else{if (Integer.parseInt(color)0|| Integer.parseInt(color)255)
JOptionPane.showInputDialog(this, "请输入喜欢的边线颜色(0--255)", "128");
else {
c = new Color(Integer.parseInt(color));
C2 = c;
}
}
}
if(e.getActionCommand() == "color3"){
String color;
Color c;
System.out.println("color3");
color = JOptionPane.showInputDialog(this, "请输入喜欢的内盘颜色(0--255)", "128");
if(color==null){}
else{if (Integer.parseInt(color)0|| Integer.parseInt(color)255)
JOptionPane.showInputDialog(this, "请输入喜欢的内盘颜色(0--255)", "128");
else {
c = new Color(Integer.parseInt(color));
C3 = c;
}
}
}
if(e.getActionCommand() == "color4"){
String color;
Color c;
System.out.println("color4");
color = JOptionPane.showInputDialog(this, "请输入喜欢的背景颜色(0--255)", "128");
if(color==null){}
else{if (Integer.parseInt(color)0|| Integer.parseInt(color)255)
JOptionPane.showInputDialog(this, "请输入喜欢的背景颜色(0--255)", "128");
else {
c = new Color(Integer.parseInt(color));
C4 = c;
}
}
}
if(e.getActionCommand() == "color5"){
String color;
Color c;
System.out.println("color5");
color = JOptionPane.showInputDialog(this, "请输入喜欢的秒针颜色(0--255)", "128");
if(color==null){}
else{if (Integer.parseInt(color)0|| Integer.parseInt(color)255)
JOptionPane.showInputDialog(this, "请输入喜欢的秒针颜色(0--255)", "128");
else {
c = new Color(Integer.parseInt(color));
C5 = c;
}
}
}
if(e.getActionCommand() == "color6"){
String color;
Color c;
System.out.println("color6");
color = JOptionPane.showInputDialog(this, "请输入喜欢的分针颜色(0--255)", "128");
if(color==null){}
else{if (Integer.parseInt(color)0|| Integer.parseInt(color)255)
JOptionPane.showInputDialog(this, "请输入喜欢的分针颜色(0--255)", "128");
else {
c = new Color(Integer.parseInt(color));
C6 = c;
}
}
}
if(e.getActionCommand() == "color7"){
String color;
Color c;
System.out.println("color7");
color = JOptionPane.showInputDialog(this, "请输入喜欢的时针颜色(0--255)", "128");
if(color==null){}
else{if (Integer.parseInt(color)0|| Integer.parseInt(color)255)
JOptionPane.showInputDialog(this, "请输入喜欢的时针颜色(0--255)", "128");
else {
c = new Color(Integer.parseInt(color));
C7 = c;
}
}
}
if(e.getActionCommand() == "help"){
String help;
help = JOptionPane.showInputDialog(this, "输入quit退出该闹钟的使用", "这是运行在Java中的指针式时钟");
if(help.equals("quit"))
dispose();
else {}
//timer.restart();
}
}
class SetTime {
String Hour;
String Minute;
public SetTime() { }
public SetTime(int hour, int minute) {
// 当时间参数小于10的时候在前面添加字符0
if (hour10) {
Hour = "0"String.valueOf(hour);
} else {
Hour = ""String.valueOf(hour);
}
if (minute10) {
Minute = "0"String.valueOf(minute);
} else {
Minute = ""String.valueOf(minute);
}
alarm = Hour":"Minute;
time.setText(alarm);
repaint();
}
}
}
JAVA时钟代码解释lz这个代码有什么看不懂的 如果这个代码看不懂的话 建议你从基础的开始学习吧
首先可以看到extends JFrame implements ActionListener 关于ActionListener 接口和JFrame 类lz有必要去看看 加入api慢慢研究下吧
int x,y,x0,y0,r,h,olds_x,olds_y,oldm_x,oldm_y,oldh_x,oldh_y,ss,mm,hh,old_m,old_h,ang;
这些东西我也看不懂 因为你的代码不完整
final double RAD=Math.PI/180; 这个应该是算一个弧度吧
super("made by mcj and wdh"); 这句无意义 是父类的代码
setDefaultCloseOperation(3);这个方法是什么??代码不完整
setSize(300,300);设置窗体大小
setBackground(Color.white);设置背景色
setLocation(300,150); 定位
setResizable(false);不调整大小
setVisible(true);可见
后面的就没有代码了
java程序 时钟 整点报时代码import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
public class Test extends JFrame implements ActionListener{
Timer t=new Timer(1000,this);
GregorianCalendar calendar=new GregorianCalendar();
JLabel label1=new JLabel(new Date().toString()),
label2=new JLabel("
");
public Test(){
super("TIME");
Container c=this.getContentPane();
c.setLayout(new FlowLayout());
c.add(label1);c.add(label2);
t.start();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200,200,200,100);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
Date date=new Date();
calendar.setTime(date);
if(e.getSource()==t){
label1.setText(date.toString());
if(calendar.get(Calendar.MINUTE)==0)//当分钟为0报时
if(calendar.get(Calendar.SECOND)=5){//持续5秒报时
if(calendar.get(Calendar.SECOND)%2==0)
label2.setText("现在是北京时间" calendar.get(Calendar.HOUR_OF_DAY) "点整 。");
if(calendar.get(Calendar.SECOND)%2==1)
label2.setText("
");
}
}
}
public static void main (String[] args) {
new Test();
}
}
用java编写一个时钟的程序java时钟源代码我给java时钟源代码你做java时钟源代码了一下:(第二个功能你自己加吧)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.text.*;
public class TestTimer extends JFrame implements ActionListener{
Timer timer;
JLabel display = new JLabel();
SimpleDateFormat dateFmt = new SimpleDateFormat("hh:mm:ss");
TestTimer(){
getContentPane().setLayout(new FlowLayout());
timer = new Timer(1000,this);
timer.setRepeats(true);
timer.setInitialDelay(1000);
timer.start();
getContentPane().add(display);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,100);
setVisible(true);
} // TestTimer
public void actionPerformed(ActionEvent e){
if(e.getSource() == timer){
display.setText(dateFmt.format(new Date()));
}
} // actionPerformed
public static void main(String[] args){
TestTimer tt = new TestTimer();
}
} // TestTimer
电脑桌面罗盘时钟源代码怎么写import java.awt.*;
导入Java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import java.util.*;
publicclassrunabletestextendsjframeimplementsrunnable {
JPanel pnlMain; //定义变量
Jlabel Lbl时间; //定义变量
thread thd时间; //定义变量
日期数据显示
java编程利用按钮实现时钟的停止和开始功能(原代码)如下:
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{
staticJButton 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;
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();
}
}
运行截图:
有问题就追问,满意请采纳 。
【java时钟源代码 java简单时钟代码及详细解释】java时钟源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java简单时钟代码及详细解释、java时钟源代码的信息别忘了在本站进行查找喔 。
推荐阅读
- 显卡设置游戏变卡怎么办,显卡设置游戏变卡怎么办解决
- python计算程序,python计算机程序
- 关于单机棋牌游戏2017的信息
- 正优直播是什么牌子,正优app是做什么的
- oracle中主键怎么写 oracle主键怎么设置
- 弹幕如何体现新媒体文化,弹幕如何体现新媒体文化传播
- 庄园经营手机游戏,庄园经营类手游
- 养成收菜游戏,收菜类游戏
- 关于windows录制系统的信息