Professional Documents
Culture Documents
AWT Program
AWT Program
AWT Program
import java.awt.*;
import java.awt.event.*;
t1 = new TextField(15);
t2 = new TextField(15);
t3 = new TextField(15);
t3.setEditable(false);
b1 = new Button("Addition");
b2 = new Button("Subtraction");
b3 = new Button("Multiplication");
b4 = new Button("Division");
b5 = new Button("Modulo Division");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
add(p5);
}
private Panel createPanel(Label label, TextField textField) {
Panel panel = new Panel(new GridLayout(1, 2));
panel.add(label);
panel.add(textField);
return panel;
}
if (ae.getSource() == b1) {
t3.setText(String.valueOf(a + b));
} else if (ae.getSource() == b2) {
t3.setText(String.valueOf(a - b));
} else if (ae.getSource() == b3) {
t3.setText(String.valueOf(a * b));
} else if (ae.getSource() == b4) {
t3.setText(String.valueOf((double) a / b));
} else if (ae.getSource() == b5) {
t3.setText(String.valueOf(a % b));
}
} catch (NumberFormatException ex) {
showMessageDialog("Invalid input. Please enter valid numbers.");
} catch (ArithmeticException ex) {
showMessageDialog("Error: " + ex.getMessage());
}
}
Using Swing
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
t1 = new JTextField(15);
t2 = new JTextField(15);
t3 = new JTextField(15);
t3.setEditable(false);
b1 = new JButton("Addition");
b2 = new JButton("Subtraction");
b3 = new JButton("Multiplication");
b4 = new JButton("Division");
b5 = new JButton("Modulo Division");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
add(p5);
}
if (ae.getSource() == b1) {
t3.setText(String.valueOf(a + b));
} else if (ae.getSource() == b2) {
t3.setText(String.valueOf(a - b));
} else if (ae.getSource() == b3) {
t3.setText(String.valueOf(a * b));
} else if (ae.getSource() == b4) {
if (b == 0) {
throw new ArithmeticException("Division by zero");
}
t3.setText(String.valueOf((double) a / b));
} else if (ae.getSource() == b5) {
if (b == 0) {
throw new ArithmeticException("Modulo division by zero");
}
t3.setText(String.valueOf(a % b));
}
} catch (NumberFormatException ex) {
showMessageDialog("Invalid input. Please enter valid numbers.");
} catch (ArithmeticException ex) {
showMessageDialog("Error: " + ex.getMessage());
}
}
Using Frame
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ArithmeticSwing extends JFrame implements ActionListener {
private JLabel l1, l2, l3;
private JTextField t1, t2, t3;
private JButton b1, b2, b3, b4, b5;
public ArithmeticSwing() {
// Set the layout manager for the frame
setLayout(new GridLayout(5, 1));
t1 = new JTextField(15);
t2 = new JTextField(15);
t3 = new JTextField(15);
t3.setEditable(false); // Make the result field read-only
b1 = new JButton("Addition");
b2 = new JButton("Subtraction");
b3 = new JButton("Multiplication");
b4 = new JButton("Division");
b5 = new JButton("Modulo Division");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
if (ae.getSource() == b1) {
t3.setText(String.valueOf(a + b));
} else if (ae.getSource() == b2) {
t3.setText(String.valueOf(a - b));
} else if (ae.getSource() == b3) {
t3.setText(String.valueOf(a * b));
} else if (ae.getSource() == b4) {
t3.setText(String.valueOf((double) a / b));
} else if (ae.getSource() == b5) {
t3.setText(String.valueOf(a % b));
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid input. Please enter valid numbers.");
} catch (ArithmeticException ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Arithmetic Operations");
t1 = new TextField();
t2 = new TextField();
t3 = new TextField();
t3.setEditable(false);
switch (operation) {
case "+":
result = a + b;
break;
case "-":
result = a - b;
break;
case "*":
result = a * b;
break;
case "/":
if (b == 0) {
throw new ArithmeticException("Division by zero");
}
result = a / b;
break;
case "%":
if (b == 0) {
throw new ArithmeticException("Modulo division by zero");
}
result = a % b;
break;
}
t3.setText(String.valueOf(result));
} catch (NumberFormatException ex) {
showErrorMessage("Invalid input. Please enter valid numbers.");
} catch (ArithmeticException ex) {
showErrorMessage("Error: " + ex.getMessage());
}
}