0% found this document useful (0 votes)
4 views3 pages

Practical Twentytwo

The document is a Java program that creates a simple graphical user interface (GUI) for an addition calculator using Swing. It allows users to input two numbers, and upon clicking the 'Add' button, it calculates and displays the sum, handling invalid inputs gracefully. The GUI consists of labels, text fields, and a button organized in a grid layout.

Uploaded by

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

Practical Twentytwo

The document is a Java program that creates a simple graphical user interface (GUI) for an addition calculator using Swing. It allows users to input two numbers, and upon clicking the 'Add' button, it calculates and displays the sum, handling invalid inputs gracefully. The GUI consists of labels, text fields, and a button organized in a grid layout.

Uploaded by

doddamaniasmita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

import javax.swing.

*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class AdditionApp {

public static void main(String[] args) {

JFrame frame = new JFrame("Addition Calculator");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 200);

JPanel panel = new JPanel(new GridLayout(3, 2, 5, 5));

JLabel num1Label = new JLabel("Number 1:");

JTextField num1Field = new JTextField(10);

JLabel num2Label = new JLabel("Number 2:");

JTextField num2Field = new JTextField(10);

JLabel resultLabel = new JLabel("Result:");

JTextField resultField = new JTextField(10);

resultField.setEditable(false);

JButton addButton = new JButton("Add");

addButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

try {

int num1 = Integer.parseInt(num1Field.getText());

int num2 = Integer.parseInt(num2Field.getText());

int sum = num1 + num2;

resultField.setText(String.valueOf(sum));

} catch (NumberFormatException ex) {

resultField.setText("Invalid Input");

});

panel.add(num1Label);

panel.add(num1Field);

panel.add(num2Label);

panel.add(num2Field);

panel.add(resultLabel);

panel.add(resultField);

JPanel buttonPanel = new JPanel();

buttonPanel.add(addButton);

frame.setLayout(new BorderLayout());

frame.add(panel, BorderLayout.CENTER);

frame.add(buttonPanel, BorderLayout.SOUTH);
frame.setVisible(true);

You might also like