import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Answer3 extends JFrame implements ActionListener {
private JLabel lblMoney;
private JTextField tfMoney;
private JLabel lblRate;
private JTextField tfRate;
private JButton btnCalc;
private JTextField tfResult;
public Answer3() {
init();
setDisplay();
addListeners();
showFrame();
}
private void init() {
lblMoney = new JLabel("원금을 입력하시오", JLabel.LEFT);
lblRate = new JLabel("이자율을 입력하시오", JLabel.LEFT);
tfMoney = new JTextField(5);
tfRate = new JTextField(5);
btnCalc = new JButton("변환");
tfResult = new JTextField(15);
tfResult.setEditable(false);
tfResult.setBackground(Color.WHITE);
}
private void setDisplay() {
JPanel pnlLeft = new JPanel(new GridLayout(2, 1));
JPanel pnlMoneyInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
pnlMoneyInfo.add(lblMoney);
JPanel pnlRateInfo = new JPanel(new FlowLayout(FlowLayout.LEFT));
pnlRateInfo.add(lblRate);
pnlLeft.add(pnlMoneyInfo);
pnlLeft.add(pnlRateInfo);
JPanel pnlRight = new JPanel(new GridLayout(2, 1));
JPanel pnlMoney = new JPanel();
pnlMoney.add(tfMoney);
JPanel pnlRate = new JPanel();
pnlRate.add(tfRate);
pnlRight.add(pnlMoney);
pnlRight.add(pnlRate);
JPanel pnlSouth = new JPanel(new BorderLayout());
JPanel pnlBtn = new JPanel();
pnlBtn.add(btnCalc);
JPanel pnlResult = new JPanel();
pnlResult.add(tfResult);
pnlSouth.add(pnlBtn, BorderLayout.NORTH);
pnlSouth.add(pnlResult, BorderLayout.CENTER);
add(pnlLeft, BorderLayout.WEST);
add(pnlRight, BorderLayout.CENTER);
add(pnlSouth, BorderLayout.SOUTH);
}
private void showFrame() {
setTitle("이자 계산기");
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void addListeners() {
btnCalc.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae) {
JTextField input = tfMoney;
try {
double money = Double.parseDouble(tfMoney.getText());
input = tfRate;
double rate = Double.parseDouble(tfRate.getText());
money += rate * money;
tfResult.setText(String.valueOf(money));
} catch (NumberFormatException e) {
String target = "이자율";
if(input == tfMoney) {
target = "원금";
}
JOptionPane.showMessageDialog(
this,
target + "은 숫자로 입력해주세요",
"경고",
JOptionPane.WARNING_MESSAGE);
input.requestFocus();
input.selectAll();
}
}
public static void main(String[] args) {
new Answer3();
}
}