[Java] 창 설정_한글화 영어화 변경하기

Jeini·2023년 1월 23일
0

📌 Code list

목록 보기
48/55
post-thumbnail

❗️ 영어버전 한글버전의 properties를 각각 만들어준다. (총 2개)

  • 키 값은 동일하게 맞춰준다.
  • properties의 한글은 유니코드로 입력해야 인식하기 때문에 유니코드로 넣어줘야 한다.

✏️ properties


#korean.properties
#Sun Jan 22 20:53:17 KST 2023
quiz.mFile=\uD30C\uC77C
quiz.miOpen=\uC5F4\uAE30
quiz.miExit=\uB098\uAC00\uAE30
quiz.lblMain=\uB808\uC774\uBE14
quiz.lblTip=\uD301\: \uD31D\uC5C5\uBA54\uB274\uB97C \uC0AC\uC6A9\uD558\uC5EC \uC5B8\uC5B4\uB97C \uBCC0\uACBD\uD560 \uC218 \uC788\uC2B5\uB2C8\uB2E4.
#english.properties
#Sun Jan 22 20:53:17 KST 2023
quiz.mFile=File
quiz.miOpen=open
quiz.miExit=exit
quiz.lblMain=Label
quiz.lblTip=Tip: You can change the language using the pop-up menu.

✏️ InternationalFrame


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class InternationalFrame extends JFrame {
    private JMenu mFile;
    private JMenuItem miOpen;
    private JMenuItem miExit;

    private JLabel lblMain;
    private JLabel lblTip;

    private JRadioButton rbtnKorean;
    private JRadioButton rbtnEnglish;
    private JPopupMenu popupMenu;

    private Properties prop = new Properties();
    private File fKorean = new File("korean.properties");
    private File fEnglish = new File("english.properties");

    public InternationalFrame() {
        init();
        setDisplay();
        addListeners();
        showFrame();
    }

    private void init() {
        setLanguage(fKorean);

        mFile = new JMenu(prop.getProperty("quiz.mFile"));
        miOpen = new JMenuItem(prop.getProperty("quiz.miOpen"));
        miExit = new JMenuItem(prop.getProperty("quiz.miExit"));

        lblMain = new JLabel(prop.getProperty("quiz.lblMain"), JLabel.CENTER);
        lblTip = new JLabel(prop.getProperty("quiz.lblTip"));


        popupMenu = new JPopupMenu();
        rbtnKorean = new JRadioButton("한국어", true);
        rbtnEnglish = new JRadioButton("English");
        ButtonGroup group = new ButtonGroup();
        group.add(rbtnKorean);
        group.add(rbtnEnglish);

    }

    private void setDisplay() {
        JMenuBar mBar = new JMenuBar();
        mFile.add(miOpen);
        mFile.addSeparator();
        mFile.add(miExit);
        mBar.add(mFile);
        setJMenuBar(mBar);

        popupMenu.add(rbtnKorean);
        popupMenu.add(rbtnEnglish);

        add(lblMain, BorderLayout.CENTER);
        add(lblTip, BorderLayout.SOUTH);
    }

    private void addListeners() {
        lblMain.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                showPopup(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                showPopup(e);
            }
        });

        ActionListener aListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == rbtnKorean) {
                    setLanguage(fKorean);
                } else {
                    setLanguage(fEnglish);
                }
                setText();
            }
        };
        rbtnKorean.addActionListener(aListener);
        rbtnEnglish.addActionListener(aListener);

        ActionListener bListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == miOpen) {
                    if(rbtnKorean.isSelected()) {
                        JOptionPane.showMessageDialog(InternationalFrame.this, "연다!");
                    } else {
                        JOptionPane.showMessageDialog(InternationalFrame.this, "open!");
                    }
                }
                else {
                    System.exit(0);
                }
            }
        };
        miOpen.addActionListener(bListener);
        miExit.addActionListener(bListener);

    }

    private void showPopup(MouseEvent e) {
        if(e.isPopupTrigger()) {
            popupMenu.show(lblMain, e.getX(), e.getY());
        }
    }

    private void setText() {
        mFile.setText(getVal("quiz.mFile"));
        miOpen.setText(getVal("quiz.miOpen"));
        miExit.setText(getVal("quiz.miExit"));
        lblMain.setText(getVal("quiz.lblMain"));
        lblTip.setText(getVal("quiz.lblTip"));

    }
    
    private String getVal(String key) {
        return prop.getProperty(key);
    }

    private void showFrame() {
        setTitle("International System Demo");
        setSize(500, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

    }

    private void setLanguage(File file) {
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(file);
            prop.load(fis);

        } catch (IOException e) {
            JOptionPane.showConfirmDialog(
                    this,
                    "불러오는데 실패했습니다. 다시 시도해주세요",
                    "Error",
                    JOptionPane.OK_OPTION,
                    JOptionPane.ERROR_MESSAGE
            );

        } finally {
            IOUtill.closeAll(fis);
        }
    }

    public static void main(String[] args) {
        new InternationalFrame();
    }

}

✏️ IOUtill


import java.io.Closeable;

public class IOUtill {
	public static void closeAll(Closeable ...c) {
		for(Closeable temp : c) {
			try {
				temp.close();
			} catch (Exception e) {}
		}
	}
}
profile
Fill in my own colorful colors🎨

0개의 댓글