Lunes, Setyembre 30, 2013

LabExer#39

39. Create a program that will get the sum of the two numbers and determine if it is divisible by 5.



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TheSumGUI extends JFrame{
    private JLabel jl1,jl2,jlResult;
    private JTextField jt1,jt2;
    private JButton btnOK,btnCANCEL;
   
    private ButtonHandlerOK btnok;
    private ButtonHandlerCANCEL btncancel;
   
    public TheSumGUI(){
        jl1=new JLabel("Enter 1st Number: ");
        jl2=new JLabel("Enter 2nd Number: ");
        jlResult=new JLabel();
       
        jt1=new JTextField(20);
        jt2=new JTextField(20);
       
        btnOK=new JButton("OK");
        btnCANCEL=new JButton("CANCEL");
       
        btnok=new ButtonHandlerOK();
        btnOK.addActionListener(btnok);
       
        btncancel=new ButtonHandlerCANCEL();
        btnCANCEL.addActionListener(btncancel);
       
        Container pane= new Container();
        pane.setLayout(new GridLayout(3,2));
       
        pane.add(jl1);
        pane.add(jt1);
        pane.add(btnOK);
        pane.add(jl2);
        pane.add(jt2);
        pane.add(btnCANCEL);
        pane.add(jlResult);
       
        add(pane);       
        setTitle("Amante Inc.");   
        setSize(650,180);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    public class ButtonHandlerOK implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent arg0) {
            int num1,num2;
            num1=Integer.parseInt(jt1.getText());
            num2=Integer.parseInt(jt2.getText());   
           
            if((num1+num2)%5==0){
                jlResult.setText("The sum is: "+(num1+num2)+ " and is divisible by 5.");
            }
            else{
                jlResult.setText("The sum is: "+(num1+num2)+ " and not divisible by 5.");
            }
        }
    }
    public class ButtonHandlerCANCEL implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent arg0) {
            JOptionPane.showMessageDialog(null,"Program terminating...");
            System.exit(0);   
        }       
    }
    public static void main(String[] args) {
        new TheSumGUI();   
    }
}

Walang komento:

Mag-post ng isang Komento