0% found this document useful (0 votes)
39 views2 pages

AWT Examples Calculator Form

The document provides two Java AWT examples: a Mini Calculator and a Name Input Form. The Mini Calculator allows users to input two numbers and displays their sum, while the Name Input Form prompts users to enter their name and greets them. Both examples demonstrate the use of AWT components and event handling in Java applications.

Uploaded by

nishcheymanuja98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views2 pages

AWT Examples Calculator Form

The document provides two Java AWT examples: a Mini Calculator and a Name Input Form. The Mini Calculator allows users to input two numbers and displays their sum, while the Name Input Form prompts users to enter their name and greets them. Both examples demonstrate the use of AWT components and event handling in Java applications.

Uploaded by

nishcheymanuja98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java AWT Examples - Calculator and Form

1. Mini Calculator using AWT

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

public class CalculatorAWT extends Frame implements ActionListener {


TextField num1, num2, result;
Button add;

CalculatorAWT() {
setTitle("Mini Calculator");
setSize(300, 200);
setLayout(new FlowLayout());

num1 = new TextField(10);


num2 = new TextField(10);
result = new TextField(15);
result.setEditable(false);

add = new Button("Add");

add(new Label("Num 1:"));


add(num1);
add(new Label("Num 2:"));
add(num2);
add(add);
add(new Label("Result:"));
add(result);

add.addActionListener(this);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});

setVisible(true);
}

public void actionPerformed(ActionEvent e) {


int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
result.setText(String.valueOf(n1 + n2));
}

public static void main(String[] args) {


new CalculatorAWT();
}
Java AWT Examples - Calculator and Form

2. Name Input Form using AWT

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

public class FormAWT extends Frame implements ActionListener {


TextField nameField;
Label greeting;
Button submit;

FormAWT() {
setTitle("Simple Form");
setSize(300, 180);
setLayout(new FlowLayout());

nameField = new TextField(15);


submit = new Button("Submit");
greeting = new Label();

add(new Label("Enter your name:"));


add(nameField);
add(submit);
add(greeting);

submit.addActionListener(this);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});

setVisible(true);
}

public void actionPerformed(ActionEvent e) {


String name = nameField.getText();
greeting.setText("Hello, " + name + "!");
}

public static void main(String[] args) {


new FormAWT();
}
}

You might also like