Sunday, 23 March 2014

Calculator in java swings with image

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

public class nups extends JFrame implements ActionListener {

JFrame jf;
JPanel jp;
JTextField tf1,tf2,tf3;
JButton jb,jb2,jb3,jb4,jb5;
JLabel jl1,jl2,jl3;
nups()
{
jf = new JFrame("calculator");
jf.setSize(500,500);
jf.setLayout(null);
tf1 = new JTextField(20);
Dimension Size = tf1.getPreferredSize();
tf1.setBounds(400, 20, Size.width, Size.height);
tf1.setVisible(true);
jf.add(tf1);
tf2 = new JTextField(20);
tf2.getPreferredSize();
tf2.setBounds(400, 100, Size.width, Size.height);
tf2.setVisible(true);
jf.add(tf2);
tf3 = new JTextField(20);
tf3.getPreferredSize();
tf3.setBounds(400, 180, Size.width, Size.height);
tf3.setVisible(true);


jf.add(tf3);
jb = new JButton("add");
jb.getPreferredSize();
jb.setBounds(400, 400, Size.width, Size.height);
jb.setVisible(true);
jb.addActionListener(this);
jf.add(jb);

jb2 = new JButton("done");
jb2.getPreferredSize();
jb2.setBounds(600, 600, Size.width, Size.height);
jb2.setVisible(true);
jb2.addActionListener(this);
jf.add(jb2);

jb3 = new JButton("subtract");
jb3.getPreferredSize();
jb3.setBounds(700, 400, Size.width, Size.height);
jb3.setVisible(true);
jb3.addActionListener(this);
jf.add(jb3);

jb4 = new JButton("divide");
jb4.getPreferredSize();
jb4.setBounds(100, 400, Size.width, Size.height);
jb4.setVisible(true);
jb4.addActionListener(this);
jf.add(jb4);

jb5 = new JButton("multiply");
jb5.getPreferredSize();
jb5.setBounds(1000, 400, Size.width, Size.height);
jb5.setVisible(true);
jb5.addActionListener(this);
jf.add(jb5);

jl1 = new JLabel("enter 1 number");
jl1.getPreferredSize();
jl1.setBounds(20,20,Size.width,Size.height);
jl1.setVisible(true);
jf.add(jl1);

jl2 = new JLabel("enter 2 number");
jl2.getPreferredSize();
jl2.setBounds(20,100,Size.width,Size.height);
jl2.setVisible(true);
jf.add(jl2);

jl3 = new JLabel("result");
jl3.getPreferredSize();
jl3.setBounds(20,180,Size.width,Size.height);
jl3.setVisible(true);
jf.add(jl3);

ImageIcon image = new ImageIcon("C:/Users/RAJPUTS/Desktop/simba.jpg");
JLabel jl = new JLabel(image);
jl.getPreferredSize();
jl.setSize(1366,768);
jl.setVisible(true);
jf.add(jl);
jf.setVisible(true);
jf.setResizable(true);

}
public void actionPerformed (ActionEvent ae)

{

try{
float f = Float.parseFloat(tf2.getText());
float e = Float.parseFloat(tf1.getText());
float mn = f+e;
float lk = e-f;
float j = e/f;
Float k = f*e;
if(ae.getSource()== jb)
{
tf3.setText(Float.toString(mn));
}
if(ae.getSource()==jb3)
{
tf3.setText(Float.toString(lk));
}
if(ae.getSource()==jb4)
{
tf3.setText(Float.toString(j));
}
if(ae.getSource()==jb5)
{
tf3.setText(Float.toString(k));
}
if(ae.getSource()==jb2)
{
jf.setVisible(false);
}
}

catch( NumberFormatException e)
{
String msg;
msg = e.getMessage();
JFrame rt = new JFrame("error");
rt.setSize(200,200);
jp = new JPanel();
jp.setSize(500,500);
jp.setBackground(Color.red);
rt.add(jp);
JTextField zx = new JTextField(10);
Dimension Size = zx.getPreferredSize();
zx.setBounds(20,20,Size.width,Size.height);
zx.setText("error "+msg);
zx.setVisible(true);
jp.add(zx);
JTextField x = new JTextField(10);
x.getPreferredSize();
x.setBounds(0,0,Size.width,Size.height);
x.setText("you are mad");
jp.add(x);
x.setVisible(true);

jp.setVisible(true);
rt.setVisible(true);
if(ae.getSource()==jb2)
{
jf.setVisible(false);
rt.setVisible(false);
}
}
}
public static void main(String arg[])
{
nups fr = new nups();
}
}

No comments:

Post a Comment