❗️ 먼저 직접 Properties 파일을 만든 후 진행했다.
- myframe.properties
: 규칙을 지켜가며 짜야지 작동된다.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
public class MyFrame extends JFrame {
private JLabel lblText;
private Properties prop = new Properties();
private JMenuBar mBar;
private JMenu mSettings;
private JMenuItem miProperties;
private File file = new File("myframe.properties");
public MyFrame() {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
prop.load(fis);
} catch (FileNotFoundException fe) {
if(!file.exists()) {
try{
file.createNewFile();
} catch (IOException e1) {
fe.printStackTrace();
}
}
} catch(IOException e) {
e.printStackTrace();
} finally {
IOUtil.closeAll(fis);
}
mBar = new JMenuBar();
mSettings = new JMenu("settings");
miProperties = new JMenuItem("set properties");
mSettings.add(miProperties);
mBar.add(mSettings);
setJMenuBar(mBar);
miProperties.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
new SettingDlg(MyFrame.this);
}
});
lblText = new JLabel(prop.getProperty("myframe.text"), JLabel.CENTER);
add(lblText, BorderLayout.CENTER);
setTitle(prop.getProperty("myframe.title"));
setSize(getIntValue("myframe.width"), getIntValue("myframe.height"));
setLocation(getIntValue("myframe.x"), getIntValue("myframe.y"));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
// String -> int 변환 메서드
private int getIntValue(String key) {
return Integer.parseInt(prop.getProperty(key));
}
// prop 저장
public void setProp(String key, String value) {
prop.setProperty(key, value);
}
// value 값 불러오기
public String getValue(String key) {
return prop.getProperty(key);
}
// key값을 String[]로 변환
public String[] getKeys() {
return prop.keySet().toArray(new String[0]);
}
public void save() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
prop.store(fos, "myframe.properties");
int choice = JOptionPane.showConfirmDialog(
MyFrame.this,
"저장완료! 변경된 내용을 위해 다시 시작해주세요",
"저장알림",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE
);
if(choice == JOptionPane.OK_OPTION) {
JOptionPane.showMessageDialog(
MyFrame.this,
"MyFrame을 종료합니다."
);
System.exit(0);
}
} catch (IOException e) {
JOptionPane.showConfirmDialog(
MyFrame.this,
"저장에 실패하였습니다. 다시 시도해주세요.",
"저장에러",
JOptionPane.ERROR_MESSAGE,
JOptionPane.OK_OPTION
);
} finally {
IOUtil.closeAll(fos);
}
}
public static void main(String[] args) {
new MyFrame();
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SettingDlg extends JDialog {
private MyFrame owner;
private InputPanel[] pnls; // Jpanel[]
private JButton btnSave;
private JButton btnClose;
public SettingDlg(MyFrame owner) {
super(owner, "settings", true);
this.owner = owner;
String[] info = owner.getKeys(); // info: key값 []
pnls = new InputPanel[info.length];
for(int i = 0; i < pnls.length; i++) {
pnls[i] = new InputPanel(info[i]);
}
JPanel pnlCenter = new JPanel(new GridLayout(0, 1));
for(InputPanel pnl : pnls) {
pnlCenter.add(pnl);
}
btnSave = new JButton("save");
btnClose = new JButton("close");
JPanel pnlSouth = new JPanel(new GridLayout(1, 2));
pnlSouth.add(btnSave);
pnlSouth.add(btnClose);
add(pnlCenter, BorderLayout.CENTER);
add(pnlSouth, BorderLayout.SOUTH);
ActionListener aListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnSave) {
for(InputPanel pnl : pnls) {
owner.setProp(pnl.getName(), pnl.getValue());
}
owner.save();
}
if(e.getSource() == btnClose) {
dispose();
}
}
};
btnSave.addActionListener(aListener);
btnClose.addActionListener(aListener);
setTitle("settings");
pack();
setLocationRelativeTo(owner);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
// Inner class: InputPanel(JPanel)
private class InputPanel extends JPanel {
private JLabel lbl;
private JTextField tfInput;
private String name;
private InputPanel(String text) {
this.name = text; // text == name == owner.key
lbl = new JLabel(text, JLabel.LEFT);
lbl.setPreferredSize(new Dimension(150, 20));
tfInput = new JTextField(20);
tfInput.setText(owner.getValue(text)); // 원래 값 갖고옴
add(lbl);
add(tfInput);
}
// key값
public String getName() {
return name;
}
public String getValue() {
return tfInput.getText();
}
}
}