當前位置:首頁 » 編程軟體 » 編譯個鬧鍾

編譯個鬧鍾

發布時間: 2022-08-11 22:49:50

1. 用C語言怎麼編輯一個鬧鍾

呵呵,寫了一個玩玩:
#include <stdio.h>
#include <windows.h>
#include <winbase.h>

int main(int argc, char *argv[])
{
SYSTEMTIME time;
SYSTEMTIME clock;
GetLocalTime(&time);
printf("當前時間為:%2d:%2d:%2d\n",time.wHour,time.wMinute,time.wSecond);

printf("請設定鬧鍾時間:\n");
printf("時:");
scanf("%2d",&clock.wHour);
printf("分:");
scanf("%2d",&clock.wMinute);

while(1)
{
Sleep(1000);
GetLocalTime(&time);

if((time.wHour==clock.wHour)&&(time.wMinute==clock.wMinute))
{
printf("\a\a\a時間到!!!");
break;
}
}

return 0;
}

2. 編寫一個C語言鬧鍾程序

2樓說的創建一個線程查詢系統時間 ,就是一個查詢演算法,時間本來就是1~60秒排好序的,那就用2分法排序吧,快又簡單,然後當系統時間==鬧鍾事件【i】,就開始鬧
就這樣,2分演算法要是還不會就用冒泡法吧,嘿嘿

3. 1、 用C++編寫一個鬧鍾程序,能設置5組鬧鍾,定時時間到時揚聲器有音樂播放, 屏幕有文字作提示。

利用Visual C++設計一個鬧鍾程序,定時事件提醒等。本題目涉及使用設備上下文進行繪圖和定時器的應用。
功能要求:
1.表盤界面的顯示,顯示當前時間。
2.設置交互界面,用以設置時間信息和選擇提示事件。
3.用戶可以輸入特定時間和選擇播放聲音或閃動指示燈來提醒用戶。
4.具有秒錶功能。
設計提示:
1.應用程序框架設計。
2.程序菜單設計。
3.程序對話框設計。
4.表盤繪圖程序設計。
5.事件定時提醒功能設計。

#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int x=320,y=240,r=200,i,h,m,s,thetamin,thetasec;
struct time t;
char n[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"};
initgraph(&gd,&gm,"C:\Win-TC\bgi");//圖形驅動器路徑,根據自己的系統更換!
circle(x,y,210);
setcolor(4);
settextstyle(4,0,5);
for(i=0;i<12;i++)
{
if(i!=3)
outtextxy(x+(r-14)*cos(M_PI/6*i)-10,y-(r-14)*sin(M_PI/6*i)-26,n[i]);
else
outtextxy(x+(r-14)*cos(M_PI/6*i)-20,y-(r-14)*sin(M_PI/6*i)-26,n[i]);
}
gettime(&t);
printf("The current time is: %2d:%02d:%02d.%02d\n",t.ti_hour, t.ti_min,
t.ti_sec, t.ti_hund);
while(!kbhit())
{
setcolor(5);
setfillstyle(1,5);
circle(x,y,10);
floodfill(x,y,5);
gettime(&t);
if(t.ti_min!=m)
{
setcolor(0);
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180
)));
circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))
,10);
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h
-((m/2)*(M_PI/180))));
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(
(m/2)*(M_PI/180))),10);
}
if(t.ti_hour>12)
t.ti_hour=t.ti_hour-12;
if(t.ti_hour<4)
h=abs(t.ti_hour-3);
else
h=15-t.ti_hour;
m=t.ti_min;
if(t.ti_min<=15)
thetamin=(15-t.ti_min)*6;
else
thetamin=450-t.ti_min*6;
if(t.ti_sec<=15)
thetasec=(15-t.ti_sec)*6;
else
thetasec=450-t.ti_sec*6;
setcolor(4);
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h
-((m/2)*(M_PI/180))));
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(
(m/2)*(M_PI/180))),10);
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180
)));
circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))
,10);
setcolor(15);
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180
)));
delay(1000);
setcolor(0);
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180
)));
}
}
//我已通過Turbo C++3.0編譯,給分!

