Thursday, September 26, 2013

Exercise no. 39

package lord1;

import javax.swing.*;
import javax.swing.border.Border;

import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class d5 extends JFrame implements  ActionListener
{
   
  private Container con=getContentPane();
 JLabel first ;
 JButton ok,cancel;
 JTextField firstnum,secondnum,result;
JPanel firstpanel,secondpanel,mainpanel1,mainpanel;
JPanel field1,field2,results;

public void actionPerformed(ActionEvent e)
{
  
    if(e.getSource().equals(ok))
    {
      
        String textFieldValue1="";
        String divisible="";
         String textFieldValue2="";
        Integer result2=0;
         
        if(firstnum.getText().equals("") || secondnum.getText().equals(""))
        {
          
            {
              
            }
            JOptionPane.showMessageDialog(firstpanel, "Please try again");
            firstnum.setText("");
            secondnum.setText("");
        }
        else
        {
            try{
              
            textFieldValue1=firstnum.getText();
            textFieldValue2 = secondnum.getText();
          
          
          
           int x=Integer.parseInt(textFieldValue1);
           int y=Integer.parseInt(textFieldValue2);
         
        
         
              result2=x+y;
            
              if(result2 % 5==0)
              {
                  divisible="and is divisible by 5.";
              }
              else
              {
                  divisible="and is not divisible by 5.";
              }
            
              result.setText("The sum is : "+result2+"\t"+divisible);
            }
            catch (Exception f)
            {
                 JOptionPane.showMessageDialog(firstpanel, "Please use number");
                 firstnum.setText("");
                 secondnum.setText("");
              
            }
        }
      
    }
    else if(e.getSource().equals(cancel))
    {
        firstnum.setText("");
        secondnum.setText("");
        result.setText("");
  
      
    }
}

public d5()
{
    first=new JLabel("Enter first number :");
    ok=new JButton("OK");
    ok.setBackground(Color.gray);
    ok.setForeground(Color.WHITE);
    cancel=new JButton("CANCEL");
    cancel.setBackground(Color.gray);
    cancel.setForeground(Color.WHITE);
    firstnum=new JTextField(10);
    secondnum=new JTextField(10);
    result=new JTextField(20);
  
    result.setEditable(false);
  
    secondpanel=new JPanel(new GridLayout(1,2));
  
    ok.addActionListener(this);
    cancel.addActionListener(this);

    secondpanel.add(ok);
    secondpanel.add(cancel);
  
     Border border = BorderFactory.createLineBorder(Color.orange, 5);
     firstnum.setBorder(border);
     secondnum.setBorder(border);

    firstpanel=new JPanel(new GridLayout(3,1));
    firstpanel.add(first);
    firstpanel.add(firstnum);
    firstpanel.add(secondnum);

  
  

  
    results=new JPanel();
  
  
  
    mainpanel1=new JPanel(new GridLayout(2,1));
    mainpanel1.add(firstpanel);
    mainpanel1.add(secondpanel);
  
  
  
  
  
  
    mainpanel=new JPanel(new GridLayout(1,2));
    mainpanel.add(mainpanel1);
  
    mainpanel.setBackground(Color.GREEN);
  
    con.setLayout(new GridLayout(1,2));
    con.add(mainpanel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    con.setBackground(Color.green);
    con.setLayout(new FlowLayout());
    con.add(results);
  
    results.add(result);
    results.setBackground(Color.orange);
  
  
  
    setSize(500, 200);
    setVisible(true);

}


  
  
    public static void main(String[]args)
    {
        d5 d =new d5();
    }

}

No comments:

Post a Comment