import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;import sun.applet.AppletAudioClip;import java.applet.*;
@SuppressWarnings("unused")
public class AlarmClock {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                AlarmFrame f = new AlarmFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
            }
        });
    }
}@SuppressWarnings("serial")
class AlarmFrame extends JFrame implements Runnable {
    Thread clock;
    TextField tfHour;
TextField tfMinute;
TextField tfSecond;
int hour,minute,second;
boolean turnOn;
    AppletAudioClip audioClip = null;
    public AlarmFrame() {
     MenuItem b=null;
     MenuItem c=null;
     MenuItem d=null;
        setTitle("AlarmClock");
        setSize(200, 100);
        setFont(new Font("Times New Roman", Font.BOLD, 40));
        start();
        Menu timeMenu = new Menu("闹铃设置");
        b=new MenuItem("闹铃时间");
        timeMenu.add(b); 
        b.addActionListener(listener1);
        Menu playMenu = new Menu("播放设置");
        c=new MenuItem(("循环播放"));
        playMenu.add(c);
        c.addActionListener(listener2);
        d=new MenuItem("停止播放");
        playMenu.add(d);
        d.addActionListener(listener2);
        MenuBar menuBar = new MenuBar();
        setMenuBar(menuBar);
        menuBar.add(timeMenu);
        menuBar.add(playMenu);
   }
public void start() {
        if (clock == null) {
            clock = new Thread(this);
            clock.start();
        }
}
public void run() {
        while (clock != null) {
            repaint();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }    public void stop() {
        clock = null;
    }public void paint(Graphics g) {
        Calendar now = new GregorianCalendar();
        String timeInfo = "";
         hour = now.get(Calendar.HOUR_OF_DAY);
         minute = now.get(Calendar.MINUTE);
        second = now.get(Calendar.SECOND);        if (hour <= 9)
            timeInfo += "0" + hour + ":";
        else
            timeInfo += hour + ":";
        if (minute <= 9)
            timeInfo += "0" + minute + ":";
        else
            timeInfo += minute + ":";
        if (second <= 9)
            timeInfo += "0" + second;
        else
            timeInfo += second;        g.setColor(Color.white);
        Dimension dim = getSize();
        g.fillRect(0, 0, dim.width, dim.height);
        g.setColor(Color.orange);
        g.drawString(timeInfo, 20, 80);    }
   ActionListener listener1=new ActionListener(){
     public void actionPerformed(ActionEvent e){
     if((e.getActionCommand()).equals("闹铃时间")){
     Button btSure;
     tfHour=new TextField(1);
     tfMinute=new TextField(1);
     tfSecond=new TextField(1);
     JFrame ff=new JFrame("闹铃时间");
     ff.setSize(250,100);
     ff.setVisible(true);
     final Panel p1=new Panel();
     final Panel p2=new Panel();
     p1.add(new Label("闹铃时间"));
     btSure=new Button("确定");
     btSure.addActionListener(new ActionListener(){
     public void actionPerformed(final ActionEvent event){
     turnOn=true;
     }
     });
     /*if (tfHour.getText()!=-1&&tfMinute.getText()!=-1&&tfSecond.getText()!=-1)
          if (hour==tfHour.getText()){
          if (minute==tfMinute.getText()){
          if (second==tfSecond.getText()){
          AudioClipsound=getAudioClip(getDocumentBase(),"alarm.wav");
          soud.play();
          }
          }
          }*/
     p2.add(btSure);
     p1.add(tfHour);
     p1.add(new Label(":"));
     p1.add(tfMinute);
     p1.add(new Label(":"));
     p1.add(tfSecond);
     ff.add(p1);
     ff.add(p2,BorderLayout.SOUTH);
     }
     }
    };
    ActionListener listener2=new ActionListener(){
        public void actionPerformed(ActionEvent ae){ 
            Button source = (Button)ae.getSource(); 
            if ("循环播放".equals(source.getLabel())){ 
             audioClip.loop(); 
            } 
            else if("停止播放".equals(source.getLabel())){ 
             audioClip.stop(); 
            } 
          }
       };
 }
这段代码有点乱,这样写的目的是想写一个小闹钟,当我按菜单里的“闹铃设置”子菜单里的“闹铃时间”时会弹出一个新窗口,这时可以输入闹铃时间,按确定,但不知我给“确定”的监听器能否发挥作用??第二是菜单里“播放设置”里的“循环播放”或“停止播放”时能执行该命令,但好像出错了。第三是我不知怎样写当闹铃时间和当前时间一样时就响音乐,注释掉的部分本来是此用意,但不对。 在下初学,请高手赐教,感激不尽……

解决方案 »

  1.   

    哦,有一个问题啊,
    为什么
    class AlarmFrame extends JFrame implements Runnable
    我觉得不应该让一个UI元素,实现Runnable接口。
    Runnable接口表示一个Task性质的东东。
    而Frame是现实层面的东西而且无论是Java还是C++,C#,都不会赞成把一个窗口至于Work Thread的。
      

  2.   

    本来我是想用注释掉的那部分代码来播放音乐的,可是行不通呢……本来就是说那个闹钟时间和当前时间一致时则播放音乐,我是这样想的,所以用了以上被我注释掉那部分代码,用了
    AudioClipsound=getAudioClip(getDocumentBase(),"alarm.wav");
                                    soud.play();
    来播放音乐,把那个alarm.wav和这程序放在同一文件夹。
      

  3.   

    你的这个播放音乐的方法我不知道怎么用  代码是改好了1. 实现了当闹钟时间和当前时间一致的时候打印一句话。
    2. 添加了 checkTimeFormat();方法 去判断输入   但这个方法没有被实现还有什么问题  你继续提吧  音乐我可以帮你搞一下  但是肯定不会用你的什么AudioClipsound类 
    因为我没用过哇  
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.util.List;import javax.swing.*;import sun.applet.AppletAudioClip;import java.applet.*;@SuppressWarnings("unused")
    public class AlarmClock {
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    AlarmFrame f = new AlarmFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    }
    });
    }
    }@SuppressWarnings("serial")
    class AlarmFrame extends JFrame implements Runnable { private JFrame ff = null; Thread clock;
    TextField tfHour;
    TextField tfMinute;
    TextField tfSecond;
    int hour, minute, second;
    public static boolean turnOn = false;
    public static int[] time = new int[3];
    AppletAudioClip audioClip = null; public AlarmFrame() {
    MenuItem b = null;
    MenuItem c = null;
    MenuItem d = null;
    setTitle("AlarmClock");
    setSize(200, 100);
    setFont(new Font("Times New Roman", Font.BOLD, 40));
    start();
    Menu timeMenu = new Menu("闹铃设置");
    b = new MenuItem("闹铃时间");
    timeMenu.add(b);
    b.addActionListener(listener1);
    Menu playMenu = new Menu("播放设置");
    c = new MenuItem(("循环播放"));
    playMenu.add(c);
    c.addActionListener(listener2);
    d = new MenuItem("停止播放");
    playMenu.add(d);
    d.addActionListener(listener2);
    MenuBar menuBar = new MenuBar();
    setMenuBar(menuBar);
    menuBar.add(timeMenu);
    menuBar.add(playMenu);
    } public void start() {
    if (clock == null) {
    clock = new Thread(this);
    clock.start();
    }
    } public void run() {
    while (clock != null) {
    repaint();
    try {
    Thread.sleep(1000);
    } catch (InterruptedException ex) {
    ex.printStackTrace();
    }
    }
    } public void stop() {
    clock = null;
    } public void paint(Graphics g) {
    Calendar now = new GregorianCalendar();
    String timeInfo = "";
    hour = now.get(Calendar.HOUR_OF_DAY);
    minute = now.get(Calendar.MINUTE);
    second = now.get(Calendar.SECOND); if (hour <= 9)
    timeInfo += "0" + hour + ":";
    else
    timeInfo += hour + ":";
    if (minute <= 9)
    timeInfo += "0" + minute + ":";
    else
    timeInfo += minute + ":";
    if (second <= 9)
    timeInfo += "0" + second;
    else
    timeInfo += second; g.setColor(Color.white);
    Dimension dim = getSize();
    g.fillRect(0, 0, dim.width, dim.height);
    g.setColor(Color.orange);
    g.drawString(timeInfo, 20, 80); } private boolean checkTimeFormat() {
    // do something to check the format
    return true;
    } ActionListener listener1 = new ActionListener() { public void actionPerformed(ActionEvent e) {
    if ((e.getActionCommand()).equals("闹铃时间")) {
    Button btSure;
    tfHour = new TextField(1);
    tfMinute = new TextField(1);
    tfSecond = new TextField(1);
    ff = new JFrame("闹铃时间");
    ff.setSize(250, 100);
    ff.setVisible(true);
    final Panel p1 = new Panel();
    final Panel p2 = new Panel();
    p1.add(new Label("闹铃时间"));
    btSure = new Button("确定");
    btSure.addActionListener(new ActionListener() {
    public void actionPerformed(final ActionEvent event) { // the first thing to do is to check the input format.
    // This feature is very important to implement.
    if (checkTimeFormat()) {
    turnOn = true;
    System.out.println("aaa");
    time[0] = (Integer.parseInt(tfHour.getText()));
    time[1] = (Integer.parseInt(tfMinute.getText()));
    time[2] = (Integer.parseInt(tfSecond.getText()));
    ff.setVisible(false);
    new Thread(new checkClock(time)).start();
    System.out.println("H: " + time[0] + "M: "
    + time[1] + "S: " + time[2]);
    }
    }
    });
    /*
     * if
     * (tfHour.getText()!=-1&&tfMinute.getText()!=-1&&tfSecond.getText
     * ()!=-1) if (hour==tfHour.getText()){ if
     * (minute==tfMinute.getText()){ if
     * (second==tfSecond.getText()){
     * AudioClipsound=getAudioClip(getDocumentBase(),"alarm.wav");
     * soud.play(); } } }
     */
    p2.add(btSure);
    p1.add(tfHour);
    p1.add(new Label(":"));
    p1.add(tfMinute);
    p1.add(new Label(":"));
    p1.add(tfSecond);
    ff.add(p1);
    ff.add(p2, BorderLayout.SOUTH); }
    }
    };
    ActionListener listener2 = new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    Button source = (Button) ae.getSource();
    if ("循环播放".equals(source.getLabel())) {
    audioClip.loop();
    } else if ("停止播放".equals(source.getLabel())) {
    audioClip.stop();
    }
    }
    };
    }class checkClock implements Runnable { private int[] curTime = new int[3];
    private int[] clockTime = new int[3]; private AppletAudioClip audioClip = null; public checkClock(int[] Clocktime) {
    this.clockTime = Clocktime;
    } @Override
    public void run() {
    while (true) {
    try {
    Thread.sleep(500);
    getCurTime();
    System.out.println("H: " + curTime[0] + "M: " + curTime[1]
    + "S: " + curTime[2]);
    if (curTime[0] == clockTime[0] && curTime[1] == clockTime[1]
    && curTime[2] == clockTime[2]) {
    System.out.println("=======Time's Up==Sound's Up======");
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    } public void getCurTime() {
    Calendar now = new GregorianCalendar();
    int hour = now.get(Calendar.HOUR_OF_DAY);
    int minute = now.get(Calendar.MINUTE);
    int second = now.get(Calendar.SECOND);
    curTime[0] = ((Integer) hour);
    curTime[1] = ((Integer) minute);
    curTime[2] = ((Integer) second);
    }
    }
      

  4.   

    声音也算是加好了 但是是很垃圾很垃圾的方法  
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.util.List;import javax.swing.*;import sun.applet.AppletAudioClip;import java.applet.*;@SuppressWarnings("unused")
    public class AlarmClock {
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    AlarmFrame f = new AlarmFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    }
    });
    }
    }@SuppressWarnings("serial")
    class AlarmFrame extends JFrame implements Runnable { private JFrame ff = null; Thread clock;
    TextField tfHour;
    TextField tfMinute;
    TextField tfSecond;
    int hour, minute, second;
    public static boolean turnOn = false;
    public static int[] time = new int[3];
    AppletAudioClip audioClip = null; public AlarmFrame() {
    MenuItem b = null;
    MenuItem c = null;
    MenuItem d = null;
    setTitle("AlarmClock");
    setSize(200, 100);
    setFont(new Font("Times New Roman", Font.BOLD, 40));
    start();
    Menu timeMenu = new Menu("闹铃设置");
    b = new MenuItem("闹铃时间");
    timeMenu.add(b);
    b.addActionListener(listener1);
    Menu playMenu = new Menu("播放设置");
    c = new MenuItem(("循环播放"));
    playMenu.add(c);
    c.addActionListener(listener2);
    d = new MenuItem("停止播放");
    playMenu.add(d);
    d.addActionListener(listener2);
    MenuBar menuBar = new MenuBar();
    setMenuBar(menuBar);
    menuBar.add(timeMenu);
    menuBar.add(playMenu);
    } public void start() {
    if (clock == null) {
    clock = new Thread(this);
    clock.start();
    }
    } public void run() {
    while (clock != null) {
    repaint();
    try {
    Thread.sleep(1000);
    } catch (InterruptedException ex) {
    ex.printStackTrace();
    }
    }
    } public void stop() {
    clock = null;
    } public void paint(Graphics g) {
    Calendar now = new GregorianCalendar();
    String timeInfo = "";
    hour = now.get(Calendar.HOUR_OF_DAY);
    minute = now.get(Calendar.MINUTE);
    second = now.get(Calendar.SECOND); if (hour <= 9)
    timeInfo += "0" + hour + ":";
    else
    timeInfo += hour + ":";
    if (minute <= 9)
    timeInfo += "0" + minute + ":";
    else
    timeInfo += minute + ":";
    if (second <= 9)
    timeInfo += "0" + second;
    else
    timeInfo += second; g.setColor(Color.white);
    Dimension dim = getSize();
    g.fillRect(0, 0, dim.width, dim.height);
    g.setColor(Color.orange);
    g.drawString(timeInfo, 20, 80); } private boolean checkTimeFormat() {
    // do something to check the format
    return true;
    } ActionListener listener1 = new ActionListener() { public void actionPerformed(ActionEvent e) {
    if ((e.getActionCommand()).equals("闹铃时间")) {
    Button btSure;
    tfHour = new TextField(1);
    tfMinute = new TextField(1);
    tfSecond = new TextField(1);
    ff = new JFrame("闹铃时间");
    ff.setSize(250, 100);
    ff.setVisible(true);
    final Panel p1 = new Panel();
    final Panel p2 = new Panel();
    p1.add(new Label("闹铃时间"));
    btSure = new Button("确定");
    btSure.addActionListener(new ActionListener() {
    public void actionPerformed(final ActionEvent event) { // the first thing to do is to check the input format.
    // This feature is very important to implement.
    if (checkTimeFormat()) {
    turnOn = true;
    System.out.println("aaa");
    time[0] = (Integer.parseInt(tfHour.getText()));
    time[1] = (Integer.parseInt(tfMinute.getText()));
    time[2] = (Integer.parseInt(tfSecond.getText()));
    ff.setVisible(false);
    new Thread(new checkClock(time)).start();
    System.out.println("H: " + time[0] + "M: "
    + time[1] + "S: " + time[2]);
    }
    }
    });
    /*
     * if
     * (tfHour.getText()!=-1&&tfMinute.getText()!=-1&&tfSecond.getText
     * ()!=-1) if (hour==tfHour.getText()){ if
     * (minute==tfMinute.getText()){ if
     * (second==tfSecond.getText()){
     * AudioClipsound=getAudioClip(getDocumentBase(),"alarm.wav");
     * soud.play(); } } }
     */
    p2.add(btSure);
    p1.add(tfHour);
    p1.add(new Label(":"));
    p1.add(tfMinute);
    p1.add(new Label(":"));
    p1.add(tfSecond);
    ff.add(p1);
    ff.add(p2, BorderLayout.SOUTH); }
    }
    };
    ActionListener listener2 = new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    Button source = (Button) ae.getSource();
    if ("循环播放".equals(source.getLabel())) {
    audioClip.loop();
    } else if ("停止播放".equals(source.getLabel())) {
    audioClip.stop();
    }
    }
    };
    }class checkClock implements Runnable { private int[] curTime = new int[3];
    private int[] clockTime = new int[3]; private AppletAudioClip audioClip = null; public checkClock(int[] Clocktime) {
    this.clockTime = Clocktime;
    } @Override
    public void run() {
    while (true) {
    try {
    Thread.sleep(500);
    getCurTime();
    System.out.println("H: " + curTime[0] + "M: " + curTime[1]
    + "S: " + curTime[2]);
    if (curTime[0] == clockTime[0] && curTime[1] == clockTime[1]
    && curTime[2] == clockTime[2]) {
    System.out.println("=======Time's Up==Sound's Up======");
    ToolKitBeep.displayBeep();
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    } public void getCurTime() {
    Calendar now = new GregorianCalendar();
    int hour = now.get(Calendar.HOUR_OF_DAY);
    int minute = now.get(Calendar.MINUTE);
    int second = now.get(Calendar.SECOND);
    curTime[0] = ((Integer) hour);
    curTime[1] = ((Integer) minute);
    curTime[2] = ((Integer) second);
    }
    }class ToolKitBeep {
    public static void displayBeep(){
    Toolkit toolkit = Toolkit.getDefaultToolkit();   
    toolkit.beep();
    }
    }
      

  5.   

    1.当前时间我在paint函数里已经有了,而且是毫秒跳一次,为什么你还要用getCurTime()函数呢?
    2.当我设置闹钟时间后,点确定,这个闹钟时间好像有用了哦???
    3.还有就是我给那个“循环播放”和"停止播放“注册监听器,出现的问题很大哦……
    4.音乐播放我其实也很不想用我写的那个,但由于初学,不知道怎么用,所以……
    再次感谢你的热情帮助……
      

  6.   

    1. 因为你的paint函数里面的时间是在main thread里面的 而我加上的getCurTime是在new thread里面的
       当然你有办法优化 那更好啊
    2. 点了确定之后  会开启一个线程 每隔500毫秒去监控一次是否时间到点
    3. 那2个东西你自己也可以做的  用我的ToolKitBeep类
    4. 加油
      

  7.   


    是类型转换的问题  MenuItem mi = (MenuItem) ae.getSource(); 这句ActionListener listener2 = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
             MenuItem mi = (MenuItem) ae.getSource();
                if ("循环播放".equals(mi.getLabel())) {
                    //audioClip.loop();   这里循环的代码你自己写吧
                } else if ("停止播放".equals(mi.getLabel())) {
                    //audioClip.stop();   这里停止播放的代码也你自己写吧
                }
            }
        };
      

  8.   

    还有一点点不明白,就是上面是用time[]数组来存入闹钟时间的,如time[0] = (Integer.parseInt(tfHour.getText()));这一句
    可是下面却用了clockTime[],如curTime[0] == clockTime[0]这一句
    这里有点看不懂,请帮帮忙……
      

  9.   


    time[]是闹钟设定的时间也就是clockTime
    curTime是当前时间  每隔500毫秒重设一次
      

  10.   

    我知道time[]是闹钟设定的时间也就是clockTime 
    可我不知道为什么不直接用curTime[0] == time[0]
    而要用clokTime呢,在这程序里,它怎知道time[]就是clockTime呢??
    见笑了,谢谢……
      

  11.   

    import sun.applet.AppletAudioClip;
    谁能给个这个包
      

  12.   

    我知道curTime[0] == time[0] 可以,可就是不知在这程序它怎么知道time[]就是clockTime呢
      

  13.   


    因为这个方法    @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(500);
                    getCurTime();
                    System.out.println("H: " + curTime[0] + "M: " + curTime[1]
                            + "S: " + curTime[2]);
                    if (curTime[0] == clockTime[0] && curTime[1] == clockTime[1]
                            && curTime[2] == clockTime[2]) {
                        System.out.println("=======Time's Up==Sound's Up======");
                        ToolKitBeep.displayBeep();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
      

  14.   

    public checkClock(int[] Clocktime) {
            this.clockTime = Clocktime;
        }请问这几句的作用是什么呢??不是很明白