/* Program : 15 Program to perform numerical operations like calculator using Swing components Development Status : Completed and Tested Developed by : Malhar Vora Developed on : 20-11-2010 Email : vbmade2000@gmail.com WebSite : www.malhar2010.blogspot.com *****************************************************************/ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class P15 extends JFrame implements ActionListener { JLabel lblFirst = null; JLabel lblSecond = null; JLabel lblResult=null; JTextField txtFirst =null; JTextField txtSecond =null; JTextField txtResult=null; JButton btnAdd=null; JButton btnSub=null; JButton btnMul=null; JButton btnDiv=null; P15() { lblFirst = new JLabel("First Value"); lblSecond = new JLabel("Second Value"); lblResult = new JLabel("Result"); txtFirst = new JTextField(); txtSecond = new JTextField(); txtResult = new JTextField(); btnAdd = new JButton("+"); btnSub = new JButton("--"); btnMul = new JButton("X"); btnDiv = new JButton("/"); setTitle("Program 15 - Developed by Malhar Vora"); setSize(280,250); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); lblFirst.setBounds(20,10,100,50); txtFirst.setBounds(110,20,100,30); lblSecond.setBounds(20,50,100,50); txtSecond.setBounds(110,60,100,30); lblResult.setBounds(20,90,100,50); txtResult.setBounds(110,100,100,30); btnAdd.setBounds(20,150,50,50); btnSub.setBounds(80,150,50,50); btnMul.setBounds(140,150,50,50); btnDiv.setBounds(200,150,50,50); txtResult.setEditable(false); getContentPane().add(lblFirst); getContentPane().add(txtFirst); getContentPane().add(lblSecond); getContentPane().add(txtSecond); getContentPane().add(lblResult); getContentPane().add(txtResult); getContentPane().add(btnAdd); getContentPane().add(btnSub); getContentPane().add(btnMul); getContentPane().add(btnDiv); btnAdd.addActionListener(this); btnSub.addActionListener(this); btnMul.addActionListener(this); btnDiv.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String cmd = ae.getActionCommand(); if(cmd.equals("+")) { int a,b,c; a = Integer.parseInt(txtFirst.getText()); b = Integer.parseInt(txtSecond.getText()); c = a+b; txtResult.setText("" + c); } else if(cmd.equals("--")) { int a,b,c; a = Integer.parseInt(txtFirst.getText()); b = Integer.parseInt(txtSecond.getText()); c = a-b; txtResult.setText("" + c); } else if(cmd.equals("X")) { int a,b,c; a = Integer.parseInt(txtFirst.getText()); b = Integer.parseInt(txtSecond.getText()); c = a*b; txtResult.setText("" + c); } else if(cmd.equals("/")) { int a,b,c; try { a = Integer.parseInt(txtFirst.getText()); b = Integer.parseInt(txtSecond.getText()); c = a/b; txtResult.setText("" + c); } catch(Exception e) { txtResult.setText("Error in division"); } } } public static void main(String []str) { P15 p = new P15(); } }
Tuesday, November 23, 2010
Program to perform numerical operations like calculator using Swing components
Labels:
Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.