欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java鬧鐘課程設計代碼,java鬧鐘課程設計代碼是什么

java 使用swing制作一個小鬧鐘,包含以下功能:

import java.awt.BorderLayout;

創(chuàng)新互聯(lián)建站長期為成百上千客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為東鄉(xiāng)企業(yè)提供專業(yè)的網(wǎng)站設計制作、網(wǎng)站制作,東鄉(xiāng)網(wǎng)站改版等技術(shù)服務。擁有十多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.io.IOException;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Timer;

import java.util.TimerTask;

import javax.sound.sampled.AudioInputStream;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.Clip;

import javax.sound.sampled.LineEvent;

import javax.sound.sampled.LineListener;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.UnsupportedAudioFileException;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

/**

* 鬧鐘主界面

*/

public class AlarmClock extends JFrame implements ActionListener {

private static final int LOOP_COUNT = 5; // 重復播放的次數(shù)

private JLabel labelClock, labelAlarm, labelNextAlarm;

private JButton btnSet, btnClose;

private SetDialog setDialog;

private JPanel topPanel, alarmPanel;

private Timer timer;

private Clip clip;

private Calendar alarmCal;

private boolean timeReached = true;

private DateFormat df = new SimpleDateFormat("HH : mm");

public AlarmClock() {

super("鬧鐘");

}

public void launch() {

setSize(400, 300);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

getContentPane().setLayout(new BorderLayout()); // 利用邊界布局將界面分割成上中下三部分

labelAlarm = new JLabel("鬧鐘定時已到!");

btnClose = new JButton("確定");

labelNextAlarm = new JLabel(); // 指示下一次鬧鐘時間

alarmPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); // 頂部提示欄提示鬧鐘時間已到,和確定按鈕

alarmPanel.add(labelAlarm);

alarmPanel.add(btnClose);

topPanel = new JPanel(new GridLayout(2, 1));

topPanel.add(alarmPanel);

topPanel.add(labelNextAlarm);

alarmPanel.setVisible(false); // 初始隱藏頂部提示欄

labelClock = new JLabel();

Font font = new Font(Font.SERIF, Font.PLAIN, 48); // 中間的倒計時文本用大號字體展示

labelClock.setFont(font);

labelClock.setHorizontalAlignment(JLabel.CENTER); // 文本居中

btnSet = new JButton("設置");

getContentPane().add(topPanel, BorderLayout.NORTH); // 界面頂部

getContentPane().add(labelClock, BorderLayout.CENTER); // 界面中部

getContentPane().add(btnSet, BorderLayout.SOUTH); // 界面底部

btnSet.addActionListener(this); // 設置按鈕的點擊事件

btnClose.addActionListener(this); // 頂部確定按鈕的點擊事件

setLocationRelativeTo(null); // 界面居中

setDialog = new SetDialog(this); // 初始化設置對話框

try { // 初始化鬧鐘聲音

// 目前發(fā)現(xiàn)wav格式的文件是可以支持的,mp3不支持

AudioInputStream ais = AudioSystem.getAudioInputStream(

AlarmClock.class.getResource("/res/alarm.wav"));

clip = AudioSystem.getClip();

clip.open(ais);

ais.close();

int loop = LOOP_COUNT = 0 ? 1 : LOOP_COUNT;

final long totalFrames = ais.getFrameLength() * loop;

clip.addLineListener(new LineListener() {

@Override

public void update(LineEvent e) {

// 當鬧鐘音樂播放結(jié)束時,自動隱藏頂部提示欄

if(e.getFramePosition() = totalFrames) {

stopAlarm();

}

}

});

} catch (UnsupportedAudioFileException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (LineUnavailableException e) {

e.printStackTrace();

}

initTimer();

}

public static void main(String[] args) {

new AlarmClock().launch(); // 啟動主界面

}

@Override

public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

if(source == btnSet) { // 點擊設置按鈕時彈出設置界面,以模對話框顯示

setDialog.setVisible(true);

} else if(source == btnClose) { // 點擊頂部確定按鈕時隱藏頂部提示欄

stopAlarm();

}

}

private void initTimer() {

timer = new Timer(); // 初始化倒計時任務

// 開始倒計時

timer.scheduleAtFixedRate(new TimerTask() {

@Override

public void run() {

Calendar cal = Calendar.getInstance();

int hour = cal.get(Calendar.HOUR_OF_DAY);

int min = cal.get(Calendar.MINUTE);

int sec = cal.get(Calendar.SECOND);

// 設置倒計時文本

labelClock.setText(String.format("%02d : %02d : %02d", hour, min, sec));

if(null != alarmCal !timeReached) {

int alarmHour = alarmCal.get(Calendar.HOUR_OF_DAY);

int alarmMin = alarmCal.get(Calendar.MINUTE);

if(alarmHour == hour alarmMin == min) { // 到時間時播放聲音

timeReached = true;

System.out.println("Time over");

startAlarm();

}

}

}

}, 0, 1000L); // 每隔1秒刷新倒計時文本

}

/**

* 開始計時

* @param hour

* @param minute

*/

public void startTimer(int hour, int minute) {

alarmCal = Calendar.getInstance();

alarmCal.set(Calendar.HOUR_OF_DAY, hour);

alarmCal.set(Calendar.MINUTE, minute);

labelNextAlarm.setText("下次鬧鐘時間:" + df.format(alarmCal.getTime()));

timeReached = false;

}

/**

* 取消倒計時任務

*/

public void cancelTimer() {

labelNextAlarm.setText("");

alarmCal = null;

}

private void startAlarm() { // 開始播放提示音

if(null != clip) {

alarmPanel.setVisible(true); // 顯示頂部提示欄

clip.setFramePosition(0); // 將音頻幀重置為第0幀

clip.loop(LOOP_COUNT); // 開始循環(huán)播放

}

labelNextAlarm.setText("");

}

private void stopAlarm() { // 停止播放提示音

if(null != clip clip.isRunning()) {

clip.stop(); // 結(jié)束播放

}

labelNextAlarm.setText("");

alarmPanel.setVisible(false); // 隱藏頂部提示欄

}

/**

* 鬧鐘設置頁面

*/

class SetDialog extends JDialog implements KeyListener, ActionListener {

private JLabel labelHour, labelMin;

private JTextField textHour, textMin;

private JPanel mainPanel, labelPanel, buttonPanel;

private JButton btnOk, btnCancel, btnBack;

private Calendar cal = Calendar.getInstance();

public SetDialog(Frame frame) {

super(frame);

setTitle("設置");

setModal(true); // 設置為模窗口,就是說在本彈窗未消失時不允許點擊主界面。

setSize(300, 150);

// 顯示時分

labelHour = new JLabel("時");

labelMin = new JLabel("分");

labelHour.setHorizontalAlignment(JLabel.CENTER);

labelMin.setHorizontalAlignment(JLabel.CENTER);

textHour = new JTextField();

textMin = new JTextField();

// 上面的部分用網(wǎng)格布局將各組件以2x2的格子放進去

labelPanel = new JPanel(new GridLayout(2, 2));

labelPanel.add(labelHour);

labelPanel.add(labelMin);

labelPanel.add(textHour);

labelPanel.add(textMin);

// 時分輸入框添加按鍵監(jiān)聽,只允許輸入數(shù)字

textHour.addKeyListener(this);

textMin.addKeyListener(this);

// 初始化按鈕

btnOk = new JButton("確定");

btnCancel = new JButton("取消");

btnBack = new JButton("返回");

// 下面的按鈕依次居中放進去

buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

buttonPanel.add(btnBack);

buttonPanel.add(btnCancel);

buttonPanel.add(btnOk);

// 初始化主面板,將主面板分割為上下兩部分

mainPanel = new JPanel(new BorderLayout());

mainPanel.add(labelPanel, BorderLayout.CENTER); // 上面顯示時分的組件

mainPanel.add(buttonPanel, BorderLayout.SOUTH); // 下面排列三個按鈕

setContentPane(mainPanel);

// 設置按鈕監(jiān)聽

btnBack.addActionListener(this);

btnOk.addActionListener(this);

btnCancel.addActionListener(this);

cal.add(Calendar.HOUR, 1); // 默認設置為當前時間加1小時的整點時間

cal.set(Calendar.MINUTE, 0);

int hour = cal.get(Calendar.HOUR_OF_DAY);

int min = cal.get(Calendar.MINUTE);

textHour.setText(String.format("%02d", hour));

textMin.setText(String.format("%02d", min));

setLocationRelativeTo(frame);

}

@Override

public void keyPressed(KeyEvent arg0) {

}

@Override

public void keyReleased(KeyEvent arg0) {

}

@Override

public void keyTyped(KeyEvent e) {

int keyChar = e.getKeyChar();

if(keyChar = KeyEvent.VK_0 keyChar = KeyEvent.VK_9){

}else{ // 如果輸入的不是數(shù)字則屏蔽輸入

e.consume(); //關鍵,屏蔽掉非法輸入

}

}

@Override

public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

if(source == btnOk) { // 如果點擊了確定按鈕,則開始計時

int hour = 0, min = 0;

try {

hour = Integer.parseInt(textHour.getText());

} catch (NumberFormatException e1) {

}

try {

min = Integer.parseInt(textMin.getText());

} catch (NumberFormatException e1) {

}

startTimer(hour, min);

setVisible(false);

} else if(source == btnCancel) { // 點擊取消按鈕時取消計時

cancelTimer();

setVisible(false);

} else if(source == btnBack) { // 點擊返回按鈕時什么也不做,直接關閉設置界面

setVisible(false);

}

}

}

}

java鬧鐘程序代碼

搞不懂為什么要匿名提問,真的那么怕笑話,不至于吧!

import java.util.Timer;

import java.util.Date;

import java.text.SimpleDateFormat;

public class Alarm

{

public static void main(String[] args)

{

Timer timer = new Timer();

try{

timer.schedule(new task(),1000,1000);

}

catch(IllegalStateException e)

{

System.out.println("時間到");

}

}

static class task extends java.util.TimerTask

{

public void run()

{

Date now = new Date();

SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");

if(time.format(now).toString.equals("23:27:00"))

this.cancel();

else

System.out.println(time.format(now));

}

}

}

用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

* 方法說明:實現(xiàn)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("系統(tǒng)時間背靜顏色");

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

// 窗體添加事件監(jiān)聽,監(jiān)聽秒表的觸發(fā)

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;// 內(nèi)盤顏色

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("系統(tǒng)時間:" + 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

}

// 鬧鈴時間的判斷及實現(xiàn)

// 鬧鈴聲音的實現(xiàn)

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位數(shù)字", "Error",

JOptionPane.ERROR_MESSAGE);

repaint();

break judge;

}

// 判斷輸入的是不是數(shù)字

for (int i = 0; i (getTime.length()); i++) {

c = getTime.charAt(i);

if (i == 2 !Character.isDigit(c))

continue;

// 判斷當前字符,如果不是數(shù)字則跳出該事件

if (i != 2 !Character.isDigit(c)) {

JOptionPane.showMessageDialog(this, "格式錯誤\n請按格式輸入5位數(shù)字",

"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的正數(shù)",

"Error", JOptionPane.ERROR_MESSAGE);

repaint();

break judge;

}

if (newMin = 60 || newHou 0) {

JOptionPane.showMessageDialog(this, "格式錯誤\n分鐘應該是小于60的正數(shù)", "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, "請輸入喜歡的內(nèi)盤顏色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "請輸入喜歡的內(nèi)盤顏色(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) {

// 當時間參數(shù)小于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();

}

}

}

尋找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

* 方法說明:實現(xiàn)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("系統(tǒng)時間背靜顏色");

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

// 窗體添加事件監(jiān)聽,監(jiān)聽秒表的觸發(fā)

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;// 內(nèi)盤顏色

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("系統(tǒng)時間:" + 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

}

// 鬧鈴時間的判斷及實現(xiàn)

// 鬧鈴聲音的實現(xiàn)

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位數(shù)字", "Error",

JOptionPane.ERROR_MESSAGE);

repaint();

break judge;

}

// 判斷輸入的是不是數(shù)字

for (int i = 0; i (getTime.length()); i++) {

c = getTime.charAt(i);

if (i == 2 !Character.isDigit(c))

continue;

// 判斷當前字符,如果不是數(shù)字則跳出該事件

if (i != 2 !Character.isDigit(c)) {

JOptionPane.showMessageDialog(this, "格式錯誤\n請按格式輸入5位數(shù)字",

"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的正數(shù)",

"Error", JOptionPane.ERROR_MESSAGE);

repaint();

break judge;

}

if (newMin = 60 || newHou 0) {

JOptionPane.showMessageDialog(this, "格式錯誤\n分鐘應該是小于60的正數(shù)", "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, "請輸入喜歡的內(nèi)盤顏色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "請輸入喜歡的內(nèi)盤顏色(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) {

// 當時間參數(shù)小于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();

}

}

}

請用JAVA編一個鬧鐘程序,符合要求追加100分!急!!

import java.util.*;

import java.awt.*;

import java.applet.*;

import java.text.*;

public class AlarmClock extends Applet implements Runnable

{

Thread timer=null; //創(chuàng)建線程timer

Image clockp,gif1,gif2,clock6,clock7; //clockp:鬧鐘的外殼,鬧鈴和報時鳥

int s,m,h,hh;

AudioClip ipAu,danger,chirp;

boolean canPaint=true;

boolean flag=false;

boolean strike=true;

int counter=0;

int lasts;

Image offscreen_buf=null;

int i,j,t=0;

int timeout=166;

int lastxs=0,lastys=0,lastxm=0,lastym=0,lastxh=0,lastyh=0;

Date dummy=new Date(); //生成Data對象

GregorianCalendar cal=new GregorianCalendar();

SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//設置時間格式

String lastdate=df.format(dummy);

Font F=new Font("TimesRoman",Font.PLAIN,14);//設置字體格式

Date dat=null;

Date timeNow=null;

Color fgcol=Color.blue;

Color fgcol2=Color.darkGray;

Panel setpanel;

Color backcolor=Color.pink;

TextField showhour,showmin,showsec,sethour,setmin,setsec;//顯示當前時間文本框和定時文本框

Button onbutton;

Button offbutton;

Label hlabel1,mlabel1,slabel1,hlabel2,mlabel2,slabel2;//顯示時間單位時所用的標簽(時、分、秒)

Label info1=new Label("歡迎使用定時提醒鬧鐘"),info2=new Label("");

Label note1=new Label("當前時間:"),note2=new Label("鬧鐘設置:");

boolean setalerm=false,clickflag=false;//判斷是否響鈴和振動

int fixh=0,fixm=0,fixs=0;//記錄鬧鐘的定時

public void init()//初始化方法

{

Integer gif_number;

int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//顯示時間和定時文本框的定位參數(shù)

setLayout(null); //將布局管理器初始化為null

setpanel=new Panel();

setpanel.setLayout(null);

setpanel.add(note1);

setpanel.add(note2);

note1.setBounds(30,100,60,20);

note1.setBackground(backcolor);

note1.setForeground(Color.black);

note2.setBounds(30,180,60,20);

note2.setBackground(backcolor);

note2.setForeground(Color.black);

hlabel1=new Label();

mlabel1=new Label();

slabel1=new Label();

hlabel2=new Label();

mlabel2=new Label();

slabel2=new Label();

//顯示當前時間用的文本框

showhour=new TextField("00",5);

showmin=new TextField("00",5);

showsec=new TextField("00",5);

//定時用的文本框(時、分、秒)

sethour=new TextField("00",5);

setmin=new TextField("00",5);

setsec=new TextField("00",5);

//當前時間用的文本框的位置、大小

setpanel.add(showhour);

showhour.setBounds(fieldx,fieldy1,fieldw,fieldh);

showhour.setBackground(Color.white);

//在文本框后加入單位“時”

setpanel.add(hlabel1);

hlabel1.setText("時");

hlabel1.setBackground(backcolor);

hlabel1.setForeground(Color.black);

hlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

//當前時間的分鐘文本框的位置、大小

setpanel.add(showmin);

showmin.setBounds(fieldx,fieldy1,fieldw,fieldh);

showmin.setBackground(Color.white);

//在文本框后加入單位“分”

setpanel.add(mlabel1);

mlabel1.setText("分");

mlabel1.setBackground(backcolor);

mlabel1.setForeground(Color.black);

mlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

//當前時間的秒文本框的位置、大小

setpanel.add(showsec);

showsec.setBounds(fieldx,fieldy1,fieldw,fieldh);

showsec.setBackground(Color.white);

//在文本框后加入單位“秒”

setpanel.add(slabel1);

slabel1.setText("秒");

slabel1.setBackground(backcolor);

slabel1.setForeground(Color.black);

slabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=50;

//定時的小時文本框的位置、大小

setpanel.add(sethour);

sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);

sethour.setBackground(Color.white);

//在文本框后加入單位“時”

setpanel.add(hlabel2);

hlabel2.setText("時");

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("分");

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("秒");

slabel2.setBackground(backcolor);

slabel2.setForeground(Color.black);

slabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

//設置鬧鐘控制按鈕(on,off)

onbutton=new Button("開");

offbutton=new Button("關");

setpanel.add(onbutton);

setpanel.add(offbutton);

onbutton.setBounds(90,180,40,20);

offbutton.setBounds(140,180,40,20);

//加入一些附加的信息標簽(題頭,題尾)

setpanel.add(info1);

info1.setBackground(backcolor);

info1.setForeground(Color.blue);

info1.setBounds(50,50,150,20);

setpanel.add(info2);

info2.setBackground(backcolor);

info2.setForeground(Color.blue);

info2.setBounds(150,280,100,20);

//將面板加入當前容器中,并設置面板的大小和背景色

add(setpanel);

setpanel.setBounds(300,1,250,420);

setpanel.setBackground(backcolor);

//獲取聲音文件

ipAu=getAudioClip(getDocumentBase(),"bells/仙劍.mid");

danger=getAudioClip(getDocumentBase(),"bells/0.mid");

chirp=getAudioClip(getDocumentBase(),"bells/3.mid");

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);//創(chuàng)建Tracke對象

clockp=getImage(getDocumentBase(),"休閑.png");

gif1=getImage(getDocumentBase(),"gif1.gif");

gif2=getImage(getDocumentBase(),"gif2.gif");

clock6=getImage(getDocumentBase(),"clock6.gif");

clock7=getImage(getDocumentBase(),"clock7.gif");

mt.addImage(clockp,i++);

mt.addImage(gif1,i++);

mt.addImage(gif2,i++);

mt.addImage(clock6,i++);

mt.addImage(clock7,i++);

try{mt.waitForAll();}catch(InterruptedException e){};//等待加載結(jié)束

resize(600,420);//設置窗口大小

}

public void paint(Graphics g){//重寫paint()方法

int xh,yh,xm,ym,xs,ys,strike_times;

int xcenter,ycenter;

String today;

Integer gif_number;

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)*18+xcenter);

yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

//設置字體和顏色

g.setFont(F);

g.setColor(fgcol);

g.setColor(fgcol2);

g.setColor(getBackground());

g.fillRect(1,1,634,419);

g.drawImage(clockp,75,110,this);

g.drawImage(clock6,83,280,this);

g.setColor(fgcol2);

g.setColor(getBackground());

g.setColor(fgcol2);

//以數(shù)字方式顯示年、月、日和時間

g.drawString(today,55,415);

g.drawLine(xcenter,ycenter,xs,ys);

g.setColor(fgcol);

//畫指針

g.drawLine(xcenter,ycenter-1,xm,ym);

g.drawLine(xcenter-1,ycenter,xm,ym);

g.drawLine(xcenter,ycenter-1,xh,yh);

g.drawLine(xcenter-1,ycenter,xh,yh);

lastxs=xs;lastys=ys;

lastxm=xh;lastym=ym;

lastxh=xh;lastyh=yh;

lastdate=today;

if(h12)hh=h;//將系統(tǒng)時間變換到0-11區(qū)間

else hh=h-12;

if(hh==0) strike_times=12;//計算整點時鐘聲數(shù)

else strike_times=hh;

if((s==0m==0)||flag){//判斷是否整點,是否是主動刷新

if(counterstrike_times){

flag=true;

g.drawImage(gif2,115,35,this);

if(lasts!=s){

if(strike){

counter++;

danger.play();//播放鬧鈴聲

}

if(strike)strike=false;

else strike=true;

}

}

else {

counter=0;

flag=false;

}

}

else

g.drawImage(gif1,115,35,this);

int timedelta;//記錄當前時間與鬧鈴定時的時差

Integer currh,currm,currs;//分別記錄當前的時、分、秒

timeNow=new Date();

currh=new Integer(timeNow.getHours());

currm=new Integer(timeNow.getMinutes());

currs=new Integer(timeNow.getSeconds());

//判斷是否要更新當前顯示的時間,這樣可以避免文本框出現(xiàn)頻率閃動

if(currh.intValue()!=Integer.valueOf(showhour.getText()).intValue())

showhour.setText(currh.toString());

if(currm.intValue()!=Integer.valueOf(showmin.getText()).intValue())

showmin.setText(currh.toString());

if(currs.intValue()!=Integer.valueOf(showsec.getText()).intValue())

showsec.setText(currh.toString());

if(setalerm){ //判斷是否設置了鬧鐘

//判斷當前時間是否為鬧鐘所定的時間

if((currh.intValue()==fixh)(currm.intValue()==fixm)(currs.intValue()==fixs))

clickflag=true;

timedelta=currm.intValue()*60+currs.intValue()-fixm*60-fixs;

if((timedelta60)(clickflag==true)){ //若當前時間與鬧鐘相差時間達到60秒

chirp.play();

g.drawImage(clock7,83,280,this);

}

else{

chirp.stop();

clickflag=false;

}

}

if(lasts!=s)

ipAu.play();//播放滴答聲

lasts=s;

if(canPaint){

t+=1;

if(t==12)t=0;

}

canPaint=false;

dat=null;

}

public void start(){

if(timer==null){

timer=new Thread(this);//將timer實例化

timer.start();

}

}

public void stop(){

timer=null;

}

public void run(){

while(timer!=null){

try{timer.sleep(timeout);}catch(InterruptedException e){}

canPaint=true;

repaint();//刷新畫面

}

timer=null;

}

public void update(Graphics g){ //采用雙緩沖技術(shù)的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);

}

public boolean action(Event evt,Object arg){ //按鈕事件處理函數(shù)

if(evt.target instanceof Button){

String lable=(String)arg;

if(lable.equals("開")){

setalerm=true;

//獲取輸入的時間

fixh=Integer.valueOf(sethour.getText()).intValue();

fixm=Integer.valueOf(setmin.getText()).intValue();

fixs=Integer.valueOf(setsec.getText()).intValue();

clickflag=false;

}

if(lable.equals("關")){

setalerm=false;

if(chirp!=null)

chirp.stop();

clickflag=false;

}

return true;

}

return false;

}

}

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; //創(chuàng)建線程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提供的是一個日歷式的東東,上面又多了很多的參數(shù),是方便操作了不少。而Date類的功能遠不及其,求個和日期有聯(lián)系的還要自己計算。

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程序中,首先自動調(diào)用初始化完成必要的初始化工作,緊接著自動調(diào)用start,在進入執(zhí)行程序和返回到該頁面時被調(diào)用,而從該頁面轉(zhuǎn)到別的頁面時,stop被調(diào)用,關閉瀏覽器時,執(zhí)行destroy。

public void init()//初始化方法

{

int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//顯示時間和定時文本框的定位參數(shù)

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);//為給定組件創(chuàng)建一個跟蹤媒體的MediaTracker對象,把圖片添加到被跟蹤的圖片組

//Java允?Sapplet??HTML所在的位置(decument base)下?d?Y料,也允?Sapplet?鈉涑淌醬a所在的位置(code base)下?d?Y料。藉由呼叫g(shù)etDocumentBase()?cgotCodeBase()可得到URL物件。?@些函?????湍閼業(yè)僥閬胂螺d的?n案的位置

//clockp=getImage(getDocumentBase(),"11.png");

gif1=getImage(getCodeBase(),"2.gif");

//i為id號

mt.addImage(gif1,i++);

try

{

mt.waitForAll();

}

catch(InterruptedException e)

{};//等待加載結(jié)束

resize(600,420);//設置窗口大小

}

//窗口顯示有改變的時候調(diào)用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);

//以數(shù)字方式顯示年、月、日和時間

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

分享文章:java鬧鐘課程設計代碼,java鬧鐘課程設計代碼是什么
當前網(wǎng)址:http://chinadenli.net/article49/dsgsceh.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序云服務器微信公眾號全網(wǎng)營銷推廣App設計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)