我无法看到我的框架内的组件



嗨,我正在编写一个警报应用程序。我有一个贪睡课程,闹钟响了就会叫它。在使用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]

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

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

事实上,这会引起严重的问题。

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

相关内容

  • 没有找到相关文章

最新更新