728x90
반응형
이전 글에 이어 계산 버튼에 대한 이벤트 처리기를 추가합니다.
각 버튼에 대한 이벤트 처리를 위해 아래와 같이 코딩해줍니다.
// 초기화
tfPrincipal.setText("0,00");
tfInerestRate.setText("7.55");
tfPeriod.setText("0");
tfInterestEarned.setText("0.00");
tfAmountEarned.setText("0.00");
checkPeriod1Month.setState(true);
// 닫기 버튼
buttonClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// 계산 버튼
buttonCalc.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String strPrincipal = tfPrincipal.getText();
String strInterest = tfInerestRate.getText();
String strPeriod = tfPeriod.getText();
double fPrincipal = Double.parseDouble(strPrincipal);
double fInterest = Double.parseDouble(strInterest);
fInterest /= 100;
int iPriod = Integer.parseInt(strPeriod);
int iCompundType = 12;
if (checkPeriod1Month.getState() == true)
iCompundType = 12;
else if (checkPeriod3Month.getState() == true)
iCompundType = 4;
else if (checkPeriod6Month.getState() == true)
iCompundType = 2;
else
iCompundType = 1;
double fcompoundInterest =
fInterest / iCompundType;
int n = iCompundType * iPriod;
double fRatePerPeriod = fInterest / iPriod;
double fFutureValue =
fPrincipal * Math.pow(
1 + fcompoundInterest, n);
double fInterestEarned = fFutureValue - fPrincipal;
String strInterestEarned = String.format("$%,2f", fInterestEarned);
String strAmountEarned = String.format("$%,2f", fFutureValue);
tfInterestEarned.setText(strInterestEarned);
tfAmountEarned.setText(strAmountEarned);
}
});
전체 완성 코드
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Main {
public static void main(String[] args) {
Frame calcFrame = new Frame();
calcFrame.setTitle("복리 이자 계산기");
calcFrame.setBounds(100,100,600,400);
calcFrame.setResizable(false);
calcFrame.setLayout(null);
Button buttonCalc = new Button("계산");
Button buttonClose = new Button("닫기");
buttonCalc.setBounds(500,30,90,40);
buttonClose.setBounds(500,75,90,40);
calcFrame.add(buttonCalc);
calcFrame.add(buttonClose);
calcFrame.setBackground(Color.LIGHT_GRAY);
Panel panelLeft = new Panel() {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawRect(5,5,230,230);
g.setColor(Color.LIGHT_GRAY);
g.drawLine(7,5,37,5);
g.setColor(Color.BLACK);
g.drawString("준비",10,10);
}
};
panelLeft.setBounds(10,35,240,240);
calcFrame.add(panelLeft);
Panel panelRight = new Panel() {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawRect(5,5,230,230);
g.setColor(Color.LIGHT_GRAY);
g.drawLine(7,5,92,5);
g.setColor(Color.BLACK);
g.drawString("복리 계산 주기",10,10);
}
};
panelRight.setBounds(250,35,240,240);
calcFrame.add(panelRight);
Panel panelBottom = new Panel() {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawRect(5,5,470,105);
g.setColor(Color.LIGHT_GRAY);
g.drawLine(7,5,37,5);
g.setColor(Color.BLACK);
g.drawString("결과",10,10);
}
};
panelBottom.setBounds(10,275,480,115);
calcFrame.add(panelBottom);
panelLeft.setLayout(null);
Label labelPrincipal = new Label("원금");
labelPrincipal.setBounds(30,30,40,50);
panelLeft.add(labelPrincipal);
Label labelInterestRate = new Label("이율");
labelInterestRate.setBounds(30,100,40,50);
panelLeft.add(labelInterestRate);
Label labelPeriod = new Label("기간");
labelPeriod.setBounds(30,170,40,50);
panelLeft.add(labelPeriod);
final TextField tfPrincipal = new TextField(16);
tfPrincipal.setBounds(70,38,100,30);
panelLeft.add(tfPrincipal);
final TextField tfInerestRate = new TextField(16);
tfInerestRate.setBounds(70,108,100,30);
panelLeft.add(tfInerestRate);
final TextField tfPeriod = new TextField(16);
tfPeriod.setBounds(70,178,100,30);
panelLeft.add(tfPeriod);
Label labelPer = new Label("%");
labelPer.setBounds(180,100,40,50);
panelLeft.add(labelPer);
Label labelYears = new Label("years");
labelYears.setBounds(180,170,40,50);
panelLeft.add(labelYears);
CheckboxGroup group = new CheckboxGroup();
final Checkbox checkPeriod1Month =
new Checkbox(" 1개월",group,false);
final Checkbox checkPeriod3Month =
new Checkbox(" 3개월",group,false);
final Checkbox checkPeriod6Month =
new Checkbox(" 6개월",group,false);
final Checkbox checkPeriod1Year =
new Checkbox(" 12개월",group,true);
panelRight.setLayout(null);
checkPeriod1Month.setBounds(50,30,80,50);
checkPeriod3Month.setBounds(50,80,80,50);
checkPeriod6Month.setBounds(50,130,80,50);
checkPeriod1Year.setBounds(50,180,80,50);
panelRight.add(checkPeriod1Month);
panelRight.add(checkPeriod3Month);
panelRight.add(checkPeriod6Month);
panelRight.add(checkPeriod1Year);
panelBottom.setLayout(null);
Label labelInterestEarnd = new Label("이자");
labelInterestEarnd.setBounds(30,30,40,50);
panelBottom.add(labelInterestEarnd);
Label labelAmountEarnd = new Label("총액");
labelAmountEarnd.setBounds(230,30,40,50);
panelBottom.add(labelAmountEarnd);
final TextField tfInterestEarned = new TextField(16);
tfInterestEarned.setBounds(70,38,100,30);
panelBottom.add(tfInterestEarned);
final TextField tfAmountEarned = new TextField(16);
tfAmountEarned.setBounds(270,38,100,30);
panelBottom.add(tfAmountEarned);
// 초기화
tfPrincipal.setText("0,00");
tfInerestRate.setText("7.55");
tfPeriod.setText("0");
tfInterestEarned.setText("0.00");
tfAmountEarned.setText("0.00");
checkPeriod1Month.setState(true);
// 닫기 버튼
buttonClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// 계산 버튼
buttonCalc.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String strPrincipal = tfPrincipal.getText();
String strInterest = tfInerestRate.getText();
String strPeriod = tfPeriod.getText();
double fPrincipal = Double.parseDouble(strPrincipal);
double fInterest = Double.parseDouble(strInterest);
fInterest /= 100;
int iPriod = Integer.parseInt(strPeriod);
int iCompundType = 12;
if (checkPeriod1Month.getState() == true)
iCompundType = 12;
else if (checkPeriod3Month.getState() == true)
iCompundType = 4;
else if (checkPeriod6Month.getState() == true)
iCompundType = 2;
else
iCompundType = 1;
double fcompoundInterest =
fInterest / iCompundType;
int n = iCompundType * iPriod;
double fRatePerPeriod = fInterest / iPriod;
double fFutureValue =
fPrincipal * Math.pow(
1 + fcompoundInterest, n);
double fInterestEarned = fFutureValue - fPrincipal;
String strInterestEarned = String.format("$%,2f", fInterestEarned);
String strAmountEarned = String.format("$%,2f", fFutureValue);
tfInterestEarned.setText(strInterestEarned);
tfAmountEarned.setText(strAmountEarned);
}
});
calcFrame.setVisible(true);
calcFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
728x90
반응형
'즐거운프로그래밍' 카테고리의 다른 글
[자바] 간단한 슈팅게임 만들기 예제 2 (1) | 2023.10.24 |
---|---|
[자바] 간단한 슈팅게임 만들기 예제 1 (0) | 2023.10.24 |
[자바] 대화 상자 프로그램 포팅 : Java JWT 기반(복리 이자 계산기 만들기 1) (0) | 2023.10.24 |
[자바] Canvas의 이해와 활용(그림 파일 불러오기) (0) | 2023.10.24 |
[자바] Canvas의 이해와 활용(컬러 적용하기) (0) | 2023.10.24 |