4. 用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(L0 + 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 <= 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 = 30 + r + L0;
y0 = 30 + r + T0;
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 (hh < 10)
st = "0" + hh;
else
st = "" + hh;
if (mm < 10)
st = st + ":0" + mm;
else
st = st + ":" + mm;
if(alarm.equals(st))
{
if(toolkit!=null)
toolkit.beep();
else {}
}
if (ss < 10)
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_X > 0) {
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 || newHou < 0) {
JOptionPane.showMessageDialog(this, "格式錯誤\n小時應該是不小於0不大於23的正數",
"Error", JOptionPane.ERROR_MESSAGE);
repaint();
break judge;
}
if (newMin >= 60 || newHou < 0) {
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 (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();
}
}
}

5. 急需要一個簡單的JAVA鬧鍾程序

寫了一個,也盡量在你可能不懂的地方添加註釋了。不過其實注釋越多不見得越好,因為有時你太在意注釋反而沒弄清整體的代碼結構。

importjava.awt.BorderLayout;
importjava.awt.FlowLayout;
importjava.awt.Font;
importjava.awt.Frame;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.io.IOException;
importjava.text.DateFormat;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Timer;
importjava.util.TimerTask;

importjavax.sound.sampled.AudioInputStream;
importjavax.sound.sampled.AudioSystem;
importjavax.sound.sampled.Clip;
importjavax.sound.sampled.LineEvent;
importjavax.sound.sampled.LineListener;
importjavax.sound.sampled.LineUnavailableException;
importjavax.sound.sampled.UnsupportedAudioFileException;
importjavax.swing.JButton;
importjavax.swing.JDialog;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
/**
*鬧鍾主界面
*/
{
privatestaticfinalintLOOP_COUNT=5;//重復播放的次數
privateJLabellabelClock,labelAlarm,labelNextAlarm;
privateJButtonbtnSet,btnClose;
privateSetDialogsetDialog;
privateJPaneltopPanel,alarmPanel;
privateTimertimer;
privateClipclip;
privateCalendaralarmCal;
privatebooleantimeReached=true;
privateDateFormatdf=newSimpleDateFormat("HH:mm");

publicAlarmClock(){
super("鬧鍾");
}

publicvoidlaunch(){
setSize(400,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

getContentPane().setLayout(newBorderLayout());//利用邊界布局將界面分割成上中下三部分
labelAlarm=newJLabel("鬧鍾定時已到!");
btnClose=newJButton("確定");
labelNextAlarm=newJLabel();//指示下一次鬧鍾時間
alarmPanel=newJPanel(newFlowLayout(FlowLayout.CENTER));//頂部提示欄提示鬧鍾時間已到,和確定按鈕
alarmPanel.add(labelAlarm);
alarmPanel.add(btnClose);

topPanel=newJPanel(newGridLayout(2,1));
topPanel.add(alarmPanel);
topPanel.add(labelNextAlarm);
alarmPanel.setVisible(false);//初始隱藏頂部提示欄

labelClock=newJLabel();
Fontfont=newFont(Font.SERIF,Font.PLAIN,48);//中間的倒計時文本用大號字體展示
labelClock.setFont(font);
labelClock.setHorizontalAlignment(JLabel.CENTER);//文本居中
btnSet=newJButton("設置");

getContentPane().add(topPanel,BorderLayout.NORTH);//界面頂部
getContentPane().add(labelClock,BorderLayout.CENTER);//界面中部
getContentPane().add(btnSet,BorderLayout.SOUTH);//界面底部

btnSet.addActionListener(this);//設置按鈕的點擊事件
btnClose.addActionListener(this);//頂部確定按鈕的點擊事件
setLocationRelativeTo(null);//界面居中
setDialog=newSetDialog(this);//初始化設置對話框

try{//初始化鬧鍾聲音
//目前發現wav格式的文件是可以支持的,mp3不支持
AudioInputStreamais=AudioSystem.getAudioInputStream(
AlarmClock.class.getResource("/res/alarm.wav"));
clip=AudioSystem.getClip();
clip.open(ais);
ais.close();
intloop=LOOP_COUNT<=0?1:LOOP_COUNT;
finallongtotalFrames=ais.getFrameLength()*loop;
clip.addLineListener(newLineListener(){
@Override
publicvoipdate(LineEvente){
//當鬧鍾音樂播放結束時,自動隱藏頂部提示欄
if(e.getFramePosition()>=totalFrames){
stopAlarm();
}
}
});
}catch(){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}catch(LineUnavailableExceptione){
e.printStackTrace();
}
initTimer();
}

publicstaticvoidmain(String[]args){
newAlarmClock().launch();//啟動主界面
}

@Override
publicvoidactionPerformed(ActionEvente){
Objectsource=e.getSource();
if(source==btnSet){//點擊設置按鈕時彈出設置界面,以模對話框顯示
setDialog.setVisible(true);
}elseif(source==btnClose){//點擊頂部確定按鈕時隱藏頂部提示欄
stopAlarm();
}
}

privatevoidinitTimer(){
timer=newTimer();//初始化倒計時任務
//開始倒計時
timer.scheleAtFixedRate(newTimerTask(){
@Override
publicvoidrun(){
Calendarcal=Calendar.getInstance();
inthour=cal.get(Calendar.HOUR_OF_DAY);
intmin=cal.get(Calendar.MINUTE);
intsec=cal.get(Calendar.SECOND);
//設置倒計時文本
labelClock.setText(String.format("%02d:%02d:%02d",hour,min,sec));
if(null!=alarmCal&&!timeReached){
intalarmHour=alarmCal.get(Calendar.HOUR_OF_DAY);
intalarmMin=alarmCal.get(Calendar.MINUTE);
if(alarmHour==hour&&alarmMin==min){//到時間時播放聲音
timeReached=true;
System.out.println("Timeover");
startAlarm();
}
}
}
},0,1000L);//每隔1秒刷新倒計時文本
}

/**
*開始計時
*@paramhour
*@paramminute
*/
publicvoidstartTimer(inthour,intminute){
alarmCal=Calendar.getInstance();
alarmCal.set(Calendar.HOUR_OF_DAY,hour);
alarmCal.set(Calendar.MINUTE,minute);
labelNextAlarm.setText("下次鬧鍾時間:"+df.format(alarmCal.getTime()));
timeReached=false;
}

/**
*取消倒計時任務
*/
publicvoidcancelTimer(){
labelNextAlarm.setText("");
alarmCal=null;
}

privatevoidstartAlarm(){//開始播放提示音
if(null!=clip){
alarmPanel.setVisible(true);//顯示頂部提示欄
clip.setFramePosition(0);//將音頻幀重置為第0幀
clip.loop(LOOP_COUNT);//開始循環播放
}
labelNextAlarm.setText("");
}
privatevoidstopAlarm(){//停止播放提示音
if(null!=clip&&clip.isRunning()){
clip.stop();//結束播放
}
labelNextAlarm.setText("");
alarmPanel.setVisible(false);//隱藏頂部提示欄
}

/**
*鬧鍾設置頁面
*/
,ActionListener{
privateJLabellabelHour,labelMin;
privateJTextFieldtextHour,textMin;
privateJPanelmainPanel,labelPanel,buttonPanel;
privateJButtonbtnOk,btnCancel,btnBack;
privateCalendarcal=Calendar.getInstance();

publicSetDialog(Frameframe){
super(frame);
setTitle("設置");
setModal(true);//設置為模窗口,就是說在本彈窗未消失時不允許點擊主界面。
setSize(300,150);
//顯示時分
labelHour=newJLabel("時");
labelMin=newJLabel("分");
labelHour.setHorizontalAlignment(JLabel.CENTER);
labelMin.setHorizontalAlignment(JLabel.CENTER);
textHour=newJTextField();
textMin=newJTextField();
//上面的部分用網格布局將各組件以2x2的格子放進去
labelPanel=newJPanel(newGridLayout(2,2));
labelPanel.add(labelHour);
labelPanel.add(labelMin);
labelPanel.add(textHour);
labelPanel.add(textMin);
//時分輸入框添加按鍵監聽,只允許輸入數字
textHour.addKeyListener(this);
textMin.addKeyListener(this);

//初始化按鈕
btnOk=newJButton("確定");
btnCancel=newJButton("取消");
btnBack=newJButton("返回");
//下面的按鈕依次居中放進去
buttonPanel=newJPanel(newFlowLayout(FlowLayout.CENTER));
buttonPanel.add(btnBack);
buttonPanel.add(btnCancel);
buttonPanel.add(btnOk);
//初始化主面板,將主面板分割為上下兩部分
mainPanel=newJPanel(newBorderLayout());
mainPanel.add(labelPanel,BorderLayout.CENTER);//上面顯示時分的組件
mainPanel.add(buttonPanel,BorderLayout.SOUTH);//下面排列三個按鈕
setContentPane(mainPanel);
//設置按鈕監聽
btnBack.addActionListener(this);
btnOk.addActionListener(this);
btnCancel.addActionListener(this);

cal.add(Calendar.HOUR,1);//默認設置為當前時間加1小時的整點時間
cal.set(Calendar.MINUTE,0);
inthour=cal.get(Calendar.HOUR_OF_DAY);
intmin=cal.get(Calendar.MINUTE);
textHour.setText(String.format("%02d",hour));
textMin.setText(String.format("%02d",min));
setLocationRelativeTo(frame);
}

@Override
publicvoidkeyPressed(KeyEventarg0){
}
@Override
publicvoidkeyReleased(KeyEventarg0){
}
@Override
publicvoidkeyTyped(KeyEvente){
intkeyChar=e.getKeyChar();
if(keyChar>=KeyEvent.VK_0&&keyChar<=KeyEvent.VK_9){
}else{//如果輸入的不是數字則屏蔽輸入
e.consume();//關鍵,屏蔽掉非法輸入
}
}

@Override
publicvoidactionPerformed(ActionEvente){
Objectsource=e.getSource();
if(source==btnOk){//如果點擊了確定按鈕,則開始計時
inthour=0,min=0;
try{
hour=Integer.parseInt(textHour.getText());
}catch(NumberFormatExceptione1){
}
try{
min=Integer.parseInt(textMin.getText());
}catch(NumberFormatExceptione1){
}
startTimer(hour,min);
setVisible(false);
}elseif(source==btnCancel){//點擊取消按鈕時取消計時
cancelTimer();
setVisible(false);
}elseif(source==btnBack){//點擊返回按鈕時什麼也不做,直接關閉設置界面
setVisible(false);
}
}
}
}

用到的音頻文件是wav格式的,你要注意mp3格式的是肯定不行的,其它格式行不行你可以自己試試。wav文件我傳到附件上吧,你如果覺得太大的話也可以自己弄一個放到指定目錄,記得重命名或修改代碼,然後再重新編譯運行。

6. C語言 鬧鍾程序

用C語言實現的鬧鍾程序
#include
#include
#include
#include
#include
void
main()
{
int
gd=DETECT,gm;
int
x=320,y=240,r=200,i,h,m,s,thetamin,thetasec;
struct
time
t;
char
n[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"};
initgraph(&gd,&gm,"g:\\tc\\bgi");//圖形驅動器路徑,根據自己的系統更換。
circle(x,y,210);
setcolor(4);
settextstyle(4,0,5);
for(i=0;i<12;i++)
{
if(i!=3)
outtextxy(x+(r-14)*cos(M_PI/6*i)-10,y-(r-14)*sin(M_PI/6*i)-26,n[i]);
else
outtextxy(x+(r-14)*cos(M_PI/6*i)-20,y-(r-14)*sin(M_PI/6*i)-26,n[i]);
}
gettime(&t);
printf("The
current
time
is:
%2d:%02d:%02d.%02d\n",t.ti_hour,
t.ti_min,
t.ti_sec,
t.ti_hund);
while(!kbhit())
{
setcolor(5);
setfillstyle(1,5);
circle(x,y,10);
floodfill(x,y,5);
gettime(&t);
if(t.ti_min!=m)
{
setcolor(0);
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180
)));
circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))
,10);
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h
-((m/2)*(M_PI/180))));
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(
(m/2)*(M_PI/180))),10);
}
if(t.ti_hour>12)
t.ti_hour=t.ti_hour-12;
if(t.ti_hour<4)
h=abs(t.ti_hour-3);
else&n
bsp;
h=15-t.ti_hour;
m=t.ti_min;
if(t.ti_min<=15)
thetamin=(15-t.ti_min)*6;
else
thetamin=450-t.ti_min*6;
if(t.ti_sec<=15)
thetasec=(15-t.ti_sec)*6;
else
thetasec=450-t.ti_sec*6;
setcolor(4);
line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h
-((m/2)*(M_PI/180))));
circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(
(m/2)*(M_PI/180))),10);
line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180
)));
circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))
,10);
setcolor(15);
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180
)));
delay(1000);
setcolor(0);
line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180
)));
}
}
該程序已在Turbo
C++3.0中通過編譯.

7. 想用delphi編一個鬧鍾程序

新建一個程序,並在窗體中加入以下元件:
Timer1: TTimer;
Image1: TImage;//時鍾的表盤
Edit1: TEdit; //日期框
Label1: TLabel;
PopupMenu1: TPopupMenu;
而後在 {$R *.DFM} 下面加入這么一句 {$R CLOCKSOU.RES} <--時鍾的聲音文件,
(註:CLOCKSOU.RES 是先用windows的記事本新建一個文件,並加入以下幾行
CLICKBEL WAVE "CLICKBEL.WAV"
TIMEBELL WAVE "TIMEBELL.WAV"
並存為文件 CLOCKSOU.RC 然後在 DOS解面下用BRCC32.exe 將 CLOCKSOU.RC 編譯
為 DELPHI 格式的資源文件 CLOCOSOU.RES,當然,您如果嫌步驟太煩,那嗎最簡單的,
到我的主頁 http://www.suye.8u8.com 下載完整的程序代碼)
OK,在完成上面的步驟後我們就可以在窗體的代碼筐中輸入以下代碼。

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls,unit2, StdCtrls,inifiles,MMSystem, Menus;
type
TForm1 = class(TForm)
Timer1: TTimer;
Image1: TImage;
Edit1: TEdit;
Label1: TLabel;
PopupMenu1: TPopupMenu;
pop1: TMenuItem;
pop2: TMenuItem;
pop4: TMenuItem;
pop3: TMenuItem;
pop5: TMenuItem;
N1: TMenuItem;
procere Timer1Timer(Sender: TObject);
procere timdrow(tim:real;long,col:integer);
procere FormCreate(Sender: TObject);
procere Image1Click(Sender: TObject);
procere pop1Click(Sender: TObject);
procere pop2Click(Sender: TObject);
procere pop4Click(Sender: TObject);
procere pop5Click(Sender: TObject);
procere pop3Click(Sender: TObject);
procere tiniwr();
procere FormClose(Sender: TObject; var Action: TCloseAction);
procere N1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
CLOCKBELL,wid,pofse:integer;
tim1,tim2,tim3,tim4:real;
dtme: TDateTime;
timini:tinifile;
dtim,textdef,tmfilename:string;
implementation

{$R *.DFM}
{$R LL1.RES} //時鍾的發聲文件
procere tform1.tiniwr();
begin
timini:=TIniFile.Create(tmfilename);
with timini do
begin
WriteString('內容','文字',textdef);
Writestring('參數','時間',dtim);
WriteBool('參數','報時允許',pop1.checked);
WriteBool('參數','秒響允許',pop2.checked);
WriteBool('參數','定時允許',pop3.checked);
end;
timini.free;
end;

procere tform1.timdrow(tim:real;long,col:integer); //指針的過程函數。
var
xx,yy:integer;
vtt,vt:real;
begin
IF (TIM2=59) AND (TIM3=0) THEN TIM:=TIM-1;
IF (COL=3)or (col=6) THEN
if col=3 then VT:=TIM1*5+(TIM2/720)
else begin
VT:=TIM1*5+((TIM2-1)/720);
col:=5;
end
ELSE vt:=tim;
vt:=Pi-pi*vt/30-2*pi*(vt-15); //得出指針末端的 Y 坐標
vtt:=int(long*sin(vt))+45; //得出指針末端的 X 坐標
xx:=strtoint(floattostr(vtt));
vtt:=int(long*cos(vt))+45;
yy:=strtoint(floattostr(vtt));
case col of //判斷所要畫的指針是 分針、秒針或時針
0:begin
image1.Canvas.pen.Color:=clgray;
image1.Canvas.pen.Width:=1;
END;
1:BEGIN
image1.Canvas.pen.Color:=clRed;
image1.Canvas.pen.Width:=1;
END;
2:BEGIN
image1.Canvas.pen.color:=clblue;
image1.Canvas.pen.Width:=2;
END;
3:BEGIN
image1.canvas.pen.color:=clgreen;
image1.Canvas.pen.Width:=2;
END;
4:begin
image1.canvas.pen.color:=clblack;
image1.Canvas.pen.Width:=1;
END;
5:begin
image1.canvas.pen.color:=clgray;
image1.Canvas.pen.Width:=3;
END;
end;
IMAGE1.Canvas.Ellipse(43,43,47,47);
image1.Canvas.MoveTo(45,45);
image1.Canvas.Lineto(xx,yy);
end;

procere TForm1.Timer1Timer(Sender: TObject);
var
str1,str2:string;
begin
str1:=timetostr(time());
STR2:=COPY(STR1,2,1);
if str2=':' then pofse:=0 else pofse:=1;
EDIT1.TEXT:=' '+DATETOSTR(DATE()); //日期盤上的日期數據
str2:=(str1,pofse+6,2);
tim4:=strtofloat(str2);
if tim4<>tim3 then //判斷是否到了移動指針的時間
begin
if (tim3=0) or (tim3=15) or (tim3=30)
or (tim3=45) then timdrow(tim3,39,4) else timdrow(tim3,39,0);
tim3:=tim4;
if pop2.checked then PlaySound(PChar('CLICKBEL'),
hInstance, snd_ASync or snd_Resource); //秒針的「滴答」發聲
str2:=(str1,1,pofse+1);

IF(TIM3=0)AND(TIM2=59)THEN //判斷是否到了發出整點報時「鍾聲」的時間
BEGIN
CLOCKBELL:=STRTOINT(STR2);
IF CLOCKBELL=0 THEN CLOCKBELL:=12;
END;
IF CLOCKBELL>12 THEN CLOCKBELL:=CLOCKBELL-12;
timdrow(tim1,26,6);
tim1:=strtofloat(str2);
timdrow(tim2,33,5);
str2:=(str1,pofse+3,2);
tim2:=strtofloat(str2);
timdrow(tim2,33,2);
IF (TIM2<>59) AND (TIM4<>0) THEN timdrow(tim1-1,26,3) ELSE TIMDROW(TIM1,26,3);
tim2:=strtofloat(str2);
str2:=(str1,pofse+3,2);
timdrow(tim3,39,1);
IF CLOCKBELL>=1 THEN
BEGIN
IF (CLOCKBELL<>0)and(pop2.checked) THEN
PlaySound(PChar('TIMEBELL'), hInstance,
snd_ASync or snd_Resource);//時鍾的整點「鍾聲」發聲
DEC(CLOCKBELL);
END;
if (pop3.checked)and(strtotime(dtim)<=time()) then//判斷是否到了定時的時間
begin
form2.Button2.Caption:='取消';
clockbell:=120;
form2.Show; //顯示給自己的提示。
end;
END;
end;

procere TForm1.FormCreate(Sender: TObject); //讀入程序開始的初始化設定
var
str1,str2:string;
begin
tmfilename:=ExtractFilePath(Application.EXEName)+'clock1.ini';
timini:=TIniFile.Create(tmfilename);
with timini do
begin
Textdef:=readString('內容','文字','在這里輸入您給自己的提醒');
dtim:=readstring('參數','時間','23:59:59');
pop1.checked:=readBool('參數','報時允許',true);
pop2.checked:=readBool('參數','秒響允許',true);
pop3.checked:=readBool('參數','定時允許',false);
form1.Left:=readinteger('位置','X坐標',566);
form1.top:=readinteger('位置','Y坐標',8);
end;
timini.free;
dtme:=time();
pop2.checked:=true;
pop1.Checked:=true;
str1:=timetostr(time());
STR2:=COPY(STR1,2,1);
If str2=':' then pofse:=0 else pofse:=1;
image1.Canvas.BRUSH.COLOR:=CLBLACK;;
str2:=(str1,pofse+6,2);
tim3:=strtofloat(str2);
str2:=(str1,pofse+3,2);
tim2:=strtofloat(str2);
str2:=(str1,1,pofse+1);
tim1:=strtofloat(str2);
CLOCKBELL:=-1;
end;

procere TForm1.Image1Click(Sender: TObject);//表盤滑鼠點擊時的的處理
begin
PopupMenu1.Popup(FORM1.LEFT+40,FORM1.TOP+150);
end;

procere TForm1.pop1Click(Sender: TObject);
begin
if pop1.checked then pop1.checked:=false else pop1.checked:=true;
form1.tiniwr();
end;

procere TForm1.pop2Click(Sender: TObject);
begin
if pop2.checked then pop2.checked:=false else pop2.checked:=true;
form1.tiniwr();
end;

procere TForm1.pop4Click(Sender: TObject);
begin
form2.show;
form1.tiniwr();
end;

procere TForm1.pop5Click(Sender: TObject);
begin
close;
end;

procere TForm1.pop3Click(Sender: TObject);
begin
if pop3.Checked then
begin
pop3.Checked:=false;
form2.Button2.Caption:='取消';
end
else
begin
pop3.Checked:=true;
form2.Button2.Caption:='關閉';
end;
form1.tiniwr();
end;

procere TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
timini:=TIniFile.Create('tmfilename');
with timini do
begin
Writeinteger('位置','X坐標',form1.Left);//保存時鍾窗口的初始 X 坐標
Writeinteger('位置','Y坐標',form1.top);//保存時鍾窗口的初始 Y 坐標
end;
timini.free;

end;

procere TForm1.N1Click(Sender: TObject);
begin
showmessage('模擬鬧鍾 0.1,'+#13+' Copy(R)ight by Suye.2001');
end;

end.

熱點內容
超級腳本製作 發布:2025-02-07 19:31:30 瀏覽:486
怎麼查看支付寶的賬號密碼 發布:2025-02-07 19:26:48 瀏覽:15
惠普伺服器查看ip指令 發布:2025-02-07 19:26:47 瀏覽:434
演算法設計模式 發布:2025-02-07 19:15:52 瀏覽:744
伺服器1u能連接幾台電腦 發布:2025-02-07 18:50:02 瀏覽:153
立人編譯 發布:2025-02-07 18:48:32 瀏覽:765
日產途達四驅的有哪些配置 發布:2025-02-07 18:42:02 瀏覽:832
伺服器搭建鏡像站 發布:2025-02-07 18:41:55 瀏覽:377
游戲上雲成標配雲伺服器該怎麼選 發布:2025-02-07 18:26:13 瀏覽:141
哪個安卓手機自帶系統沒有廣告 發布:2025-02-07 18:22:36 瀏覽:724