You are on page 1of 12

V.L.B.

JANAKIAMMAL COLLEGE OF ARTS


AND SCIENCE
AUTONOMOUS

Accredited By NAAC Grade ‘A’

Affiliated To Bharathiar University And


Approved By UGC And AICT

Department Of
ComputerApplication(BCA)
BATCH:2021-24

KOVAIPUDUR,COIMBATORE-641042
MiniProject
RegisterNumber:21BCA031
Name: Madhan.D
Class:3rd Year ‘A’

Project Title
AWT & SWING IN JAVA
USING AWT & SWING IN JAVA
• AWT stands for Abstract window toolkit is an
Applica on programming interface (API) for crea
ng Graphical User Interface (GUI) in Java. It
allows Java programmers to develop windowbased
applica ons.
• AWT provides various components like bu on,
label, checkbox, etc. used as objects inside a Java
Program. AWT components use the resources of
the opera ng system, i.e., they are pla
ormdependent, which means, component's view
can be changed according to the view of the opera
ng system. The classes for AWT are provided by
the Java.awt package for various AWT
components.
• Java Swing is a GUI (graphical user Interface)
widget toolkit for Java. Java Swing is a part of
Oracle’s Java founda on classes . Java Swing is an
API for providing graphical user interface
elements to Java Programs.Swing was created to
provide more powerful and flexible components
than Java AWT (Abstract Window Toolkit).
• In this ar cle we will use Java Swing
components to create a simple calculator with only
+, -, /, * opera ons.

Calculator in java
• We will learn how to build a simple calculator
using Java AWT. This calculator has some
simple func onality like all the basic mathema
cal opera ons and some special addon features
, we will see as we follow • So let’s get
started
• FIRST of all small descrip on on AWT
AWT(Abstract Window Toolkit) is an API that helps in
building GUI (Graphical User Interface) based java
applica ons. GUI helps in user interac ons using some
graphics. It primarily consists of a set of classes and
methods that are required for crea ng and managing the
GUI in a simplified manner such as bu ons, windows,
frame, tex ield, RadioBu on etc
• I have provided the Java code for the
calculator which uses Ac on listener interface
for EventHandling.
• And yes its not an applet code, which is
deprecated
 Program of Awt &Swing Calculator
import javax.swing.*; import
java.awt.*; import
java.awt.event.ActionEvent; import
java.awt.event.ActionListener;
public class Calculator { private JFrame frame;
private JTextField textField; private JButton[]
numberButtons; private JButton[] functionButtons;
private JButton addButton, subtractButton,
multiplyButton, divideButton; private JButton
decimalButton, equalButton, clearButton;
private JPanel panel;
private double num1, num2, result;
private String operator;
public Calculator() { frame =
new JFrame("Calculator");
frame.setDefaultCloseOperation(JFram
e.EXIT_ON_CL OSE);
frame.setSize(300, 400);
frame.setLayout(null);
textField = new JTextField();
textField.setBounds(30, 25, 240, 40);
frame.add(textField);
numberButtons = new JButton[10];
for (int i = 0; i < 10; i++) {
numberButtons[i] = new
JButton(String.valueOf(i));
numberButtons[i].addActionListener(new
NumberButtonListener());
}
functionButtons = new JButton[7];
addButton = new JButton("+");
subtractButton = new JButton("-");
multiplyButton = new JButton("*");
divideButton = new JButton("/");
decimalButton = new JButton(".");
equalButton = new JButton("="); clearButton
= new JButton("C");
functionButtons[0] = addButton;
functionButtons[1] = subtractButton;
functionButtons[2] = multiplyButton;
functionButtons[3] = divideButton;
functionButtons[4] = decimalButton;
functionButtons[5] = equalButton;
functionButtons[6] = clearButton;
for (int i = 0; i < 7; i++) {
functionButtons[i].addActionListener(new
FunctionButtonListener());
functionButtons[i].setFont(new Font("Arial",
Font.BOLD, 16));
}
panel = new JPanel();
panel.setBounds(30, 80, 240, 250);
panel.setLayout(new GridLayout(4, 4, 10, 10));
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(divideButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(multiplyButton);
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(subtractButton);
panel.add(numberButtons[0]);
panel.add(decimalButton);
panel.add(equalButton); panel.add(addButton);
frame.add(panel);
frame.setVisible(true);
}
private class NumberButtonListener implements
ActionListener {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
textField.setText(textField.getText() +
button.getText());
}
}
private class FunctionButtonListener implements
ActionListener {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String buttonText = button.getText();
if (buttonText.equals("C")) {
textField.setText("");
} else if (buttonText.equals("=")) {
num2 =
Double.parseDouble(textField.getText());
switch (operator) { case "+":
result = num1 + num2;
break; case "-":
result = num1 - num2;
break; case "*":
result = num1 * num2;
break; case "/":
if (num2 != 0)
result = num1 / num2;
else

textField.setText("Error: Cannot
divide by zero");
break;
}
textField.setText(String.valueOf(result));
} else {
num1 =
Double.parseDouble(textField.getText());
operator = buttonText;
textField.setText("");
}
}
}
public static void main(String[] args) {
new Calculator();
}
}

Output
Conclusion
1.AWT (Abstract Window Toolkit): AWT provides a
basic set of components and layouts for crea ng GUIs. It
offers pla orm-specific look and feel, limited customiza
on op ons, and simpler layout management. AWT
components use na ve pla orm resources, making them
compa ble across different opera ng systems.

2.Swing: Swing is an extension of AWT and offers a


more extensive and customizable set of components. It
provides a pluggable look and feel architecture, allowing
developers to customize the appearance of their applica
ons. Swing also offers more advanced layout managers
and addi onal features like improved event handling.

You might also like