HI. I'm new at programing in Java and I made a little program that:
1. Show current date
2. Show curent time
3. When button about pressed pop up about
4. When button setalarm pressed set Alarm(for begining in minutes)
The problem is when i run the program everything works OK. Setting alarm and choosing files works ok but after that when the countdown begins program freeze(time stoped refreshing) and after countdown everything forks fine. I realy need help becouse I tried many things and nothing doesn't work.
MY MAIN CLASS:
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
public class Executor {
public static void main(String[] args) throws InterruptedException {
new Executor();
}
public Executor() throws InterruptedException{
new clock();
}
}
MY CODE FOR ALARM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.Date;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
//sound
import java.awt.Component;
import java.io.*;
import javax.swing.JFileChooser;
import sun.audio.*;
public class clock extends JFrame{
//components
JTextField timeF;
JPanel panel;
JLabel string;
JTextField date;
JTextField countd;
JTextField alarm;
//Buttons for About/Exit/Set Alarm
//buttons
public JButton about;
public JButton exit;
public JButton setalarm;
//program
public clock(){
// housekeeping
super("Java Clock by Boris v0.1A");
setSize(650,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
//initiliaze the panel
panel = new JPanel();
panel.setLayout(new FlowLayout());
//initiliaze the date
date = new JTextField(10);
date.setEditable(false);
date.setFont(new Font("Arial",Font.PLAIN,36));
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(Calendar.YEAR);
int day= rightNow.get(Calendar.DAY_OF_MONTH);
int month = rightNow.get(Calendar.MONTH);
date.setText("Date: "+day+"/"+month+"/"+year);
panel.add(date);
add(panel);
//initiliaze the textField for time
timeF = new JTextField(10);
timeF.setEditable(false);
timeF.setFont(new Font("Arial",Font.PLAIN,36));
panel.add(timeF);
add(panel);
Timer t = new Timer(1000,new Listener());
t.start();
//initialize buttons for app
//ABOUT
about = new JButton("About");
panel.add(about);
add(panel);
AboutClass handler = new AboutClass();
about.addActionListener(handler);
//SetAlarm
setalarm = new JButton("Set Alarm");
panel.add(setalarm);
add(panel);
setAlarm set = new setAlarm();
setalarm.addActionListener(set);
//classes for setting up things
}
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
int min = rightNow.get(Calendar.MINUTE);
int sec = rightNow.get(Calendar.SECOND);
timeF.setText("Time: " + hour+":"+min+":"+sec);
}
}
//Button for about
private class AboutClass implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("About\nThis app is everything you need to wake up\nin the mornings.", event.getActionCommand()));
}
}
//Button for SetAlarm
private class setAlarm implements ActionListener{
public void actionPerformed(ActionEvent event){
String minute = JOptionPane.showInputDialog("Alarm set(minutes)");
int min = Integer.parseInt(minute);
countd = new JTextField(20);
countd.setEditable(false);
countd.setFont(new Font("Arial",Font.PLAIN,36));
panel.add(countd);
add(panel);
countd.setText("Alarm is set for "+ min + " seconds from now");
try{
JFileChooser openf = new JFileChooser();
Component j = null;
openf.showOpenDialog(j);
File fl=openf.getSelectedFile();
for(int i = min*60;i>=0;i-=1){
//initialize for countdown
countd.setText("Alarm is set for "+ i + " seconds from now");
Thread.sleep(1000);
}
for(int x=0;x < 20;x++){ // for looping trought selected sound 20 times
String st = fl.getAbsolutePath();
InputStream in = new FileInputStream(st);
AudioStream au = new AudioStream(in);
AudioPlayer.player.start(au);
Thread.sleep(1000);
}
}
catch(Exception e){}
}
}
}