/*
 * SumApplet.java
 *
 * Copyright 2013 Ajay Bhatia <ajay@dumb-box>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;

public class SumApplet extends JApplet {
    private JLabel lblFirstNumber;
    private JLabel lblSecondNumber;
    private JLabel lblResult;
    private JTextField txtFirst;
    private JTextField txtSecond;
    private JButton btnSum;
   
    public void init() {
        lblFirstNumber = new JLabel("Enter First Number:");
        lblSecondNumber = new JLabel("Enter Second Number:");
        txtFirst = new JTextField(4);
        txtSecond = new JTextField(4);
        lblResult = new JLabel("Result");
        btnSum = new JButton("Add");
       
        setLayout(new GridLayout(3, 2));
       
        add(lblFirstNumber);
        add(txtFirst);
        add(lblSecondNumber);
        add(txtSecond);
        add(lblResult);
        add(btnSum);
       
        btnSum.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int a = Integer.parseInt(txtFirst.getText());
                int b = Integer.parseInt(txtSecond.getText());
               
                int c = a + b;
               
                lblResult.setText("Sum of " + a + " and " + b + " is " + c);
            }
        });
    }
   
}

Comments

Popular posts from this blog

Zeller's Congruence

Property Event-Delegation

Method with variable arguments