I have tried to strip down my project to what you see. What the program should be is an alarm system for my computer, that loads the alarms from an external file named ALARMS.txt in my C: drive. What I expected to happen was to click on the START ALARMS label and have the alarms load and continuously check. What IS happening is the alarms are loading, but even with a 1 second sleep, I am still unable to close my window. Am I missing something?
Java Code:
package com.scheduler.ryan;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.StringTokenizer;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Alarm2 extends JComponent{
private JFrame frame;
private Dimension dim = new Dimension(1400,500);
private Calendar now;
private JPanel panel;
private JLabel startLabel, stopLabel;
private BufferedReader in;
private File file = new File("C:\\ALARMS.txt");
private int month, day, hour, min, ampm;
private String alarmLines, alarmMonth, alarmDay, alarmHour, alarmMinute, alarmAMPM, alarmName, alarmFile;
private ArrayList<String> alarms = new ArrayList<String>();
private StringTokenizer st;
private boolean alarmsRunning;
public Alarm2(){
setFrame();
setPanels();
loadAlarms();
}
public static void main(String[] args){
new Alarm2();
}
private void setFrame(){
frame = new JFrame();
frame.setMinimumSize(dim);
frame.setMaximumSize(dim);
frame.setPreferredSize(dim);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void setPanels(){
panel = new JPanel();
panel.setSize(ScheduleMain.dim);
panel.setBackground(new Color(0,0,75));
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
Insets i = new Insets(5,5,5,5);
c.gridx = 0;
c.gridy = 2;
c.insets = i;
startLabel = new JLabel("START ALARMS");
startLabel.setFont(new Font("Arial", Font.BOLD, 60));
startLabel.setForeground(Color.WHITE);
startLabel.addMouseListener(new MouseAdapter(){
@Override
public void mouseEntered(MouseEvent e) {
startLabel.setForeground(Color.ORANGE);
super.mouseEntered(e);
}
@Override
public void mouseExited(MouseEvent e) {
startLabel.setForeground(Color.WHITE);
super.mouseExited(e);
}
@Override
public void mouseClicked(MouseEvent e) {
startLabel.setVisible(false);
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
checkAlarms2();
super.mouseClicked(e);
}
});
panel.add(startLabel, c);
frame.add(panel);
}
private void getNow(){
now = Calendar.getInstance();
month = now.get(Calendar.MONTH);
day = now.get(Calendar.DAY_OF_MONTH);
hour = now.get(Calendar.HOUR);
min = now.get(Calendar.MINUTE);
ampm = now.get(Calendar.AM_PM);
}
private void loadAlarms(){
try {
in = new BufferedReader(new FileReader(file));
while((alarmLines = in.readLine()) != null){
alarms.add(alarmLines);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
private void checkAlarms2(){
alarmsRunning = true;
loadAlarms();
while(alarmsRunning){
getNow();
for(int i = 0; i < alarms.size(); i++){
System.out.println(alarms.get(i));
st = new StringTokenizer(alarms.get(i), ",");
alarmMonth = st.nextToken();
alarmDay = st.nextToken();
alarmHour = st.nextToken();
alarmMinute = st.nextToken();
alarmAMPM = st.nextToken();
alarmName = st.nextToken();
alarmFile = st.nextToken();
if(month+1 == Integer.parseInt(alarmMonth) || alarmMonth.equalsIgnoreCase("0")){
if(day == Integer.parseInt(alarmDay) || alarmDay.equalsIgnoreCase("0")){
if(hour == Integer.parseInt(alarmHour) || alarmHour.equalsIgnoreCase("0")){
if(min == Integer.parseInt(alarmMinute)){
if(ampm == Integer.parseInt(alarmAMPM)){
File file = new File(alarmFile);
try {
Desktop.getDesktop().open(file);
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
No comments:
Post a Comment