欢迎来到天天文库
浏览记录
ID:40557068
大小:98.50 KB
页数:7页
时间:2019-08-04
《java时钟—代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、昆明理工大学信息工程与自动化学院学生实验报告(2011—2012学年第1学期)课程名称:Java上机报告开课实验室:信自楼4442011年10月23日年级、专业、班计科092学号200910405214姓名徐兴繁成绩实验项目名称图形界面与线程设计指导教师舒国锋教师评语教师签名:年月日实验目的:1、学会图像界面的设计以及事件的监听(掌握按钮的监听和微调的监听)2、学会多线程的控制3、理解微调的功能和作用实验原理:时钟界面的设计,学会设计图形以及线程的控制,时间每隔1s加1.最重要的时钟指针的控制,每当Spinner的只改变,就要改变指
2、针的位置。时钟界面的指针的位置的改变是通过改变终点的坐标实验操作:时钟界面,时钟界面的操作分为两块,首先是对Spinner的设置,要设置为时间格式,通过API的查找,找到日期格式的函数,然后经过字符串的截取得到时间格式,通过线程的学习,设置时间每隔一秒改变一次。其次是画时钟界面,首先计算圆心画一个圆,并在圆上标出12个点数和三根线段(起点坐标为圆心),通过对Spinner监听得到的返回值并进行坐标的计算,画出时针,分针,秒针的终点坐标位置。代码如下:importjava.awt.*;importjava.awt.BasicStrok
3、e;importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.text.DateFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjavax.swing.JSpinner;importjavax.swing.JFrame;importj
4、avax.swing.SpinnerDateModel;importjavax.swing.JPanel;importjavax.swing.JButton;importjava.awt.event.ActionListener;importjava.awt.event.ActionEvent;importjava.awt.event.*;importjavax.swing.event.*;importjavax.swing.Timer;publicclassClockimplementsMouseListener,ChangeLi
5、stener{ClockPanelcpanel;publicClockSpinnercspinner;Calendarmycal;JFramecframe;intk=0;Timermanager;publicClock(){this.mycal=Calendar.getInstance();this.cpanel=newClockPanel(this.mycal);this.cspinner=newClockSpinner(this.mycal);cframe=newJFrame("时钟界面");this.cspinner.addC
6、hangeListener(this);this.cframe.addMouseListener(this);this.cpanel.addMouseListener(this);this.cframe.setBounds(300,300,300,300);this.cframe.getContentPane().add(cpanel);JPanelpanel=newJPanel();panel.add(cspinner);this.cframe.getContentPane().add(panel,"South");this.cf
7、rame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.cframe.setVisible(true);ActionListenerac=newActionListener(){publicvoidactionPerformed(ActionEvente){cspinner.nextvalue();cpanel.repaintp(cspinner.getvalue());}};this.manager=newTimer(1000,ac);manager.start();}pu
8、blicvoidstateChanged(ChangeEvente){if(k==0)cpanel.repaintp(cspinner.getvalue());}publicvoidmouseClicked(MouseEvente){
此文档下载收益归作者所有