import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class Investment extends JFrame { private JTextField jtfInvestmentAmount; private JTextField jtfYears;

paltryboarpigΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

88 εμφανίσεις

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;


public class Investment extends JFrame {


private JTextField jtfInvestmentAmount;


private JTextField jtfYears;


private JTextField jtfInterestRate;


private JTextField jtfFutureValue;


private JButton jbtCalculate;



public static void main(String[] args) {


JFrame frame = new Investment();


frame.pack();


frame.setTitle("Investment");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
);


frame.setLocationRelativeTo(null); // Center the frame


frame.setVisible(true);


}



public Investment() {


// Create a new panel with GridLayout to hold labels and text boxes


JPanel p = new JPanel();


p.setLayout(new GridLayout(4, 2
, 5, 0));


p.setBorder(new EmptyBorder(5, 5, 5, 5));


p.add(new JLabel("Investment Amount"));


p.add(jtfInvestmentAmount = new JTextField(8));


p.add(new JLabel("Years"));


p.add(jtfYears = new JTextField(8));


p.add(new JLabel("Annual
Interest Rate"));


p.add(jtfInterestRate = new JTextField(8));


p.add(new JLabel("Future value"));


p.add(jtfFutureValue = new JTextField(8));


jtfFutureValue.setEditable(false);



// Add the panel and a button to the frame


setLayout(new

BorderLayout());


add(p, BorderLayout.CENTER);



// Add a button to a panel


JPanel p1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));


p1.add(jbtCalculate = new JButton("Calculate"));


add(p1, BorderLayout.SOUTH);



jbtCalculate.setMnem
onic('C');


jtfInvestmentAmount.setHorizontalAlignment(JTextField.RIGHT);


jtfYears.setHorizontalAlignment(JTextField.RIGHT);


jtfInterestRate.setHorizontalAlignment(JTextField.RIGHT);


jtfFutureValue.setHorizontalAlignment(JTextField.RIGHT);



// Register listener


jbtCalculate.addActionListener(new ActionListener() {


// Handle ActionEvent


public void actionPerformed(ActionEvent e) {


calculate();


}


});


}



// Calculate future value


public

void calculate() {


double investmentAmount =


Double.valueOf(jtfInvestmentAmount.getText().trim()).


doubleValue();


int years =


Integer.valueOf(jtfYears.getText().trim()).intValue();


double interestRate = Double.valueOf(jtfInte
restRate.getText().


trim()).doubleValue();


double futureValue = investmentAmount * Math.pow((1 +


interestRate / (12 * 100)), 12 * years);

// NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

// jtfFutureValue.setText(nf.f
ormat(futureValue));


jtfFutureValue.setText((int)(futureValue * 100) / 100.0 + "");


}

}