Sunday, January 24, 2016

Intro To NetBeans - Action Event



package week1day1;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



/**
 *
 *
 */
public class MainFrame extends javax.swing.JFrame implements ActionListener {

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
     
        //Register the button with the action listner
        jbtnCompute.addActionListener(this);
        jbtnClear.addActionListener(this);
    }

// Variables declaration - do not modify                  
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton jbtnClear;
    private javax.swing.JButton jbtnCompute;
    private javax.swing.JTextField jtxtValue1;
    private javax.swing.JTextField jtxtValue2;
    // End of variables declaration                

    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getSource()==jbtnCompute){
        //read textfields
            double v1 = Double.parseDouble(jtxtValue1.getText());
            double v2 = Double.parseDouble(jtxtValue2.getText());
           //computations;
            double sum = v1+v2;
            double mul = v1*v2;
            double div = v1/v2;
        //display results
            jTextArea1.append("sum = " + sum + "\n" +
                              "Product = " + mul + "\n"+
                              "division = " + div);
        }else if(ae.getSource()== jbtnClear){
            jtxtValue1.setText("");
            jtxtValue2.setText("");
            jTextArea1.setText("");
        }
    }
}

No comments:

Post a Comment