Friday, November 5, 2010

Create an application which demonstrates the use of JLabel, JButton and JTextField classes of the Swing to perform binary operation. Also implement the concepts of Icon,ToolTipText and shortCutKey within the same program.

/* Program : 3 
   Create an application which demonstrates the use of JLabel, JButton and JTextField classes of the Swing to perform binary operation. Also implement the concepts of Icon,ToolTipText and shortCutKey within the same program.
   Developed by : Malhar Vora
   Developed on : 5-11-2010
   Development Status : Completed and tested
   Email     : vbmade2000@gmail.com
   WebSite   : http://technojungle.co.cc
*******************************************************************************************************************/

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


class P3 extends JFrame implements ActionListener
{ 


 JLabel lblval1 = null;
 JLabel lblval2 = null;
 JLabel lblresult = null;
 JTextField txtFirst =null;
 JTextField txtSecond = null;
 JTextField txtSum = null;
 JButton btnSum = null;
 Icon icn = null;
 
 
 P3()
 {
   lblval1 = new JLabel("First Value");
   lblval2 = new JLabel("Second Value");
   lblresult = new JLabel("Result");
   
   txtFirst = new JTextField(5);
   txtSecond = new JTextField(5);
   txtSum = new JTextField(5);
   
   icn = new ImageIcon("icon.png");
   btnSum = new JButton("Sum",icn);
   
   setLayout(null);
   setTitle("Program 3 - Developed by Malhar Vora");
   setVisible(true);
   setSize(400,250);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
 
  lblval1.setBounds(80,50,100,10);
  txtFirst.setBounds(160,40,100,30);
    
  lblval2.setBounds(60,90,100,10);
  txtSecond.setBounds(160,80,100,30);
    
  lblresult.setBounds(80,120,100,30);
  txtSum.setBounds(160,120,100,30);
  txtSum.setEditable(false);
    
  btnSum.setBounds(160,160,120,30); 
  btnSum.setToolTipText("Click to make summation");
  btnSum.setMnemonic(KeyEvent.VK_S);
  
   
   this.getContentPane().add(lblval1);
   this.getContentPane().add(lblval2);
   this.getContentPane().add(txtFirst);
   this.getContentPane().add(txtSecond);
   this.getContentPane().add(lblresult);
   this.getContentPane().add(txtSum);
   this.getContentPane().add(btnSum);   

  btnSum.addActionListener(this); 
  
  txtFirst.requestFocus();
  
 }

 public void actionPerformed(ActionEvent ae)
 {
  String cmd = ae.getActionCommand();
  
  if(cmd.equals("Sum"))
  {
   
   if(txtFirst.getText().equals(""))
   {
    JOptionPane.showMessageDialog(this,"Please enter first value","Error in summation",JOptionPane.ERROR_MESSAGE);
    txtFirst.requestFocus();
   }
   else if(txtSecond.getText().equals(""))
   {
    JOptionPane.showMessageDialog(this,"Please enter second value","Error in summation",JOptionPane.ERROR_MESSAGE);
    txtSecond.requestFocus();
   }
   else
   {
    try{
     int a,b,c;
     a = Integer.parseInt(txtFirst.getText());
     b = Integer.parseInt(txtSecond.getText());
     c = a+b;
     txtSum.setText("" + c);
    }
    catch(Exception e)
    {
     txtSum.setText("Error in summation");
    }
    
   }
  }
  
 }

 public static void main(String []str)
 {
  P3 p = new P3(); 
 }

}

No comments:

Post a Comment