java - 我需要知道如何在用户设置时间后调用闹钟声音

标签 java

我已经尝试了 2-3 天来找到一种方法来调用 play() 方法,这是我的警报声音,但没有真正成功...我尝试过 while 和 for 循环、带有 boolean 标志的新 TimerTasks,但没有成功...警报声音在满足用户输入时不会触发,所以我可以请更有经验的人告诉我我做错了什么吗?

这是我的命令类,其中放置了闹钟声音,它本身连接在主类 ClockGui 中

 package Clock;

 import java.io.*;
 import java.text.DecimalFormat;
  import java.util.Calendar;
  import java.util.Timer;
 import java.util.TimerTask;

import javax.swing.*;
import sun.audio.*;

public class Commands {
ClockGui gui;
Time clock;
Timezones zone;
 DecimalFormat df = new DecimalFormat("00");
int hour1 ;
int min1 ;
int sec1;

boolean alarmOn = false;


public Commands (ClockGui gui) {
     this.gui = gui;
     zone = new Timezones(gui);
     clock = new Time(gui);

}

public void setAlarm() {
      hour1 = Integer.parseInt(JOptionPane.showInputDialog("Set the hour of the alarm \n in the form of '00' :"));
      min1 = Integer.parseInt(JOptionPane.showInputDialog("Set the minutes of the alarm \n in the form of '00' :"));
      sec1 = Integer.parseInt(JOptionPane.showInputDialog("Set the seconds of the alarm \n in the form of '00' :"));
      gui.alarm1.setText("" + df.format(hour1) + " : " + df.format(min1) + " : " + df.format(sec1));
      Timer timer2 = new Timer();
       TimerTask task2 = new TimerTask() {
           public void run(){
               Calendar timeNow = Calendar.getInstance();
               int hours = timeNow.get(Calendar.HOUR_OF_DAY);
               int mins = timeNow.get(Calendar.MINUTE);

               if (hours == hour1 && mins == min1) {
                   alarmOn = true;
               }

           }
       };
       timer2.scheduleAtFixedRate(task2,0,1000);



 }

public void play() {
if(alarmOn == true){

    InputStream sound;
    try {
        sound = new FileInputStream(new File("C:\\Users\\User\\workspace\\Alarm Clock\\src\\Clock\\sound.wav"));
        AudioStream alarm = new AudioStream(sound);
        AudioPlayer.player.start(alarm);
    } catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}

}

这是我的主课

package Clock;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

 import javax.imageio.ImageIO;
import javax.print.DocFlavor.URL;
import javax.swing.*;
import javax.swing.event.MenuEvent;
 import javax.swing.event.MenuListener;

 public class ClockGui implements ActionListener, MenuListener,   MouseListener {
  Time clock;
  Commands command;
  Timezones zone;
  JFrame frame = new JFrame("Clock");
  JLabel label1 = new JLabel("The Time is now :");
  JLabel label2 = new JLabel("Stopwatch");
  JLabel label3 = new JLabel("Select a country :");
  JPanel panel1 = new JPanel();
  JPanel panel3 = new JPanel();
  JPanel panel2 = new JPanel();
  JPanel panel4 = new JPanel();
  JPanel panel5 = new JPanel();
  JPanel panel6 = new JPanel();
  JButton start = new JButton("Start");
  JButton stop = new JButton("Stop");
  Container contentPane = new Container();
  JTextField time = new JTextField(10);
  JTextField stopwatch = new JTextField(20);
  JTextField timeZone = new JTextField(6);
  JTextField alarm1 = new JTextField(7);
  JButton alarm = new JButton("Set Alarm");
  JButton reset = new JButton("Reset");


public ClockGui() throws IOException   {
    clock = new Time(this);
    command = new Commands(this);
    zone = new Timezones(this);
    makeFrame();
    if (command.alarmOn == true){
        command.play();
    }


}

public static void main(String[] args) throws IOException   {

    new ClockGui();



}

最佳答案

只需删除 alarmOn 变量并调用 play(),而不是在 TimerTask 中设置 alarmOn = true

关于java - 我需要知道如何在用户设置时间后调用闹钟声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31456427/

相关文章:

java - 如何处理 chrome 驱动程序中的 “Unable to receive message from renderer”?

java - 客户端登录Applet

java - JOGL透明背景

java - 由于隐式 PendingIntent 漏洞的补救措施,Google 拒绝更新

java - 是创建一个新对象并返回它更好,还是在返回语句中创建新对象更好?

javascript - 使用来自已加载网页的 JavaScript 调用重新加载 WebView

Java - 扫描器 : reading line of record separated by Pipe | as delimiter

java - 在 Eclipse 中编程 Java 7

java - Java 中的合并排序未按预期工作。尝试在没有 while 循环的情况下做到这一点

java - 在 Vaadin 中,如何在 Label 中设置样式而不影响整个页面