AWT Program

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

AWT Program Using Applet

import java.awt.*;
import java.awt.event.*;

public class Arithmetic extends Applet implements ActionListener {


Label l1, l2, l3;
TextField t1, t2, t3;
Button b1, b2, b3, b4, b5;

public void init() {


l1 = new Label("First Number");
l2 = new Label("Second Number");
l3 = new Label("Result");

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);

Panel p1 = createPanel(l1, t1);


Panel p2 = createPanel(l2, t2);
Panel p3 = createPanel(l3, t3);

Panel p4 = new Panel(new GridLayout(1, 5));


p4.add(b1);
p4.add(b2);
p4.add(b3);
p4.add(b4);
p4.add(b5);

Panel p5 = new Panel(new GridLayout(4, 1));


p5.add(p1);
p5.add(p2);
p5.add(p3);
p5.add(p4);

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;
}

public void actionPerformed(ActionEvent ae) {


try {
int a = Integer.parseInt(t1.getText());
int b = Integer.parseInt(t2.getText());

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());
}
}

private void showMessageDialog(String message) {


Dialog dialog = new Dialog(this, "Error", true);
dialog.setLayout(new FlowLayout());
dialog.add(new Label(message));
Button okButton = new Button("OK");
okButton.addActionListener(e -> dialog.dispose());
dialog.add(okButton);
dialog.setSize(200, 100);
dialog.setVisible(true);
}
}

Using Swing
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ArithmeticSwing extends JApplet implements ActionListener {


private JLabel l1, l2, l3;
private JTextField t1, t2, t3;
private JButton b1, b2, b3, b4, b5;

public void init() {


l1 = new JLabel("First Number");
l2 = new JLabel("Second Number");
l3 = new JLabel("Result");

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);

JPanel p1 = createPanel(l1, t1);


JPanel p2 = createPanel(l2, t2);
JPanel p3 = createPanel(l3, t3);

JPanel p4 = new JPanel(new GridLayout(1, 5));


p4.add(b1);
p4.add(b2);
p4.add(b3);
p4.add(b4);
p4.add(b5);

JPanel p5 = new JPanel(new GridLayout(4, 1));


p5.add(p1);
p5.add(p2);
p5.add(p3);
p5.add(p4);

add(p5);
}

private JPanel createPanel(JLabel label, JTextField textField) {


JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(label);
panel.add(textField);
return panel;
}

public void actionPerformed(ActionEvent ae) {


try {
int a = Integer.parseInt(t1.getText());
int b = Integer.parseInt(t2.getText());

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());
}
}

private void showMessageDialog(String message) {


JDialog dialog = new JDialog((Frame) null, "Error", true);
dialog.setLayout(new FlowLayout());
dialog.add(new JLabel(message));
JButton okButton = new JButton("OK");
okButton.addActionListener(e -> dialog.dispose());
dialog.add(okButton);
dialog.setSize(200, 100);
dialog.setVisible(true);
}
}

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));

l1 = new JLabel("First Number");


l2 = new JLabel("Second Number");
l3 = new JLabel("Result");

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);

JPanel p1 = createPanel(l1, t1);


JPanel p2 = createPanel(l2, t2);
JPanel p3 = createPanel(l3, t3);

JPanel p4 = new JPanel(new GridLayout(1, 5));


p4.add(b1);
p4.add(b2);
p4.add(b3);
p4.add(b4);
p4.add(b5);

// Add panels to the frame


add(p1);
add(p2);
add(p3);
add(p4);
}

private JPanel createPanel(JLabel label, JTextField textField) {


JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(label);
panel.add(textField);
return panel;
}

public void actionPerformed(ActionEvent ae) {


try {
int a = Integer.parseInt(t1.getText());
int b = Integer.parseInt(t2.getText());

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());
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
ArithmeticSwing frame = new ArithmeticSwing();
frame.setTitle("Arithmetic Swing Program");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
Using JavaFX
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class ArithmeticJavaFX extends Application {
private TextField t1, t2, t3;

public static void main(String[] args) {


launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Arithmetic Operations");

Label l1 = new Label("First Number");


Label l2 = new Label("Second Number");
Label l3 = new Label("Result");

t1 = new TextField();
t2 = new TextField();
t3 = new TextField();
t3.setEditable(false);

Button b1 = new Button("Addition");


Button b2 = new Button("Subtraction");
Button b3 = new Button("Multiplication");
Button b4 = new Button("Division");
Button b5 = new Button("Modulo Division");

b1.setOnAction(e -> handleOperationButton("+"));


b2.setOnAction(e -> handleOperationButton("-"));
b3.setOnAction(e -> handleOperationButton("*"));
b4.setOnAction(e -> handleOperationButton("/"));
b5.setOnAction(e -> handleOperationButton("%"));

GridPane gridPane = new GridPane();


gridPane.setVgap(10);
gridPane.setHgap(10);
gridPane.add(l1, 0, 0);
gridPane.add(t1, 1, 0);
gridPane.add(l2, 0, 1);
gridPane.add(t2, 1, 1);
gridPane.add(l3, 0, 2);
gridPane.add(t3, 1, 2);

VBox buttonBox = new VBox(10, b1, b2, b3, b4, b5);


gridPane.add(buttonBox, 0, 3, 2, 1);

Scene scene = new Scene(gridPane, 300, 250);


primaryStage.setScene(scene);
primaryStage.show();
}
private void handleOperationButton(String operation) {
try {
int a = Integer.parseInt(t1.getText());
int b = Integer.parseInt(t2.getText());
int result = 0;

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());
}
}

private void showErrorMessage(String message) {


Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.setTitle("Error");

Label label = new Label(message);


Button okButton = new Button("OK");
okButton.setOnAction(e -> dialog.close());

VBox dialogVBox = new VBox(20);


dialogVBox.getChildren().addAll(label, okButton);
dialogVBox.setPadding(new Insets(20));
Scene dialogScene = new Scene(dialogVBox, 200, 100);
dialog.setScene(dialogScene);
dialog.showAndWait();
}
}

You might also like