Problem:
Create a GUI interest calculator in java.

Output:
Solution:
import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JSpinner; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; public class Calculator extends JFrame{ private JLabel amountLabel, rateLabel, yearsLabel, outputLabel; private JTextField amountTextField, rateTextField; private JButton calculateButton; private JSpinner durationSpinner; private JTextArea outputTextArea; private JScrollPane scrollPane; private JButton clearButton; public Calculator() { createGUI(); } private void createGUI() { Container contentPane = getContentPane(); contentPane.setLayout(null); amountLabel = new JLabel(); amountLabel.setText("Principal"); amountLabel.setBounds(16, 16, 76, 24); contentPane.add(amountLabel); amountTextField = new JTextField(); amountTextField.setBounds(108, 16, 100, 24); amountTextField.setHorizontalAlignment(JTextField.RIGHT); contentPane.add(amountTextField); calculateButton = new JButton("Calculate"); calculateButton.setBounds(224, 16, 100, 24); calculateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { processData(); } private void processData() { if (amountTextField.getText().length()!=0 && rateTextField.getText().length()!=0) { double principal = Double.parseDouble(amountTextField .getText()); double rate = Double.parseDouble(rateTextField.getText()); Integer yearsInteger = (Integer) durationSpinner.getValue(); int yearsint = yearsInteger.intValue(); DecimalFormat fmt = new DecimalFormat("$#.##"); double amount; outputTextArea.setText("Year\tAmount"); for (int i = 1; i <= yearsint; i++) { amount = principal * Math.pow(1 + rate / 100, i); outputTextArea.append("\n" + i + "\t" + fmt.format(amount)); } outputTextArea.setCaretPosition(0); } } }); contentPane.add(calculateButton); rateLabel = new JLabel("Interest rate"); rateLabel.setBounds(16, 56, 76, 24); contentPane.add(rateLabel); rateTextField = new JTextField(); rateTextField.setBounds(108, 56, 100, 24); rateTextField.setHorizontalAlignment(JTextField.RIGHT); contentPane.add(rateTextField); clearButton = new JButton("Clear Data"); clearButton.setBounds(224, 56, 100, 24); clearButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { clearData(); } private void clearData() { outputTextArea.setText(""); amountTextField.setText(""); rateTextField.setText(""); durationSpinner.setValue(new Integer(1)); } }); contentPane.add(clearButton); yearsLabel = new JLabel("Years:"); yearsLabel.setBounds(16, 96, 76, 24); contentPane.add(yearsLabel); durationSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 10, 1)); durationSpinner.setBounds(108, 96, 100, 24); contentPane.add(durationSpinner); outputLabel = new JLabel(); outputLabel.setText("Yearly Account Balance:"); outputLabel.setBounds(16, 136, 250, 24); contentPane.add(outputLabel); outputTextArea = new JTextArea(); outputTextArea.setEditable(false); scrollPane = new JScrollPane(outputTextArea); scrollPane.setBounds(16, 165, 300, 90); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); contentPane.add(scrollPane); setSize(340 , 300); setVisible(true); setResizable(false); setLocationRelativeTo(null); } public static void main(String[] args) { Calculator demoFrame = new Calculator(); demoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
No comments :
Post a Comment