본문 바로가기

Study/Java

12. 10. 08 과제

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

import javax.swing.*;
import javax.swing.border.TitledBorder;

class Calculate extends JFrame
{
 private JPanel panel = new JPanel();
 private JLabel PriceText = new JLabel("Price"),
   DownPaymentText = new JLabel("Down Payment"),
   AnnualInterestRateText = new JLabel("Annual Interest Rate");
 private JTextField PriceField = new JTextField(),
   DownPaymentField = new JTextField(),
   AnnualInterestRateField = new JTextField();
 private JButton CalculateButton = new JButton("Calculate");
 private JTextArea textArea = new JTextArea();

 DecimalFormat dollars = new DecimalFormat("$0.00");

 double Total = 0;

 public Calculate()
 {
  // 창 세팅
  setTitle("Car Payment Calculator");
  setSize(500, 600);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  panel.setLayout(null);

  // TextLabel 설정
  PriceText.setBounds(50, 50, 100, 25);
  panel.add(PriceText);
  DownPaymentText.setBounds(50, 100, 100, 25);
  panel.add(DownPaymentText);
  AnnualInterestRateText.setBounds(50, 150, 100, 25);
  panel.add(AnnualInterestRateText);

  PriceField.setBounds(200, 50, 200, 25);
  PriceField.setHorizontalAlignment(JTextField.RIGHT);
  panel.add(PriceField);
  DownPaymentField.setBounds(200, 100, 200, 25);
  DownPaymentField.setHorizontalAlignment(JTextField.RIGHT);
  panel.add(DownPaymentField);
  AnnualInterestRateField.setBounds(200, 150, 200, 25);
  AnnualInterestRateField.setHorizontalAlignment(JTextField.RIGHT);
  panel.add(AnnualInterestRateField);

  CalculateButton.setBounds(150, 200, 150, 40);
  CalculateButton.addActionListener(new MyListener());
  panel.add(CalculateButton);

  textArea.setBounds(30, 265, 430, 250);
  textArea.setText("  Months  Monthly Payments");
  panel.add(textArea);

  add(panel);
  setVisible(true);
 }

 public class MyListener implements ActionListener
 {
  public void actionPerformed(ActionEvent e)
  {
   if (e.getSource() == CalculateButton)
   {
    double temp = ((Integer.parseInt(PriceField.getText()) - Integer
      .parseInt(DownPaymentField.getText())) * ((Double
      .parseDouble(AnnualInterestRateField.getText()) / 100) + 1));

    textArea.setText("  Months  Monthly Payments" +
      "\n  24  " + dollars.format(temp / 24) +
      "\n  36  " + dollars.format(temp / 36) +
      "\n  48  " + dollars.format(temp / 48) +
      "\n  60  " + dollars.format(temp / 60));
   }
  }
 }
}

public class Test
{
 public static void main(String[] args)
 {
  Calculate donate = new Calculate();

  // System.exit(0);
 }
}

'Study > Java' 카테고리의 다른 글

12. 10. 10 실습  (0) 2012.10.14
중간고사  (0) 2012.10.10
12. 09. 26 테스트  (0) 2012.10.01
12. 09. 26 과제  (0) 2012.09.26
12. 09. 25  (0) 2012.09.25