public class MultiWin extends JApplet implements ActionListener { private JButton jbtCalc; private JButton jbtMort;
//Create the frames to hold the calculator and mortgage calculators JFrame calcFrame = new JFrame(); JFrame mortFrame = new JFrame();
//Construct the frame public void init() { //Place the buttons in the frame getContentPane().setLayout(new FlowLayout()); getContentPane().add(jbtCalc = new JButton("Simple Calculator")); getContentPane().add(jbtMort = new JButton("Mortgage"));
class SimpCalc extends JFrame implements ActionListener { // Text fields for Number 1, Number 2, and Result private JTextField jtfNum1, jtfNum2, jtfResult;
// Menu items "Add", "Subtract", "Multiply","Divide" and "Close" private JMenuItem jmiAdd, jmiSub, jmiMul, jmiDiv, jmiClose;
// Default Constructor public SimpCalc() {
// Create menu bar JMenuBar jmb = new JMenuBar(); JFrame calcFrame = new JFrame();
// Add menu "Operation" to menu bar JMenu operationMenu = new JMenu("Operation"); operationMenu.setMnemonic('O'); jmb.add(operationMenu);
// Add menu "Exit" in menu bar JMenu exitMenu = new JMenu("Exit"); exitMenu.setMnemonic('E'); jmb.add(exitMenu);
// Add menu items with mnemonics to menu "Operation" operationMenu.add(jmiAdd= new JMenuItem("Add", 'A')); operationMenu.add(jmiSub = new JMenuItem("Subtract", 'S')); operationMenu.add(jmiMul = new JMenuItem("Multiply", 'M')); operationMenu.add(jmiDiv = new JMenuItem("Divide", 'D')); exitMenu.add(jmiClose = new JMenuItem("Close", 'C'));
// Panel p1 to hold text fields and labels JPanel p1 = new JPanel(); p1.setLayout(new FlowLayout()); p1.add(new JLabel("Number 1")); p1.add(jtfNum1 = new JTextField(3)); p1.add(new JLabel("Number 2")); p1.add(jtfNum2 = new JTextField(3)); p1.add(new JLabel("Result")); p1.add(jtfResult = new JTextField(4)); jtfResult.setEditable(false);
// Panel p2 to hold buttons JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout()); p2.add(jbtAdd = new JButton("Add")); p2.add(jbtSub = new JButton("Subtract")); p2.add(jbtMul = new JButton("Multiply")); p2.add(jbtDiv = new JButton("Divide"));
// Add menubar to the frame setJMenuBar(jmb);
//Add the panels to the frame getContentPane().setLayout(new BorderLayout()); getContentPane().add(p1, BorderLayout.CENTER); getContentPane().add(p2, BorderLayout.SOUTH);
// Handle ActionEvent from buttons and menu items public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand();
// Handle button events if (e.getSource() instanceof JButton) { if ("Add".equals(actionCommand)) calculate('+'); else if ("Subtract".equals(actionCommand)) calculate('-'); else if ("Multiply".equals(actionCommand)) calculate('*'); else if ("Divide".equals(actionCommand)) calculate('/'); } else if (e.getSource() instanceof JMenuItem) { // Handle menu item events if ("Add".equals(actionCommand)) calculate('+'); else if ("Subtract".equals(actionCommand)) calculate('-'); else if ("Multiply".equals(actionCommand)) calculate('*'); else if ("Divide".equals(actionCommand)) calculate('/'); else if ("Close".equals(actionCommand)) System.exit(0); } }
// Calculate and show the result in jtfResult private void calculate(char operator) { // Obtain Number 1 and Number 2 int num1 = (Integer.parseInt(jtfNum1.getText().trim())); int num2 = (Integer.parseInt(jtfNum2.getText().trim())); int result = 0;
// Perform selected operation switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; }
// Set result in jtfResult jtfResult.setText(String.valueOf(result)); }
public class MortCalc extends JFrame implements ActionListener { //Declare and create textfields for interest rate private JTextField jtfAnnualInterestRate = new JTextField(); private JTextField jtfNumOfYears = new JTextField(); private JTextField jtfLoanAmount = new JTextField(); private JTextField jtfMonthlyPayment = new JTextField(); private JTextField jtfTotalPayment = new JTextField();
private JButton jbtComputeMortgage = new JButton("Compute Mortgage");
//Initialize the interface public void init() { //Set properties for the text fields jtfMonthlyPayment.setEditable(false); jtfTotalPayment.setEditable(false);
//Align the text Fields jtfAnnualInterestRate.setHorizontalAlignment(JTextField.RIGHT); jtfNumOfYears.setHorizontalAlignment(JTextField.RIGHT); jtfLoanAmount.setHorizontalAlignment(JTextField.RIGHT); jtfMonthlyPayment.setHorizontalAlignment(JTextField.RIGHT); jtfTotalPayment.setHorizontalAlignment(JTextField.RIGHT);
//Panel p1 to hold labels and text fields JPanel p1 = new JPanel(); p1.setLayout(new GridLayout (5,2)); p1.add(new Label("Annual Interest Rate")); p1.add(jtfAnnualInterestRate); p1.add(new Label("Number of Years")); p1.add(new Label("Loan Amount")); p1.add(jtfLoanAmount); p1.add(new Label("Monthly Payment")); p1.add(jtfMonthlyPayment); p1.add(new Label("Total Payment")); p1.add(jtfTotalPayment); p1.setBorder(new TitledBorder("Enter interest rate, year and loan amount"));
//panel p2 holds the button JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout(FlowLayout.RIGHT)); p2.add(jbtComputeMortgage);
//Add the components to the applet getContentPane().add(p1,BorderLayout.CENTER); getContentPane().add(p2,Bo rderLayout.SOUTH);
//Handler for the button public void actionPerformed(ActionEvent e) { if (e.getSource() == jbtComputeMortgage) { double interest = (Double.valueOf(jtfAnnualInterestRate.getText())).doubleValue(); int year = (Integer.valueOf(jtfNumOfYears.getText())).intValue(); double loan = (Double.valueOf(jtfLoanAmount.getText())).doubleValue();
//Create a mortgage object double Mortgage; Mortgage m = new Mortgage(interest, year, loan);
//Display monthly pament and total payment jtfMonthlyPayment.setText(String.valueOf(m.monthlyPay())); jtfTotalPaym ent.setText(String.valueOf(m.totalPay())); } } } public class Mortgage { private double interest; private int year; private double loan;
// Construct a mortgage with specified interest rate, year and // loan amount public Mortgage(double i, int y, double l) { interest = i/1200.0; year = y; loan = l; }
// Getter method for interest public double getInterest() { return interest; }
// Getter method for year public double getYear() { return year; }
// Getter method for loan public double getLoan() { return loan; }