본문 바로가기

자바(java)

일기장 애플리케이션: 일기를 작성하고 저장하며 날짜별로 관리하는 애플리케이션을 개발합니다.

반응형

일기장 애플리케이션: 자바 기반 개발

1. 내용 설명

이 프로그램은 자바를 사용하여 개발된 일기장 애플리케이션입니다. 사용자는 일기를 작성하고 저장할 수 있으며, 날짜별로 일기를 관리할 수 있습니다. 이 애플리케이션은 Swing을 사용하여 그래픽 사용자 인터페이스(GUI)를 제공하며, 파일 입출력을 통해 일기 내용을 저장하고 불러옵니다.

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

  • createAndShowGUI(): 사용자 인터페이스를 생성하고 보여줍니다. 일기 작성과 저장을 위한 텍스트 영역과 버튼을 포함합니다.
  • saveDiary(): 현재 작성된 일기를 파일로 저장합니다.
  • loadDiary(String date): 지정된 날짜의 일기를 불러옵니다.
  • main(String[] args): 프로그램의 진입점입니다. GUI를 생성하고 표시합니다.

 

3. 코딩 내용

 

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

public class DiaryApplication {
    private JFrame frame;
    private JTextArea diaryTextArea;
    private JButton saveButton, loadButton;
    private JSpinner dateSpinner;

    public DiaryApplication() {
        createAndShowGUI();
    }

    private void createAndShowGUI() {
        frame = new JFrame("Diary Application");
        diaryTextArea = new JTextArea(20, 40);
        saveButton = new JButton("Save");
        loadButton = new JButton("Load");
        dateSpinner = new JSpinner(new SpinnerDateModel());
        ((JSpinner.DefaultEditor) dateSpinner.getEditor()).getTextField().setEditable(false);

        frame.setLayout(new BorderLayout());
        frame.add(new JScrollPane(diaryTextArea), BorderLayout.CENTER);

        JPanel panel = new JPanel();
        panel.add(new JLabel("Date:"));
        panel.add(dateSpinner);
        panel.add(saveButton);
        panel.add(loadButton);
        frame.add(panel, BorderLayout.SOUTH);

        saveButton.addActionListener(e -> saveDiary());
        loadButton.addActionListener(e -> loadDiary());

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

    private void saveDiary() {
        String fileName = getFormattedDate() + ".txt";
        File file = new File(fileName);
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            diaryTextArea.write(writer);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(frame, "Error saving diary.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void loadDiary() {
        String fileName = getFormattedDate() + ".txt";
        File file = new File(fileName);
        if (file.exists()) {
            try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                diaryTextArea.read(reader, null);
            } catch (IOException e) {
                JOptionPane.showMessageDialog(frame, "Error loading diary.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            diaryTextArea.setText("");
            JOptionPane.showMessageDialog(frame, "No diary entry found for this date.", "Info", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private String getFormattedDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        return formatter.format((Date) dateSpinner.getValue());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(DiaryApplication::new);
    }
}

 

 

4. 전문용어

  • Swing: 자바의 GUI 위젯 툴킷입니다.
  • JFrame: GUI 윈도우를 생성하는 클래스입니다.
  • JTextArea: 여러 줄의 텍스트를 입력받는 컴포넌트입니다.
  • JButton: 사용자가 클릭할 수 있는 버튼을 생성하는 클래스입니다.
  • JFileChooser: 파일 선택기 대화 상자를 생성하는 클래스입니다.
  • BufferedWriter: 텍스트 데이터를 파일에 쓰는 데 사용되는 클래스입니다.
  • IOException: 입출력 관련 예외 처리를 위한 클래스입니다.

 

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

본 프로그램은 자바 표준 라이브러리의 Swing 컴포넌트를 사용하므로, 별도의 라이브러리 추가가 필요하지 않습니다.

 

컴파일 방법:
javac DiaryApplication.java


실행 방법:
java DiaryApplication

 

 

반응형