收藏 分享(赏)

java编程闹钟1.doc

上传人:hyngb9260 文档编号:5957029 上传时间:2019-03-22 格式:DOC 页数:11 大小:102.50KB
下载 相关 举报
java编程闹钟1.doc_第1页
第1页 / 共11页
java编程闹钟1.doc_第2页
第2页 / 共11页
java编程闹钟1.doc_第3页
第3页 / 共11页
java编程闹钟1.doc_第4页
第4页 / 共11页
java编程闹钟1.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、编写时钟程序,要求画出时钟面板,时针,分针,秒针,可设定或解除闹钟时间,并提供闹钟服务相应的文字提示功能与声音提示功能。最佳答案 自己之前做过的一个: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; publi

2、c 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;JMenuIte

3、m 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();/* * 方法说明:实现 ActionListener 类必须过载的方法*/public static void main(String args) Clock cp = new Cl

4、ock();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.addActionListen

5、er(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.a

6、ddActionListener(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(“

7、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(“静音

8、“);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

9、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);

10、ClockPanel clock = new ClockPanel();contentPane.add(clock, BorderLayout.CENTER);/ 窗体添加事件监听,监听秒表的触发ActionListener taskPerformer = new ActionListener() public void actionPerformed(ActionEvent evt) repaint();new Timer(delay, taskPerformer).start();/* * 方法说明:绘制图形*/Color C1 = Color.lightGray;/ 外圈颜色Color

11、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(L0 +

12、30, T0 + 30, h - 60, h - 60);g.setColor(C2);g.drawOval(L0 + 31, T0 + 31, h - 62, h - 62);g.setColor(C3);g.fillOval(L0 + 50, T0 + 50, h - 100, h - 100);g.setColor(C2);g.drawOval(L0 + 51, T0 + 51, h - 102, h - 102);r = h / 2 - 30;x0 = 30 + r - 5 + L0;y0 = 30 + r - 5 - T0;ang = 60;for (int i = 1; i 0)

13、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.setColo

14、r(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.dr

15、awLine(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();/如果撤消设置时间,就什

16、么打印 nullif(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;/ 判断输入的是不

17、是数字for (int i = 0; i = 24 | newHou = 60 | newHou 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, “请输入喜欢的边

18、线颜色(0-255)“, “128“);if(color=null)elseif (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.showInputDia

19、log(this, “请输入喜欢的内盘颜色(0-255)“, “128“);if(color=null)elseif (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 = JOptio

20、nPane.showInputDialog(this, “请输入喜欢的背景颜色(0-255)“, “128“);if(color=null)elseif (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(“color

21、5“);color = JOptionPane.showInputDialog(this, “请输入喜欢的秒针颜色(0-255)“, “128“);if(color=null)elseif (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.

22、out.println(“color6“);color = JOptionPane.showInputDialog(this, “请输入喜欢的分针颜色(0-255)“, “128“);if(color=null)elseif (Integer.parseInt(color) 255)JOptionPane.showInputDialog(this, “请输入喜欢的分针颜色(0-255)“, “128“);else c = new Color(Integer.parseInt(color);C6 = c; if(e.getActionCommand() = “color7“)String col

23、or;Color c;System.out.println(“color7“);color = JOptionPane.showInputDialog(this, “请输入喜欢的时针颜色(0-255)“, “128“);if(color=null)elseif (Integer.parseInt(color) 255)JOptionPane.showInputDialog(this, “请输入喜欢的时针颜色(0-255)“, “128“);else c = new Color(Integer.parseInt(color);C7 = c; if(e.getActionCommand() = “

24、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 的时候在前面添加字符 0if (hour 10) Hour = “0“ + String.valueOf(hour); else Hour = “ + String.valueOf(hour);if (minute 10) Minute = “0“ + String.valueOf(minute); else Minute = “ + String.valueOf(minute);alarm = Hour + “:“ + Minute;time.setText(alarm);repaint();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 网络科技 > Java

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报