gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:26 25 4
gpt4 key购买 nike

我已经尝试了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com