Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

img  ---

📁 프로젝트 구조

SimpleCalculatorProject/
├── src/
│   └── test/
│       └── SimpleCalculator.java          # 메인 클래스
├── resources/                             # 이미지 등 리소스 폴더 (선택)
│   ├── calculator_icon.png
│   └── calculator_image.jpg
├── README.md
├── .gitignore
└── pom.xml                                # Maven 사용 시 (또는 build.gradle)

전체 수정된 README.md (필요하면 전체 교체)

# Simple Calculator - Java Swing GUI

간단한 사칙연산 계산기 GUI 애플리케이션입니다.

## ✨ 주요 기능
- 사칙연산 (`+`, `-`, `*`, `/`)
- 입력값 검증 및 오류 처리
- 0으로 나누기 방지
- 색상별 버튼 디자인
- JOptionPane을 이용한 결과 및 오류 표시
- 이미지 추가가 용이하도록 플레이스홀더 포함

## 📁 프로젝트 구조

```bash
SimpleCalculatorProject/
├── src/
│   └── test/
│       └── SimpleCalculator.java          # 메인 클래스
├── resources/                             # 이미지 등 리소스 폴더 (선택)
│   ├── calculator_icon.png
│   └── calculator_image.jpg
├── README.md
├── .gitignore
└── pom.xml                                # Maven 사용 시

🛠️ 실행 방법

  1. Java JDK 8 이상 설치
  2. SimpleCalculator.java 파일을 컴파일 후 실행
javac -d . src/test/SimpleCalculator.java
java test.SimpleCalculator

📋 전체 소스 코드

package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.awt.Color;

public class SimpleCalculator extends JFrame implements ActionListener {

    private JTextField num1Field;
    private JTextField num2Field;
    private JButton addButton, subtractButton, multiplyButton, divideButton;

    public SimpleCalculator() {
        setTitle("Simple Calculator");
        setSize(450, 350);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setBackground(Color.LIGHT_GRAY);
        setLayout(new BorderLayout(10, 10));

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        panel.setBackground(Color.WHITE);

        // Image Placeholder
        JLabel imageLabel = new JLabel("Image Placeholder");
        imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        imageLabel.setFont(new Font("Arial", Font.ITALIC, 14));
        panel.add(imageLabel);

        // Input Fields
        num1Field = new JTextField();
        num2Field = new JTextField();
        num1Field.setFont(new Font("Arial", Font.PLAIN, 16));
        num2Field.setFont(new Font("Arial", Font.PLAIN, 16));

        // Buttons
        addButton = new JButton("+");
        subtractButton = new JButton("-");
        multiplyButton = new JButton("*");
        divideButton = new JButton("/");

        // Button Colors
        addButton.setBackground(new Color(76, 175, 80));
        subtractButton.setBackground(new Color(244, 67, 54));
        multiplyButton.setBackground(new Color(255, 152, 0));
        divideButton.setBackground(new Color(0, 188, 212));
        addButton.setForeground(Color.WHITE);
        subtractButton.setForeground(Color.WHITE);
        multiplyButton.setForeground(Color.WHITE);
        divideButton.setForeground(Color.WHITE);

        Font btnFont = new Font("Arial", Font.BOLD, 20);
        addButton.setFont(btnFont);
        subtractButton.setFont(btnFont);
        multiplyButton.setFont(btnFont);
        divideButton.setFont(btnFont);

        // Add components
        panel.add(new JLabel("Number 1:"));
        panel.add(num1Field);
        panel.add(new JLabel("Number 2:"));
        panel.add(num2Field);
        panel.add(addButton);
        panel.add(subtractButton);
        panel.add(multiplyButton);
        panel.add(divideButton);

        addButton.addActionListener(this);
        subtractButton.addActionListener(this);
        multiplyButton.addActionListener(this);
        divideButton.addActionListener(this);

        add(panel, BorderLayout.CENTER);
        setLocationRelativeTo(null);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            double num1 = Double.parseDouble(num1Field.getText());
            double num2 = Double.parseDouble(num2Field.getText());
            double result = 0;
            String op = "";

            if (e.getSource() == addButton) {
                result = num1 + num2; op = "+";
            } else if (e.getSource() == subtractButton) {
                result = num1 - num2; op = "-";
            } else if (e.getSource() == multiplyButton) {
                result = num1 * num2; op = "*";
            } else if (e.getSource() == divideButton) {
                if (num2 == 0) {
                    JOptionPane.showMessageDialog(this, "Error: Division by zero!", "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                result = num1 / num2; op = "/";
            }

            String message = String.format("%.2f %s %.2f = %.2f", num1, op, num2, result);
            JOptionPane.showMessageDialog(this, message, "Result", JOptionPane.INFORMATION_MESSAGE);

        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Please enter valid numbers!", "Input Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new SimpleCalculator().setVisible(true));
    }
}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors