课程设计(综合实验)报告( 2011 -- 20112 年度第 1 学期)名称:课程或实验名称题目:迷你闹钟的设计与实现院系:经济与管理学院班级:学号:学生姓名:指导教师:设计周数:一周成绩:日期:2012 年01 月06 日一、课程设计(综合实验)的目的与要求➢设计目的:通过迷你闹钟的设计和实现,考察学生对JA V A语言的掌握情况,并提高学生对图形用户界面的设计和编程能力。
➢设计要求:1.显示时间功能:迷你闹钟可以即时显示当前时间。
2.闹钟功能:能够根据用户设置的时间来提醒用户,可以用声音或者文字进行提醒。
3.时间设置功能:提供菜单来设置时钟的当前时间,和闹钟的提醒时间。
4.界面设计:可模拟真实钟表界面,也可以用数字界面显示。
要求美观、实用和个性化。
二、设计(实验)正文1.设计迷你闹钟的方法1.1利用CardLayout布局实现时间界面和设置闹钟界面的转换1.2在时间界面设置label1和label2实现当前时间的数字化显示,并利用Date类和SimpleDateFormate类实现界面显示“上午好”或“下午好”。
在label1上绘制模拟钟表实现模拟钟表的界面。
1.3在闹钟设置面板上添加当前时间,通过文本框和标签设置需要设置的闹钟的时、分、秒。
设置列表框提供闹钟的音乐以供选择。
设置开关按钮决定闹钟的开始和结束2.遇到问题2. 1 设置迷你闹钟的界面化时遇到问题通过参考已有的程序加以解决2. 2 设置“闹铃时间”、“开关按钮”、“背景音乐列表框”时,事件的触发和监控遇到问题,经过反复修改调试得到解决。
2. 3 闹铃背景音乐格式设置遇到问题,经过调试和格式转换解决3.待解决问题闹钟的思想是“北京时间”,所以背景音乐中有《天涯共此时》音乐,界面上有北京时间等标识本来想在界面上添加一幅具有北京特色的图片做背景,但反复尝试,遗憾的是均未能成功,添加的背景图片总被上层面板遮挡,希望老师给以指导。
三、课程设计(综合实验)总结或结论总结图形界面设计由界面设计和事件响应两部分组成,图形界面使得用户和程序之间方便地进行交互。
四、参考文献[1] 耿祥义,张跃平, java大学实用教程. 电子工业出版社, 第二版. 2008年9月[2]梁春燕,java课程课件附录(设计流程图、程序、表格、数据等)package javaapplication;import java.applet.Applet;import java.util.Calendar;import java.util.Date;import java.text.SimpleDateFormat;import java.io.File;import .URI;import .URL;import java.applet.AudioClip;import javax.swing.*;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import java.util.GregorianCalendar;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Date;import javax.swing.JFrame;class Alarm extends JFrame implements Runnable, ActionListener {Thread timer = null;JLabel hour1, minute1, second1, hour2, minute2, second2;JLabel nowtime,nowdate, shownow, showset, showring, showswitch;JTextField showhour, showminute, showsecond, sethour, setminute, setsecond;JButton on, off;JScrollPane js;Font F = new Font("TimesRoman", Font.PLAIN, 14);Font mf=new Font("Courier",Font.BOLD,24);JPanel panel2;JTabbedPane t = new JTabbedPane();int time = 1000;boolean can = true, alarm = false, doring = false;int i=0;String musicname[] = { "天涯共此时.wav","清脆自然.wav", "超好听的纯音乐.wav",}; JList jlist;int fixh = 0, fixm = 0, fixs = 0;File musicfile;URI uri;URL url;AudioClip clip;Alarm() {super("桌面小闹钟");add(t);t.setFont(F);Panel1 panel1 = new Panel1();panel2 = new JPanel();t.addTab("北京时间", panel1);t.addTab("闹铃设置", panel2);panel1.setLayout(null);Timer timer = new Timer(1000, this);timer.start();nowtime = new JLabel();panel1.add(nowtime);nowtime.setBounds(0, 200, 300, 30);nowtime.setFont(mf);nowdate=new JLabel();panel1.add(nowdate);nowdate.setBounds(70,0,300,30);nowdate.setFont(mf);panel2.setLayout(null);shownow = new JLabel("现在时间"); showset = new JLabel("闹钟时间"); showring = new JLabel("闹铃设置"); showswitch = new JLabel("闹钟开关"); hour1 = new JLabel("时");minute1 = new JLabel("分");second1 = new JLabel("秒");hour2 = new JLabel("时");minute2 = new JLabel("分");second2 = new JLabel("秒");on = new JButton("on");off = new JButton("off");showhour = new JTextField(" 00", 2); showminute = new JTextField(" 00", 2); showsecond = new JTextField(" 00", 2); sethour = new JTextField("00", 2); setminute = new JTextField("00", 2); setsecond = new JTextField("00", 2);panel2.add(shownow);panel2.add(showset);panel2.add(showring);panel2.add(showswitch);panel2.add(hour1);panel2.add(minute1);panel2.add(second1);panel2.add(hour2);panel2.add(minute2);panel2.add(second2);panel2.add(on);panel2.add(off);panel2.add(showhour);panel2.add(showminute);panel2.add(showsecond);panel2.add(sethour);panel2.add(setminute);panel2.add(setsecond);shownow.setBounds(0, 5, 80, 15); shownow.setFont(F);hour1.setBounds(100, 5, 20, 15); hour1.setFont(F);minute1.setBounds(160, 5, 20, 15); minute1.setFont(F);second1.setBounds(220, 5, 20, 15); second1.setFont(F);showset.setBounds(0, 30, 80, 15); showset.setFont(F);hour2.setBounds(100, 30, 20, 15); hour2.setFont(F);minute2.setBounds(160, 30, 20, 15); minute2.setFont(F);second2.setBounds(220, 30, 20, 15); second2.setFont(F);showhour.setBounds(70, 0, 25, 25); showminute.setBounds(130, 0, 25, 25); showsecond.setBounds(190, 0, 25, 25);sethour.setBounds(70, 25, 25, 25); setminute.setBounds(130, 25, 25, 25); setsecond.setBounds(190, 25, 25, 25);showring.setBounds(0, 250, 80, 15); showring.setFont(F);showswitch.setBounds(0, 420, 80, 15); showswitch.setFont(F);on.setBounds(90, 160, 50, 30);on.setFont(F);off.setBounds(165, 160, 50, 30);off.setFont(F);on.addActionListener(this);off.addActionListener(this);jlist = new JList(musicname);jlist.setVisibleRowCount(5);jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);jlist.setFont(F);js = new JScrollPane(jlist);panel2.add(js);js.setBounds(70, 50, 165, 100);jlist.addListSelectionListener(new ListSelectionListener() {public void valueChanged(ListSelectionEvent event) {i = jlist.getSelectedIndex();}});setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(300,300 );setVisible(true);}public void actionPerformed(ActionEvent e) {if (e.getSource() == on)alarm = true;if (e.getSource() == off){alarm = false;clip.stop();doring = false;} }public void start() {if (timer == null) {timer = new Thread(this);timer.start();}}public void run() {while (timer != null){Date date = new Date();SimpleDateFormat matter1=new SimpleDateFormat(" HH:mm:ss a");SimpleDateFormat matter2=new SimpleDateFormat("yyyy-MM-dd E");nowtime.setText("北京:"+matter2.format(date));nowdate.setText( matter1.format(date)+"好");Calendar cal = new GregorianCalendar();Integer showh, showm, shows;showh = new Integer(cal.get(Calendar.HOUR_OF_DAY));showm = new Integer(cal.get(Calendar.MINUTE));shows = new Integer(cal.get(Calendar.SECOND));if (showh < 10)showhour.setText("0" + showh.toString());elseshowhour.setText(showh.toString());if (showm < 10)showminute.setText("0" + showm.toString());elseshowminute.setText(showm.toString());if (shows < 10)showsecond.setText("0" + shows.toString());elseshowsecond.setText(shows.toString());try {fixh=Integer.parseInt(sethour.getText());fixm=Integer.parseInt(setminute.getText());fixs=Integer.parseInt(setsecond.getText());}catch (Exception e) {}if (alarm){int h=showh,m=showm,s=shows;if ((h==fixh)&&(m==fixm)&&(s==fixs)){doring = true;}}if (doring){musicfile = new File(musicname[i]);uri = musicfile.toURI();try{url = uri.toURL();}catch (Exception exp) {}clip = Applet.newAudioClip(url);clip.play();doring=false;}repaint();try{timer.sleep(time);}catch (InterruptedException e) {}can = true;}timer = null;}public void stop(){timer = null;}}class Panel1 extends JPanel{int hour, minute, second;public void paintComponent(Graphics g) {super.paintComponent(g);Calendar cal = new GregorianCalendar();hour = cal.get(Calendar.HOUR_OF_DAY);minute = cal.get(Calendar.MINUTE);second = cal.get(Calendar.SECOND);int clockRadius = (int) (Math.min(getWidth(), getHeight()) * 0.3);int xCenter = getWidth() / 2;int yCenter = getHeight() / 2;g.drawOval(xCenter - clockRadius, yCenter - clockRadius,2 * clockRadius, 2 * clockRadius);g.drawString("12", xCenter - 5, yCenter - clockRadius + 12);g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);int sLength = (int) (clockRadius * 0.9);int xSecond = (int) (xCenter + sLength* Math.sin(second * (2 * Math.PI / 60)));int ySecond = (int) (yCenter - sLength* Math.cos(second * (2 * Math.PI / 60)));g.setColor(Color.red);g.drawLine(xCenter, yCenter, xSecond, ySecond);int mLength = (int) (clockRadius * 0.65);int xMinute = (int) (xCenter + mLength* Math.sin(minute * (2 * Math.PI / 60)));int yMinute = (int) (yCenter - mLength* Math.cos(minute * (2 * Math.PI / 60)));g.setColor(Color.blue);g.drawLine(xCenter, yCenter, xMinute, yMinute);int hLength = (int) (clockRadius * 0.5);int xHour = (int) (xCenter + hLength* Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));int yHour = (int) (yCenter - hLength* Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));g.setColor(Color.black);g.drawLine(xCenter, yCenter, xHour, yHour);}}public class JavaApplication {public static void main(String[] args) {Alarm alarm=new Alarm();alarm.start();}}注:根据课程设计、综合实验的内容将标题任选其一。