Create a Simple Calculator using Java AWT:
- Create a Java Class (
'CalculatorApp'
):- We’ll create a class named
'CalculatorApp'
that extendsFrame
(from AWT) and implements'ActionListener'
to handle button clicks.
- We’ll create a class named
- Define GUI Components:
- Create labels for displaying instructions and results (
'Label'
). - Create text fields for user input and displaying the result (
'TextField'
). - Create buttons for operations (
'Button'
).
- Create labels for displaying instructions and results (
- Set Layout and Positioning:
- Use layout managers like
'FlowLayout'
or'BorderLayout'
for dynamic resizing and alignment of components.
- Use layout managers like
- Handle Button Actions:
- Implement
'actionPerformed(ActionEvent e)'
method to perform operations based on button clicks.
- Implement
- Display the Frame:
- Set the frame visible and specify its size.
Program to create a simple calculator using Java AWT
- Set the frame visible and specify its size.
import java.awt.*; import java.awt.event.*; class Calculator implements ActionListener { Frame f=new Frame(); Label l1=new Label("Enter Number"); Label l2=new Label("Enter Number"); Label l3=new Label("Result"); TextField t1=new TextField(); TextField t2=new TextField(); TextField t3=new TextField(); Button b1=new Button("Add"); Button b2=new Button("Sub"); Button b3=new Button("Mul"); Button b4=new Button("Div"); Calculator() { l1.setBounds(50,100,100,20); l2.setBounds(50,150,100,20); l3.setBounds(50,200,100,20); t1.setBounds(200,100,100,20); t2.setBounds(200,150,100,20); t3.setBounds(200,200,100,20); b1.setBounds(50,250,50,20); b2.setBounds(110,250,50,20); b3.setBounds(170,250,50,20); b4.setBounds(230,250,50,20); f.add(l1); f.add(l2); f.add(l3); f.add(t1); f.add(t2); f.add(t3); f.add(b1); f.add(b2); f.add(b3); f.add(b4); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); f.setLayout(null); f.setVisible(true); f.setSize(500,500); } public void actionPerformed(ActionEvent e) { int i=Integer.parseInt(t1.getText()); int j=Integer.parseInt(t2.getText()); if(e.getSource()==b1) { t3.setText(String.valueOf(i+j)); } if(e.getSource()==b2) { t3.setText(String.valueOf(i-j)); } if(e.getSource()==b3) { t3.setText(String.valueOf(i*j)); } if(e.getSource()==b4) { t3.setText(String.valueOf(i/j)); } } public static void main(String args[]) { new Calculator() }
Explanation:
- Labels (
'Label'
): Used to display instructions (‘Number 1:'
, ‘Number 2:'
, ‘Result:'
). - TextFields (
'TextField'
): Input fields for entering numbers ('textField1'
,textField2
) and displaying the result ('resultField'
). - Buttons (
'Button'
):'addButton'
,'subButton'
,'mulButton'
,'divButton'
for performing operations. - Layout (
'GridLayout'
): Using'GridLayout(5, 2, 10, 10)'
to organize components in rows and columns with specific gaps between them. - ActionListener (
'ActionListener'
): Implemented to handle button clicks and perform corresponding arithmetic operations.