JAVA/TermProject

자바 GUI 텀프 '아스키코드 및 유니코드 변환기' - 공부하는 도비

DOVISH WISDOM 2023. 6. 18. 11:49  
728x90
반응형

자바 GUI를 활용한 아스키코드와 유니코드 변환기입니다. 
아스키코드표도 파일을 저장하여 경로를 입력하면, 출력이 됩니다. 

초기 화면
아스키코드 변환 버튼 클릭
유니코드 변환 버튼 클릭
유니코드표 보기 버튼 클릭
아무것도 입력하지 않고, 아스키코드 변환 및 유니코드 변환 버튼을 눌렀을 때

아래에 전체 코드와 아스키코드표 사진을 첨부해두겠습니다.

위 사진의 "이미지 파일 경로"에 아스키코드표 사진의 경로를 입력해주세요. 

(Ex. "C:/Desktop/Ascll_Code.png")

 

AskiUniConvrtr.java.txt
0.00MB
Ascll_Code.png
0.03MB

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

public class AskiUniConvrtr extends JFrame {
    private JTextField inputTextField;
    private JButton ascicodeButton;
    private JButton unicodeButton;
    private JButton ascicodeViewButton;
    private JButton unicodeViewButton;
    private JLabel resultLabel;
    private JLabel imageLabel;
    private ImageIcon imageIcon;

    public AskiUniConvrtr() {
        setTitle("아스키코드 및 유니코드 변환기");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setSize(700, 300);
        setVisible(true);

        inputTextField = new JTextField(5); 
        add(inputTextField, BorderLayout.NORTH);

        // 버튼패널을 생성
        JPanel buttonPanel = new JPanel(new FlowLayout());

        // 아스키코드로 변환하는 버튼을 생성
        ascicodeButton = new JButton("아스키코드 변환");
        ascicodeButton.setBackground(Color.CYAN); 
        buttonPanel.add(ascicodeButton);

        // 유니코드로 변환하는 버튼을 생성
        unicodeButton = new JButton("유니코드 변환");
        unicodeButton.setBackground(Color.PINK); // 유니코드 버튼을 핑크색으로 설정합니다.
        buttonPanel.add(unicodeButton);

        ascicodeViewButton = new JButton("아스키코드표 보기"); // 아스키 코드 보기 버튼 생성
        ascicodeViewButton.setBackground(Color.orange);
        buttonPanel.add(ascicodeViewButton);
        add(buttonPanel, BorderLayout.CENTER);

        // 결과 레이블 생성
        resultLabel = new JLabel("결과 :");
        resultLabel.setOpaque(true); 
        resultLabel.setFont(new Font("SanSerif", Font.BOLD, 20)); // 글꼴과 크기 설정
        add(resultLabel, BorderLayout.SOUTH);

       // 이미지 레이블 생성
        imageLabel = new JLabel();
        add(imageLabel, BorderLayout.WEST);
 
        // 이미지 아이콘 생성
        imageIcon = new ImageIcon("C://Ascll_Code.png"); // 이미지 파일 경로

        // 아스키 코드 변환 버튼에 대한 이벤트 처리
        ascicodeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String input = inputTextField.getText();
                if (!input.isEmpty()) {
                    char ch = input.charAt(0);
                    int asciiCode = (int) ch;
                    resultLabel.setText("결과: " + asciiCode);
                    imageLabel.setIcon(null); // 이미지 레이블 초기화
                } else {
                    resultLabel.setText("제대로 입력해주세요.");
                    imageLabel.setIcon(null); // 이미지 레이블 초기화
                }
            }
        }
        );

        // 유니코드 변환 버튼에 대한 이벤트 처리
        unicodeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String input = inputTextField.getText();
                if (!input.isEmpty()) {
                    char ch = input.charAt(0);
                    String unicodeCode = String.format("\\u%04x", (int) ch);
                    resultLabel.setText("결과: " + unicodeCode);
                    imageLabel.setIcon(null); // 이미지 레이블 초기화
                } else {
                    resultLabel.setText("잘못된 입력입니다.");
                    imageLabel.setIcon(null); // 이미지 레이블 초기화
                }
            }
        });

        // 아스키 코드 보기 버튼에 대한 이벤트 처리
        ascicodeViewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                resultLabel.setText("결과: "); // 결과 레이블 초기화
                imageLabel.setIcon(imageIcon); // 이미지 레이블에 이미지 아이콘 설정
            }
        });
    }

       
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new AskiUniConvrtr();
            }
        });
    }
}
반응형