
hi sir i have an assignment question, but i don't know the java programming code for that question. my question is here:
Q. Write an event driven program to perform the arithmetic operations as shown in the interface
arithematic operators: this is the form heading
enter n1 :textbox
enter n2 :textbox here is some options like add, sub, div,mul,exit.
result :
in the form of a calculator
please help me:

Here is an event driven example that will perform arithmetic calculations. It allows the user to input two numbers into respective textboxes. As per the button option is selected, the result will be displayed into another textbox.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class SimpleCalculate
{
public static void main(String[] args)
{
JFrame f=new JFrame();
f.setLayout(null);
JLabel lab1=new JLabel("Enter Number 1: ");
JLabel lab2=new JLabel("Enter Number 2: ");
JLabel lab3=new JLabel("Result: ");
final JTextField text1=new JTextField(20);
final JTextField text2=new JTextField(20);
final JTextField text3=new JTextField(20);
JButton b1=new JButton("Add");
JButton b2=new JButton("Subtract");
JButton b3=new JButton("Multiply");
JButton b4=new JButton("Division");
lab1.setBounds(20,20,100,20);
text1.setBounds(140,20,100,20);
lab2.setBounds(20,50,100,20);
text2.setBounds(140,50,100,20);
lab3.setBounds(20,80,100,20);
text3.setBounds(140,80,100,20);
b1.setBounds(260,80,80,20);
b2.setBounds(360,80,80,20);
b3.setBounds(460,80,80,20);
b4.setBounds(560,80,80,20);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
int n1=Integer.parseInt(text1.getText());
int n2=Integer.parseInt(text2.getText());
int cal=n1+n2;
text3.setText(Integer.toString(cal));
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
int n1=Integer.parseInt(text1.getText());
int n2=Integer.parseInt(text2.getText());
int cal=0;
if(n1>n2){
cal=n1-n2;
}
else if(n2>n1){
cal=n2-n1;
}
text3.setText(Integer.toString(cal));
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
int n1=Integer.parseInt(text1.getText());
int n2=Integer.parseInt(text2.getText());
int cal=n1*n2;
text3.setText(Integer.toString(cal));
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
int n1=Integer.parseInt(text1.getText());
int n2=Integer.parseInt(text2.getText());
int cal=0;
if(n1>n2){
cal=n1/n2;
}
else if(n2>n1){
cal=n2/n1;
}
text3.setText(Integer.toString(cal));
}
});
f.add(lab1);
f.add(text1);
f.add(lab2);
f.add(text2);
f.add(lab3);
f.add(text3);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.setVisible(true);
f.setSize(700,250);
}
}
For more information, visit the following link: