/* * GUI.java * * Created on 17 August 2007 * * The class that interacts with the user */ package loan; import java.awt.*; import java.awt.event.*; /** * * @author merethe */ public class GUI extends Frame{ Loan loan; static final int W_HEIGHT = 800; //Window height static final int W_WIDTH = 600; //Window width ActionListener buttonListener; TextField moneyInput; TextField yearsInput; TextArea resultArea; /** Creates a new instance of GUI */ public GUI(Loan loan){ this.loan = loan; } /** * Show the input boxes, calculate button and result area **/ public void show_loan_input(){ start_window(); //Make the input panel Panel panel = new Panel(); panel.setLayout(new FlowLayout(FlowLayout.LEFT)); //Add money amount Label moneyLabel = new Label("Amount of money:"); panel.add(moneyLabel); TextField moneyInput = new TextField(); moneyInput.setColumns(10); panel.add(moneyInput); this.moneyInput = moneyInput; //Add number of years Label yearsLabel = new Label("Number of years:"); panel.add(yearsLabel); TextField yearsInput = new TextField(); yearsInput.setColumns(2); panel.add(yearsInput); this.yearsInput = yearsInput; //Add calculation button Button calcButton = new Button("Calculate"); calcButton.addActionListener(this.buttonListener); panel.add(calcButton); //Make the text area panel Panel panel2 = new Panel(); panel2.setLayout(new FlowLayout(FlowLayout.LEFT)); resultArea = new TextArea(30, 90); panel2.add(resultArea); add(panel, BorderLayout.NORTH); add(panel2); end_window(); } /** * Starts a window. This method must be called before a window is made */ private void start_window(){ //Listen to actions ActionListener buttonListener = new ActionListener(){ public void actionPerformed(ActionEvent ae){ String action = ae.getActionCommand(); if(action.equals("Calculate")){ calculate(); } } }; this.buttonListener = buttonListener; } /** * Show the window and add closing mechanism. */ private void end_window(){ //Add closing mechanism addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } ); setSize(W_HEIGHT, W_WIDTH); setVisible(true); } /** * Calculate and display the payment */ private void calculate(){ try{ int money = Integer.parseInt(moneyInput.getText()); loan.set_money(money); int years = Integer.parseInt(yearsInput.getText()); loan.set_years(years); } catch(Exception e){ //TODO: Should give an error message telling the user //to only write numbers } String results = loan.calculate(); resultArea.setText(results); } }