본문 바로가기

자바(java)

윈도우 알람 시계 애플리케이션: 알람을 설정하고 시간에 맞춰 알람을 울리는 앱을 설계합니다.

반응형

윈도우 알람 시계 애플리케이션: 자바 구현

1. 내용 설명

윈도우 알람 시계 애플리케이션은 사용자가 설정한 시간에 알람을 울려 특정 활동을 상기시키는 애플리케이션입니다. 사용자는 알람 시간과 메시지를 설정할 수 있으며, 지정된 시간에 알람이 울립니다. 간단한 사용자 인터페이스를 통해 알람을 관리할 수 있습니다.

2. 프로그램 간 사용 함수 설명

  • setAlarm(): 알람 시간과 메시지를 설정합니다.
  • startAlarm(): 알람을 활성화하고 시간을 감시합니다.
  • ringAlarm(): 설정된 시간에 도달했을 때 알람을 울립니다.
  • main(): 애플리케이션의 메인 실행 함수입니다.

3. 코딩 내용

 

import javax.swing.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class AlarmClock {
    private JFrame frame;
    private JTextField timeField;
    private JTextArea messageField;
    private JButton setButton;
    private List<Alarm> alarms = new ArrayList<>();

    public AlarmClock() {
        frame = new JFrame("알람 시계 애플리케이션");
        timeField = new JTextField(5);
        messageField = new JTextArea(2, 20);
        setButton = new JButton("알람 설정");
        
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
        frame.add(new JLabel("시간 (HH:mm):"));
        frame.add(timeField);
        frame.add(new JLabel("메시지:"));
        frame.add(messageField);
        frame.add(setButton);

        setButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setAlarm(timeField.getText(), messageField.getText());
            }
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

        startAlarm();
    }

    private void setAlarm(String time, String message) {
        alarms.add(new Alarm(time, message));
    }

    private void startAlarm() {
        java.util.Timer timer = new java.util.Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                String currentTime = new SimpleDateFormat("HH:mm").format(new Date());
                alarms.removeIf(alarm -> {
                    if (alarm.getTime().equals(currentTime)) {
                        ringAlarm(alarm);
                        return true;
                    }
                    return false;
                });
            }
        }, 0, 60000); // 1분 간격으로 확인
    }

    private void ringAlarm(Alarm alarm) {
        JOptionPane.showMessageDialog(frame, "알람! " + alarm.getMessage());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new AlarmClock();
            }
        });
    }

    private class Alarm {
        private String time;
        private String message;

        public Alarm(String time, String message) {
            this.time = time;
            this.message = message;
        }

        public String getTime() {
            return time;
        }

        public String getMessage() {
            return message;
        }
    }
}

 

4. 전문용어

  • JFrame: 자바 스윙 라이브러리의 GUI 프레임을 생성하는 클래스입니다.
  • JTextField, JTextArea: 사용자로부터 텍스트 입력을 받는 컴포넌트입니다.
  • JButton: 클릭 가능한 버튼을 생성하는 컴포넌트입니다.
  • BoxLayout: 컴포넌트를 일렬로 배치하는 레이아웃 매니저입니다.
  • Timer, TimerTask: 지정된 시간에 작업을 수행하기 위한 클래스입니다.

5. 라이브러리 추가 및 실행 방법

  • 본 프로그램은 Java Swing 라이브러리를 사용합니다.

 

컴파일:
javac -encoding UTF-8 AlarmClock.java


실행:
java AlarmClock

반응형