0% found this document useful (0 votes)
7 views1 page

java Practical 22

The document contains Java code for a simple GUI application called AdditionApp that allows users to input two numbers and displays their sum. It uses Swing components such as JTextField, JButton, and JLabel, and handles user input with an ActionListener. The application also includes error handling to prompt the user for valid number entries if the input is incorrect.

Uploaded by

londheprerana72
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)
7 views1 page

java Practical 22

The document contains Java code for a simple GUI application called AdditionApp that allows users to input two numbers and displays their sum. It uses Swing components such as JTextField, JButton, and JLabel, and handles user input with an ActionListener. The application also includes error handling to prompt the user for valid number entries if the input is incorrect.

Uploaded by

londheprerana72
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
You are on page 1/ 1

Practical 22: Q4

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AdditionApp extends JFrame
{
private JTextField num1Field, num2Field, resultField;
private JButton addButton;
public AdditionApp()
{
setTitle("Addition App");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 2, 5, 5));
JLabel num1Label = new JLabel("Enter Number 1:");
num1Field = new JTextField();
JLabel num2Label = new JLabel("Enter Number 2:");
num2Field = new JTextField();
addButton = new JButton("Add");
resultField = new JTextField();
resultField.setEditable(false);
addButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
performAddition();
}
});
add(num1Label);
add(num1Field);
add(num2Label);
add(num2Field);
add(addButton);
add(new JLabel("Result:"));
add(resultField);
setVisible(true);
}
private void performAddition()
{
try
{
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
double sum = num1 + num2;
resultField.setText(String.valueOf(sum));
} catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(this, "Please enter valid numbers!",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args)
{
new AdditionApp();}}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy