0

嗨,我正在编写一个警报应用程序。我有一个贪睡课程,当警报响起时会被调用。在使用 JMF 之前它可以正常工作。但是在使用 JMF 之后,我只看到我的贪睡 UI 的外框。

我尝试为该课程启动一个新线程,但得到了相同的结果。我在这里粘贴我认为问题所在的类的代码。

请帮我解决问题。

* IsTime 类。它检查是否到了发出警报的时间。*

 package alarm;  
 import java.util.Calendar;  
 import java.util.GregorianCalendar;  

 public class IsTime {  
int hrs;  
int min;  
int sec;  
GregorianCalendar clk=new GregorianCalendar();  
Calendar gtl= Calendar.getInstance();  
    mp3 mix=new mp3();  
public void makeReady(int h,int m,int s,String ampm){  
    Calendar c1=Calendar.getInstance();  
    c1.set(Calendar.HOUR_OF_DAY,h);  
    c1.set(Calendar.MINUTE, m);  
    c1.set(Calendar.SECOND,s);  
    if("PM".equals(ampm)){  
        c1.set(Calendar.AM_PM, Calendar.PM);  
        if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.PM){  
            System.out.println("now is pm");  
            if(c1.after(Calendar.getInstance())){  
                check(c1);  
            }  
            c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1);  
            check(c1);  
        }  
    }  
    if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.AM){  
            System.out.println("now is am");  
            if(c1.after(Calendar.getInstance())){  
                check(c1);  
            }  
            c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1);  
            check(c1);  
        }  
    if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.PM){  
        c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1);  
        check(c1);  
    }  
}  
public void check(Calendar ch){  
        System.out.println("got to check");  
        System.out.println(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));  
        System.out.println(ch.get(Calendar.HOUR_OF_DAY));  
        while(!(Calendar.getInstance().after(ch))){  
        }  
    Snooze snz=new Snooze();  
            snz.start();  
            mix.start();  
    }  
public void makeReady(int mis){  
        Calendar sz=Calendar.getInstance();  
        sz.set(Calendar.SECOND, 0);  
        sz.set(Calendar.MINUTE, Calendar.getInstance().get(Calendar.MINUTE)+mis);  
        check(sz);  
    }  
    }  

*播放歌曲的媒体类 *

     package alarm;  
import java.awt.Button;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import javax.media.*;  
import java.io.*;  
import java.net.URL;  
import javax.swing.JFileChooser;  
import javax.swing.JFrame;  

class mp3 extends Thread  
{  
private URL url;  
private MediaLocator mediaLocator;  
private Player playMP3;  
static String mp3;  
static String mp4;  
public mp3()  
{  
try{  
    System.out.println(System.getProperty("os.name"));  
    System.out.println("before"+mp3);  
    if(System.getProperty("os.name").equals("Linux")){  
        mp4="file://".concat(mp3);  
    }  
    else{  
    mp4="file:///".concat(mp3);  
    }  
    System.out.println(mp4);  
   this.url = new URL(mp4);  
   }catch(java.net.MalformedURLException e)  
      {System.out.println(e.getMessage());}  
}  
public void run()  
{  
try{  
    System.out.println(url);  
   mediaLocator = new MediaLocator(url);       
   playMP3 = Manager.createPlayer(mediaLocator);  
    }catch(java.io.IOException e)  
      {System.out.println(e.getMessage());  
    }catch(javax.media.NoPlayerException e)  
      {System.out.println(e.getMessage());}  
playMP3.addControllerListener(new ControllerListener()  
  {  
  public void controllerUpdate(ControllerEvent e)  
     {  
     if (e instanceof EndOfMediaEvent)  
         {  
         playMP3.stop();  
         playMP3.close();  
         }  
     }  
  }  
 );  
 playMP3.realize();  
 playMP3.start();  
 }   
}  

*真正问题所在的打盹班*

     package alarm;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import javax.swing.JButton;  
import javax.swing.JComboBox;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
public class Snooze extends Thread implements ActionListener{  
    JFrame sozef=new JFrame("Snooze");  
    JLabel tl=new JLabel("After");  
    JLabel ml=new JLabel("mins");  
    JComboBox tiec=new JComboBox();  
    JButton soo=new JButton("Snooze");  
    JButton stp=new JButton("Stop");  
        mp3 mi=new mp3();  
        Snooze sz;  
        public void run(){  
            sozef.setSize(500,700);  
        sozef.setLayout(null);  
        tl.setBounds(50,50,50,50);  
        tiec.setBounds(50,90,50,30);  
        ml.setBounds(120,90,50,50);  
        soo.setBounds(50,130,90,30);  
        stp.setBounds(50, 170, 90, 30);  
        JcAdd(tiec);  
        sozef.add(tl);  
        sozef.add(tiec);  
        sozef.add(ml);  
        sozef.add(soo);  
        sozef.add(stp);  
        soo.addActionListener(this);  
        sozef.setVisible(true);  
        sozef.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        }  
    void JcAdd(JComboBox jc){  
        for(int i=0;i<=10;i++)  
            jc.addItem(Integer.toString(i));  
    }  
    public void actionPerformed(ActionEvent arg0) {  
        if(arg0.getSource()==soo){  
        int ma=Integer.parseInt(tiec.getSelectedItem().toString());  
        mi.stop();  
        IsTime sz=new IsTime();  
        sz.makeReady(ma);  
        }  
        mi.stop();  
    }  
}  ![this is the frame I see when the snooze is being called][1]
4

1 回答 1

1

这不是实现延迟的正确方法:

while(!(Calendar.getInstance().after(ch))){  
}  

事实上,它会导致严重的问题。

尝试计算目标日期和当前时间之间的毫秒数,并将该值传递给Thread.sleep. 您的电脑机箱爱好者会感谢您的。很可能是您的繁忙循环正在使 AWT 事件线程挨饿,从而阻止您的框架显示。

于 2012-12-29T05:16:45.760 回答