/* Calculator program*/import
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JCalculate extends JFrame implements ActionListener
{
JLabel lblnum1=new JLabel("Enter first number");
JTextField txtnum1=new JTextField(20);
JLabel lblnum2=new JLabel("Enter second number");
JTextField txtnum2=new JTextField(20);
JLabel lblresult=new JLabel("Result");
JTextField txtresult=new JTextField(20);
JButton btnadd=new JButton("Add");
JButton btnsub=new JButton("Sub");
JButton btnmul=new JButton("Multiply");
JButton btndiv=new JButton("Divide");
public JCalculate()
{
super("Temperature Conversion");
Container container=getContentPane();
container.setLayout(
new GridLayout(5,2));
container.add(lblnum1);
container.add(txtnum1);
container.add(lblnum2);
container.add(txtnum2);
container.add(lblresult);
container.add(txtresult);
container.add(btnadd);
container.add(btnsub);
container.add(btnmul);
container.add(btndiv);
setSize(300, 300);
setVisible(true);
btnadd.addActionListener(this);
btnsub.addActionListener(this);
btnmul.addActionListener(this);
btndiv.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnadd)
{
double temp=Double.parseDouble(txtnum1.getText())+Double.parseDouble(txtnum2.getText());
txtresult.setText("Addition of two numbers is"+" "+temp);
}
if(e.getSource()==btnsub)
{
double temp=Double.parseDouble(txtnum1.getText())-Double.parseDouble(txtnum2.getText());
txtresult.setText("Subtraction of two numbers is"+" "+temp);
}
if(e.getSource()==btnmul)
{
double temp=Double.parseDouble(txtnum1.getText())*Double.parseDouble(txtnum2.getText());
txtresult.setText("Multiplication of two numbers is"+" "+temp);
}
if(e.getSource()==btndiv)
{
double temp=Double.parseDouble(txtnum1.getText())/Double.parseDouble(txtnum2.getText());
txtresult.setText("Division of two numbers is"+" "+temp);
}
}
public static void main(String args[])
{
JCalculate Calc = new JCalculate();
Calc.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){
System.exit(0);}});
Calc.pack();
Calc.setVisible(true);
}
}

Congrats, you have code. Do you have a question that comes with this code? Also, please use the code-tags when posting code.

Edited 4 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.