java钟小程序代码 java 小程序

java如何在2048中实现时间java编写时钟程序_Java实现时钟小程序 原创
2021-02-17 00:47:37
无可就是九头鸟
码龄6年
关注
哎,好久没上博客园发东西了,上一次还是两个月前的五一写的一篇计算器博客,不过意外的是那个程序成了这学期的Java大作业,所以后来稍微改了一下那个程序就交了上去,这还是美滋滋 。然后五月中旬的时候写了一个2048小游戏,由于写完第二个版本第二天就生病了,休养了好几天才缓过来,最后嫌麻烦就没发园子里了,直接挂Github上了 。然后又忙着准备期末,期末玩休息了5天又开始数据库课程设计 , 用Java写了一个小系统,花了一个星期左右 , 写了差不多3500行了 。然后写完就放学了 , 放学的日子就感觉自己开始懒散了 , 静不下心来写东西,每天睡觉,看一些无聊的视频 。。。前几天算是缓过神来,找回了脑子,于是又开始想着在放弃Java之前再多写一点东西,于是花了两个下午写了一个时钟,感觉看着还行 。
-----------------------------------以下是正经话-----------------------------------
其实这个程序并不难,主要是用Java 2D画图,弄一个线程,每隔一秒 , 依照时针分针秒针各自的速度重新计算它们走过的角度(相对12点方向顺时针偏离的角度) , 利用这个角度算出它们各自端点的坐标 , 重绘一次表针,达到刷新的目的,最后时限走动的效果,思路很简单 , 清晰明了 。但是关键是这个画图呀,想着很容易,重写继承过来的JFrame里的paint函数就行,最后发现效果并不是期待的那样 , 而是一闪一闪的,于是去寻找原因,结果,尼玛,原来水这么深,一下子触到了知识盲区,什么repaint的实现细节,先调用update,再调用paint,update函数又是如何工作的,慢慢弄懂了它为什么会一闪一闪的原因,然后寻找解决办法,然后就看到了什么双缓冲原理,WTFjava钟小程序代码!什么玩意呀 , 根本没听过,然后乖乖地去看那东西的实现原理,又看到有人建议不要去重写paint方法,说是会遇到诸多意想不到的问题,建议去重写paintComponent方法,可是书本上画图的例子都是重写paint方法呀,况且我也没听过paintComponent,无奈 , 又去看了一下paint的工作原理,原来它会调用paintComponent,最后放弃了自己实现双缓冲 , 乖乖改用了paintComponent 。。。
其实我还遇到了几个脑残的问题,比如在画秒针时,直接把画时针的部分代码复制下来,最后忘了改掉走动的速度,结果运行时还在想秒针怎么走不动呢?原谅我是个逗逼,还有什么秒针逆时针走动,最后才发现算坐标时误把y轴正方向当成了向上,哎 。。。
Anyway,最后运行的效果还是挺好的
3c55d177347f0c89c364f17fe0859265.png
-----------------------------------以下是代码-----------------------------------
1 import java.awt.*;2 importjava.util.Date;3 import javax.swing.*;4
5 public class Clock extendsJComponent{6 /**
7 *8 */
9 private static final long serialVersionUID = -5379472973578609775L;10 private Font f = new Font("微软雅黑",Font.PLAIN,15);11 private Font f2 = new Font("微软雅黑",Font.BOLD,15);12 private JLabel l = new JLabel("当前时间java钟小程序代码:");13 private JLabel display = newJLabel();14 private JLabel display2 = new JLabel("");15 private int hour = 0;16 private int min = 0;17 private int sec = 0;18 private Date now = newDate();19 privateGraphics2D g;20 final double PI =Math.PI;21 private String strTime = "";22
23 @SuppressWarnings("deprecation")24 publicClock(){25 add(l);26 l.setBounds(120, 320, 80, 20);27 l.setFont(f);28 add(display);29 display.setBounds(195, 320, 80, 20);30 display.setFont(f);31 display.setBorder(BorderFactory.createLineBorder(Color.black));32 add(display2);33 display2.setBounds(90, 350, 250, 20);34 display2.setFont(f);35 hour =now.getHours();36 min =now.getMinutes();37 sec =now.getSeconds();38 setVisible(true);39 }40
41 public voidpaintComponent(Graphics g1){42 doublex,y;43 super.paintComponent(g1);44 g =(Graphics2D) g1;45 //反锯齿开关开
46 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);47
48 //画表盘
49 g.setPaint(new GradientPaint(5,40,Color.blue,15,50,Color.yellow,true));50 g.setStroke( new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));51 g.drawOval(75, 40, 250, 250);52 g.fillOval(195, 160, 10, 10);53 g.setColor(Color.black);54
55 //画60个点
56 for(int i = 0;i60;i)57 {58 double[] co = new double[2];59 co = paint_Dot(i * 2 * PI / 60);60 x = co[0];61 y = co[1];62 if(i == 0 || i == 15 || i == 30 || i == 45)//画3,6,9,12四个大点
63 {64 g.fillOval((int)(x - 5200),(int)(y - 5165),10,10);65 }66 else//其java钟小程序代码他小点
67 {68 g.fillOval((int)(x - 2.5200),(int)(y - 2.5165),5,5);69 }70 }71
72 //画四个数字
73 g.setFont(f2);74 g.drawString("3", 300, 171);75 g.drawString("6", 195, 273);76 g.drawString("9", 91, 171);77 g.drawString("12", 190, 68);78
79 //画时针,分针,秒针
80 paint_HourPointer(hour*3600min*60sec,g);//时针走过的秒数
81 paint_MinutePointer(min*60sec,g);//分针走过的秒数
82 paint_SecondPointer(sec,g);//秒针走过的秒数
83 }84
85 public voidshowUI(){86 newThread() {87 @SuppressWarnings("deprecation")88 public voidrun() {89 while (true)90 {91 now = newDate();92 hour =now.getHours();93 min =now.getMinutes();94 sec =now.getSeconds();95 try{96 Thread.sleep(1000);97 } catch(InterruptedException ex) {98 ex.printStackTrace();99 }100 showTime();101 repaint();102 }103 }104 }.start();105 }106
107 public void paint_HourPointer(int second,Graphics2D g){//second表示当前时间的时针相对00:00:00走了多少秒
108 doublex,y,angle;109 angle = second * PI / 21600;//时针的速度为PI/21600 (rad/s)
110 x = 20060 *Math.sin(angle);111 y = 165 - 60 *Math.cos(angle);112 g.setStroke( new BasicStroke(5,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));113 g.setPaint(new GradientPaint(200,165,Color.red,260,165,Color.blue,true));114 g.drawLine(200, 165, (int)x, (int)y);115 }116
117 public void paint_MinutePointer(int second,Graphics2D g){//second表示当前时间的分针相对00:00:00走了多少秒
118 doublex,y,angle;119 angle = second * PI / 1800;//分针的速度为PI/1800 (rad/s)
120 x = 20080 *Math.sin(angle);121 y = 165 - 80 *Math.cos(angle);122 g.setStroke( new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));123 g.setPaint(new GradientPaint(200,165,Color.magenta,280,165,Color.blue,true));124 g.drawLine(200, 165, (int)x, (int)y);125 }126
127 public void paint_SecondPointer(int second,Graphics2D g){//second表示当前时间的秒针相对00:00:00走了多少秒
128 doublex,y,x1,y1,x2,y2,x3,y3,angle;129 double cos = 90 / Math.sqrt(8125);//90*90 5*5
130 double sin = 5 / Math.sqrt(8125);131 angle = second * PI / 30;//时针的速度为PI/30 (rad/s)
132 x = 20095 *Math.sin(angle);133 y = 165 - 95 *Math.cos(angle);134 x1 = 20020 * Math.sin(anglePI);135 y1 = 165 - 20 * Math.cos(anglePI);136 x2 = 200Math.sqrt(8125)* ( Math.sin(angle)*cos - Math.cos(angle)*sin ); //sin(a-b)
137 y2 = 165 - Math.sqrt(8125)* ( Math.cos(angle)*cosMath.sin(angle)*sin ); //cos(a-b)
138 x3 = 200Math.sqrt(8125)* ( Math.sin(angle)*cosMath.cos(angle)*sin ); //sin(a b)
139 y3 = 165 - Math.sqrt(8125)* ( Math.cos(angle)*cos - Math.sin(angle)*sin ); //cos(a b)
140 g.setStroke( new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));141 g.setPaint(new GradientPaint(180,165,Color.CYAN,295,165,Color.MAGENTA,true));142 g.drawLine((int)x1, (int)y1, (int)x, (int)y);143 g.drawLine((int)x2, (int)y2, (int)x, (int)y);144 g.drawLine((int)x3, (int)y3, (int)x, (int)y);145 }146
147 public double[] paint_Dot(doubleangle){148 double[] co = new double[2];149 co[0] = 115 * Math.cos(angle);//横坐标
150 co[1] = 115 * Math.sin(angle);//纵坐标
151 returnco;152 }153
154 @SuppressWarnings("deprecation")155 private voidshowTime(){156 String date;157 int hour_temp = hour,min_temp = min,sec_temp =sec;158 sec_temp= 1;159 if(sec_temp = 60)160 {161 sec_temp = 0;162 min_temp= 1;163 }164 if(min_temp=60){165 min_temp=0;166 hour_temp =1;167 }168 if(hour_temp10)169 strTime = "0"hour_temp":";170 else
171 strTime = ""hour_temp":";172
173 if(min_temp10)174 strTime = strTime"0"min_temp":";175 else
176 strTime = strTime""min_temp":";177
178 if(sec10)179 strTime = strTime"0"sec_temp;180 else
181 strTime = strTime""sec_temp;182 //在窗体上设置显示时间
183 date = " "(now.getYear() 1900)"年"(now.getMonth() 1)"月"now.getDate()"日 ""星期";184 switch(now.getDay()) {185 case 1:186 date= "一";187 break;188 case 2:189 date= "二";190 break;191 case 3:192 date= "三";193 break;194 case 4:195 date= "四";196 break;197 case 5:198 date= "五";199 break;200 case 6:201 date= "六";202 break;203 case 7:204 date= "日";205 break;206 }207 date= " CST";208 strTime = " "strTime;209 display.setText(strTime);210 display2.setText(date);211 }212
213 public static voidmain(String args[]){214 Clock c = newClock();215 c.showUI();216 JFrame f = new JFrame("Clock By XJX");217 Image img=Toolkit.getDefaultToolkit().getImage("title.gif");//窗口图标
218 f.setIconImage(img);219 f.setSize(400,400);220 f.setResizable(false);221 f.add(c, BorderLayout.CENTER);222 f.setLocationRelativeTo(null);223 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);224 f.setVisible(true);225 }226 }
相关资源:...仿真和代码)_51单片机做实时时钟显示-小程序文档类资源-CSDN文库
文章知识点与官方知识档案匹配
Java技能树首页概览
91513 人正在系统学习中
打开CSDN APP,看更多技术内容
不会编程也能亲手编写一个桌面数字小时钟程序,仅3步轻松实现_程序员笑...
第一步 打开电脑自带的记事本并编写以下脚本 。源码: @echo off Title 小时钟丨编程狮(w3cschool.cn) @mode con cols=50lines=5color00:main cls echo.echo 时间:%time靐o.echo 日期:趖e靐o.ping-n20.0.0.0nulgotomain ...
继续访问
OpenGL织梦之旅【第二章】编写一个的钟表程序_plusplus7的博客-CSDN...
在配置好了glut环境以后,运行了第一个opengl程序以后,我们现在就来尝试编写一个实用一点的程序——时钟程序 。如图: 制作这样一个程序,对于初学者来说,感觉上或许很难,但是只要我们一步一步地慢慢来,总会有熟练掌握的那一天 。“包子要...
继续访问
Java时钟程序(绘制了一个带时针 分针 秒针的仿真时钟)
这是我自己写的一个java时钟程序 当然你可以自己改动 喜欢就拿去吧
Java时钟小程序
Java时钟小程序,可以显示出当前的系统时间 ,
java 钟_JAVA时钟 - 代码块 - 扣丁书屋 -
import javax.swing.*;import java.awt.*;import java.util.*;import java.lang.Thread;import java.text.DecimalFormat;public class DongClock extends JPanel {/*** @param args*/private int hour;private int m...
继续访问
使用eclipse设计开发的安卓软件,闹钟定时器
使用eclipse开发的安卓软件--闹钟定时器,学生开发的小作业 。学生党可以下载参考的软件,首次学习 , 还在很多的不足,不喜勿喷,谢谢!
java 电子时钟_Java电子时钟实现代码
用Java编写一个程序 , 实现动态的文字时钟,以秒计时 。知识点:1、线程(Thread)2、Applet类:它最先执行iniy()方法,完成后进入初始态;然后马上执行start()方法,Applet程序进入运行状态;线程也在start()中被启动,然后执行run()中的内容 。当Applet程序所在的浏览器图标化或者转入其他页面时,Applet程序马上执行stop()方法3、Java不像C,只能...
继续访问
java时钟_Java实现时钟小程序
哎 , 好久没上博客园发东西了,上一次还是两个月前的五一写的一篇计算器博客,不过意外的是那个程序成了这学期的Java大作业 , 所以后来稍微改了一下那个程序就交了上去,这还是美滋滋 。然后五月中旬的时候写了一个2048小游戏,由于写完第二个版本第二天就生病了,休养了好几天才缓过来,最后嫌麻烦就没发园子里了,直接挂Github上了 。然后又忙着准备期末 , 期末玩休息了5天又开始数据库课程设计,用Java写了一个小...
继续访问
java 简易时钟
简易时钟演示图一、界面设计二、获取时间与设置时间三、完整代码总结 演示图 非常质朴的时钟 一、界面设计 JFrame app=new JFrame("电子闹钟"); Container c=app.getContentPane(); //设置一个面板容器 面板为初始设置面板 JLabel clock=new JLabel("电子闹钟"); clock.setHorizontalTextPosition(JLabel.CENTER); /.
继续访问
时钟程序设计java_Java数字时钟实现代码详解
这是一个数字钟表程序,主要功能是从系统中获取当前的系统时间然后再将其显示在数字时钟上,由于整个的数字时钟都是用函数构成的,所以它可以实现一般的数 字时钟所不具有的功能,比如说它可以被鼠标指针拖动到窗口的任意位置,除此之外它还可以实现钟表大小随鼠标滚轮的滚动而变大变小的操作 。package TheClock;import java.awt.*;import javax.swing.*;import ...
继续访问
钟表java程序教程 创建_JAVA--编写时钟的程序
代码如下:import java.awt.*;import java.text.DateFormat;import java.util.*;import javax.swing.*;public class ClockDemo extends JFrame implements Runnable{Thread clock;final int Xpoint=180;final int Ypoint=...
继续访问
java时钟代码_JAVA实现时钟
时钟题目内容:在课程所给的时钟程序的基础上修改 。这一周的编程题是需要你在课程所给的时钟程序的基础上修改而成 。但是我们并不直接给你时钟程序的代码,请根据视频自己输入时钟程序的Display和Clock类的代码,然后来做这个题目 。我们需要给时钟程序加上一个表示秒的Display,然后为Clock增加以下public的成员函数:public Clock(int hour, int minute, int...
继续访问
JAVA实现时钟
时钟 题目内容: 在课程所给的时钟程序的基础上修改 。这一周的编程题是需要你在课程所给的时钟程序的基础上修改而成 。但是我们并不直接给你时钟程序的代码 , 请根据视频自己输入时钟程序的Display和Clock类的代码 , 然后来做这个题目 。我们需要给时钟程序加上一个表示秒的Display,然后为Clock增加以下public的成员函数: public Clock(int hour, i...
继续访问
java gui 做闹钟,用JAVA怎样编写一个可以在eclipse中运行的闹钟程序?
首先java程序的运行你需要下载和安装JDK,这是java运行的必备环境 。首先你要在桌面上找到是eclipes,双击打开 。在eclipes启动的过程中,会弹出一个窗口,让你填写java工作区的保存目录,在这个目录下会保存你写的所有的源代码文件 , 小编建议你不要把工作区放在C盘,你如果重装系统的话,源代码就会全部损失 。ecplies启动完成之后,会有一个欢迎页面,这个不用管,直接点击左上方的差号,取消...
继续访问
自定义时钟控件
通过继承View类,我们可以自定义自己需求的复杂控件 。本例实现了一个自定义的时钟 。效果如下: 代码如下: package com.example.test; import java.util.Calendar; import android.content.Context; import android.graphics.Bitmap; import android.gra
继续访问
java时钟课程设计,Java课程设计-时钟图形模拟
《Java课程设计-时钟图形模拟》由会员分享,可在线阅读,更多相关《Java课程设计-时钟图形模拟(19页珍藏版)》请在人人文库网上搜索 。1、课 程 设 计 任 务 书专 业通信工程班 级13级四班姓 名张凯铭设 计 起 止 日 期设计题目:时钟图形模拟设计任务(主要技术参数):硬件环境:CPU:Pentium 2.8GHz以上 内存:256MB以上 硬盘空间:80G以上软件环境:(1)操作系统:...
继续访问
最新发布 用Java编写一个时钟
public class Display { private int value=https://www.04ip.com/post/0; private int limate=0; public void increase() { value; if(value==limate) { value=0; } } public int getvalue() { return value; } Display(int limate){ this.limate=limate; } } public clas.
继续访问
java编写时钟程序_Java编写时钟 Applet 程序
简单分析:package clockApplet;import java.applet.Applet;import java.awt.Color;import java.awt.Graphics;import java.util.Calendar;import java.util.GregorianCalendar;public class ClockApplet extends Applet i...
继续访问
java 画图板源代码_非常值得学习的java 绘图板源代码
package minidrawpad;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.io.Serializable;//图形绘制类 用于绘制各种图形//父类,基本图形单元,用到串行的接口,保存使用到//公共的属性放到超类中,...
继续访问
Eclipse编写的Android数字时钟应用实例(用新建Runnable的方法)
Eclipse V4.2.0用新建Runnable的方法编写的Android数字时钟应用实例 。
java时钟日历_java日历时钟小程序
java日历时钟小程序 java日历时钟小程序 Java时钟程序,按类粘贴至编译器编译即可使用 。import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListene...
继续访问
java显示一个钟表_java实现时钟效果
本文实例为大家分享了java实现时钟效果的具体代码 , 供大家参考,具体内容如下实现效果如图:Java代码:文件一:ClockPanel.javaimport static java.util.Calendar.HOUR;import static java.util.Calendar.MILLISECOND;import static java.util.Calendar.MINUTE;import...
继续访问
时钟程序很难做吗
java编写时钟程序
求java经典小程序代码代码如下java钟小程序代码:
public class HelloWorld {
public static void main(String []args) {
int a = 3, b = 7 ;
System.out.println("Hello World!");
}
public static int f(int a, int b){
return a*aa*bb*b;
}
}
结果如下java钟小程序代码:
谁能给个JAVA的小程序代码,越小越好!这是我晓得的最简单的java小程序代码了你可以看看:
package com.kenki.emp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;
import java.sql.*;
public class emp extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String code = request.getParameter("code");
String name = request.getParameter("name");
String pay = request.getParameter("pay");
System.out.println("empcode:"code);
System.out.println("name:"name);
System.out.println("pay:"pay);
//创建驱动
new com.microsoft.jdbc.sqlserver.SQLServerDriver();
String strd =
"jdbc:microsoft:sqlserver://localhost:1433;databasename=emp_dates";
String username = "sa";
String pws = "";
try {
java.sql.Connection conn = java.sql.DriverManager.getConnection(
strd, username, pws);
String strs = "insert into emp values(?,?,?)";
java.sql.PreparedStatement pre = conn.prepareStatement(strs);
pre.setString(1, code);
pre.setString(2, name);
pre.setString(3, pay);
pre.execute();
pre.close();
conn.close();
//重定向至查询页面
out.println("成功保存?。?);
response.sendRedirect("emp.html");
} catch (SQLException ss) {
ss.printStackTrace();
response.sendRedirect("/WebModule1/error.html");
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
java写个谢谢~谢谢~谢谢~谢谢~谢谢~谢谢~谢谢~谢谢~谢谢~谢谢~谢谢~import java.io.IOException;
public class run_QQ {
/**
* @param args
* @throws InterruptedException
* @throws IOException
*/
public static void main(String[] args) throws InterruptedException, IOException {
// TODO Auto-generated method stub
for(int i=5;i0;i--){
System.out.println("将在"i"秒后启动QQ");
Thread.sleep(1000);
}
Process a=Runtime.getRuntime().exec("D:\\个人应用\\QQ\\QQ.exe");
}
}
我也不太会你看看吧能达到你的要求一点点
Process a=Runtime.getRuntime().exec("D:\\个人应用\\QQ\\QQ.exe");
exec("");里面改成你QQ的位置就可以了
JAVA 闹钟程序import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
import java.awt.event.*;
public class Alarm extends Applet implements Runnable
{
Thread timer=null; //创建线程timer
Image gif1; //clockp:闹钟的外壳,闹铃和报时物
boolean setflag=false,stopflag=false,cancelflag=false;
Panel setpanel;
//获取声音文件
AudioClip ring=getAudioClip(getCodeBase(), "1.mid");
Button setbutton=new Button("SET");
Button cancelbutton=new Button("CANCEL");
Button stopbutton=new Button("STOP");
//响应按钮事件
private ActionListener setli=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setflag=true;
}
};
private ActionListener cancelli=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setflag=true;
}
};
private ActionListener stopli=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ring.stop();
//清除的方法
//g.clearRect(83,280,20,30);
}
};
Label note1=new Label("Alarm clock:");
//GregorianCalendar提供的是一个日历式的东东,上面又多了很多的参数,是方便操作了不少 。而Date类的功能远不及其,求个和日期有联系的还要自己计算 。
GregorianCalendar cal=new GregorianCalendar();
GregorianCalendar cal2=new GregorianCalendar();
SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//设置时间格式
Date dummy=new Date(); //生成Data对象
String lastdate=df.format(dummy);
Font F=new Font("TimesRoman",Font.PLAIN,14);//设置字体格式
Date dat=null;
Date timeNow;
Color fgcol=Color.blue;
Color fgcol2=Color.darkGray;
Color backcolor=Color.blue;
Label hlabel2,mlabel2,slabel2;//显示时间单位时所用的标签(时、分、秒)
int i;
int s,m,h;
TextField sethour,setmin,setsec;//显示当前时间文本框和定时文本框
//在Applet程序中,首先自动调用初始化完成必要的初始化工作 , 紧接着自动调用start,在进入执行程序和返回到该页面时被调用 , 而从该页面转到别的页面时,stop被调用,关闭浏览器时,执行destroy 。
public void init()//初始化方法
{
int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//显示时间和定时文本框的定位参数
setLayout(null); //将布局管理器初始化为null
setpanel=new Panel();
setpanel.setLayout(null);
setpanel.add(note1);
note1.setBounds(30,100,60,20);
note1.setBackground(backcolor);
note1.setForeground(Color.black);
//定时用的文本框(时、分、秒)
sethour=new TextField("00",5);
setmin=new TextField("00",5);
setsec=new TextField("00",5);
hlabel2=new Label();
mlabel2=new Label();
slabel2=new Label();
//定时的小时文本框的位置、大小
setpanel.add(sethour);
sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);
sethour.setBackground(Color.white);
//在文本框后加入单位“时”
setpanel.add(hlabel2);
hlabel2.setText("h");
hlabel2.setBackground(backcolor);
hlabel2.setForeground(Color.black);
hlabel2.setBounds(fieldx fieldw 3,fieldy2,14,20);
fieldx=fieldx space;
//定时的分钟文本框的位置、大小
setpanel.add(setmin);
setmin.setBounds(fieldx,fieldy2,fieldw,fieldh);
setmin.setBackground(Color.white);
//在文本框后加入单位“分”
setpanel.add(mlabel2);
mlabel2.setText("m");
mlabel2.setBackground(backcolor);
mlabel2.setForeground(Color.black);
mlabel2.setBounds(fieldx fieldw 3,fieldy2,14,20);
fieldx=fieldx space;
//定时的秒文本框的位置、大小
setpanel.add(setsec);
setsec.setBounds(fieldx,fieldy2,fieldw,fieldh);
setsec.setBackground(Color.white);
//在文本框后加入单位“秒”
setpanel.add(slabel2);
slabel2.setText("s");
slabel2.setBackground(backcolor);
slabel2.setForeground(Color.black);
slabel2.setBounds(fieldx fieldw 3,fieldy2,14,20);
//设置闹钟控制按钮(on,off)
setpanel.add(cancelbutton);
setpanel.add(setbutton);
setpanel.add(stopbutton);
cancelbutton.setBounds(90,180,40,20);
setbutton.setBounds(140,180,40,20);
stopbutton.setBounds(522,180,40,20);
setbutton.addActionListener(setli);
cancelbutton.addActionListener(cancelli);
stopbutton.addActionListener(stopli);
stopbutton.setVisible(false);
//将面板加入当前容器中 , 并设置面板的大小和背景色
add(setpanel);
setpanel.setBounds(300,1,250,420);
setpanel.setBackground(backcolor);
/*int xcenter,ycenter,s,m,h;
//闹钟中心点所在位置
xcenter=145;
ycenter=162;
s=(int)cal.get(Calendar.SECOND);
m=(int)cal.get(Calendar.MINUTE);
h=(int)cal.get(Calendar.HOUR_OF_DAY);
//初始化指针位置
lastxs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30 xcenter);
lastys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30 ycenter);
lastxm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25 xcenter);
lastym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25 ycenter);
lastxh=(int)(Math.cos((h*30 m/2)*3.14f/180-3.14f/2)*18 xcenter);
lastyh=(int)(Math.sin((h*30 m/2)*3.14f/180-3.14f/2)*18 ycenter);
lasts=s; */
MediaTracker mt=new MediaTracker(this);//为给定组件创建一个跟踪媒体的MediaTracker对象,把图片添加到被跟踪的图片组
//Java允?Sapplet??HTML所在的位置(decument base)下?d?Y料 , 也允?Sapplet?钠涑淌酱a所在的位置(code base)下?d?Y料 。藉由呼叫getDocumentBase()?cgotCodeBase()可得到URL物件 。?@些函?????湍阏业侥阆胂螺d的?n案的位置
//clockp=getImage(getDocumentBase(),"11.png");
gif1=getImage(getCodeBase(),"2.gif");
//i为id号
mt.addImage(gif1,i);
try
{
mt.waitForAll();
}
catch(InterruptedException e)
{};//等待加载结束
resize(600,420);//设置窗口大小
}
//窗口显示有改变的时候调用paint
public void paint(Graphics g)
{//重写paint()方法
int xh,yh,xm,ym,xs,ys,strike_times;
int xcenter,ycenter;
String today;
xcenter=148;
ycenter=186;
dat=new Date();
//用当前时间初始化日历时间
cal.setTime(dat);
//读取当前时间
s=(int)cal.get(Calendar.SECOND);
m=(int)cal.get(Calendar.MINUTE);
h=(int)cal.get(Calendar.HOUR_OF_DAY);
//换一种时间表达形式
today=df.format(dat);
//指针位置
xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30 xcenter);
ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30 ycenter);
xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25 xcenter);
ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25 ycenter);
xh=(int)(Math.cos((h*30 m/2)*3.14f/180-3.14f/2)*12 xcenter);
yh=(int)(Math.sin((h*30 m/2)*3.14f/180-3.14f/2)*12 ycenter);
//设置字体和颜色
g.setFont(F);
//前景色
g.setColor(getBackground()); //取背景色的
g.drawImage(gif1,75,110,this);
//以数字方式显示年、月、日和时间
g.drawString(today,55,415);
//画指针
g.drawLine(xcenter,ycenter,xs,ys);
g.drawLine(xcenter,ycenter-1,xm,ym); //(x1,y1,x2,y2)
g.drawLine(xcenter-1,ycenter,xm,ym);
g.drawLine(xcenter,ycenter-1,xh,yh);
g.drawLine(xcenter-1,ycenter,xh,yh);
int timedelta;//记录当前时间与闹铃定时的时差
Integer currh,currm,currs;//分别记录当前的时、分、秒
Date dat2=new Date();
cal2.setTime(dat2);
//读取当前时间
currh=(int)cal2.get(Calendar.SECOND);
currm=(int)cal2.get(Calendar.MINUTE);
currs=(int)cal2.get(Calendar.HOUR_OF_DAY);
//这样做的话说我API已过时
//timeNow=new Date();
//currh=new Integer(timeNow.getHours());
//currm=new Integer(timeNow.getMinutes());
//currs=new Integer(timeNow.getSeconds());
if(setflag)
{ //判断是否设置了闹钟
//判断当前时间是否为闹钟所定的时间
if((currh.intValue()==Integer.valueOf(sethour.getText()).intValue())(currm.intValue()==Integer.valueOf(setmin.getText()).intValue())(currs.intValue()==Integer.valueOf(setsec.getText()).intValue()))
{
ring.play();
g.drawImage(gif1,83,280,this);
stopbutton.setVisible(true);
}
timedelta=currm.intValue()*60 currs.intValue()-Integer.valueOf(setmin.getText()).intValue()*60-Integer.valueOf(setsec.getText()).intValue();
if((timedelta=30))
{
//若当前时间与闹钟相差时间超过30秒,闹钟自动停
ring.stop();
//清除的方法
g.clearRect(83,280,20,30);
}
}
dat=null;
}
public void start()
{
if(timer==null)
{
timer=new Thread(this);//将timer实例化
timer.start();
}
}
public void stop()
{
timer=null;
}
//给创建线程后start之后自动执行的函数
public void run()
{
//在run()方法中 , 调用repaint()方法,以重绘小程序区,进行时钟显示的更新 。接着调用sleep方法让当前线程(也就是我们创建的线程clockthread)睡眠1000毫秒,因为我们每秒钟要更新一下显示,所以让它睡眠1秒
while(timer!=null)
{
try
{
timer.sleep(1000);
}
catch(InterruptedException e)
{}
//调用repaint时,会首先清除掉paint方法之前的画的内容,再调用paint方法
repaint();//刷新画面
}
timer=null;
}
//当AWT接收到一个applet的重绘请求时,它就调用applet的 update() , 默认地 , update() 清除applet的背景,然后调用 paint() 。重载 update() , 将以前在paint()中的绘图代码包含在update()中,从而避免每次重绘时将整个区域清除
//有两种方法可以明显地减弱闪烁:重载 update()或使用双缓冲 。
//使用双缓冲技术:另一种减小帧之间闪烁的方法是使用双缓冲,它在许多动画Applet中被使用 。其主要原理是创建一个后台图像,将需要绘制的一帧画入图像,然后调用DrawImage()将整个图像一次画到屏幕上去;好处是大部分绘制是离屏的,将离屏图像一次绘至屏幕上比直接在屏幕上绘制要有效得多,大大提高做图的性能 。
// 双缓冲可以使动画平滑,但有一个缺点 , 要分配一张后台图像,如果图像相当大,这将需要很大一块内存;当你使用双缓冲技术时,应重载 update() 。
public void update(Graphics g)
{
Image offscreen_buf=null;
//采用双缓冲技术的update()方法
if(offscreen_buf==null)
offscreen_buf=createImage(600,420);
Graphics offg=offscreen_buf.getGraphics();
offg.clipRect(1,1,599,419);
paint(offg);
Graphics ong=getGraphics();
ong.clipRect(1,1,599,419);
ong.drawImage(offscreen_buf,0,0,this);
}
/** Creates a new instance of AlarmClock */
}
【java钟小程序代码 java 小程序】关于java钟小程序代码和java 小程序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读