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

Calc

The document contains a Java program that implements a simple GUI sum calculator using Swing. It allows users to input two numbers, calculates their sum when a button is pressed, and displays the result. The application is structured as a JFrame with text fields for input and output, and a button to trigger the calculation.

Uploaded by

nzlkharel
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)
3 views1 page

Calc

The document contains a Java program that implements a simple GUI sum calculator using Swing. It allows users to input two numbers, calculates their sum when a button is pressed, and displays the result. The application is structured as a JFrame with text fields for input and output, and a button to trigger the calculation.

Uploaded by

nzlkharel
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

package HelloWorld;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SumCalculator extends JFrame {

private JTextField num1Field, num2Field, resultField;


private JButton calculateButton;

public SumCalculator() {
setTitle("Sum Calculator");
setSize(300, 150);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);

num1Field = new JTextField(10);


num2Field = new JTextField(10);
resultField = new JTextField(10);
resultField.setEditable(false);

calculateButton = new JButton("Calculate");


calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
int result = num1 + num2;
resultField.setText(Integer.toString(result));
}
});

add(new JLabel("Number 1:"));


add(num1Field);
add(new JLabel("Number 2:"));
add(num2Field);
add(calculateButton);
add(new JLabel("Result:"));
add(resultField);
}

public static void main(String[] args) {


SumCalculator calculator = new SumCalculator();
calculator.setVisible(true);
}
}

You might also like