Notes
![]() ![]() Notes - notes.io |
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class SumCalculator extends JFrame {
// Swing components
private JTextField num1Field, num2Field, sumField;
public SumCalculator() {
// Set up the frame
setTitle("Sum Calculator");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4, 2));
// Labels and text fields for number 1 and number 2
JLabel num1Label = new JLabel("Number 1:");
num1Field = new JTextField();
JLabel num2Label = new JLabel("Number 2:");
num2Field = new JTextField();
// Label and text field for sum (output)
JLabel sumLabel = new JLabel("Sum:");
sumField = new JTextField();
sumField.setEditable(false); // Output field should be non-editable
// Add components to the frame
add(num1Label);
add(num1Field);
add(num2Label);
add(num2Field);
add(sumLabel);
add(sumField);
// Add key listeners to the input fields to handle key events
num1Field.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
calculateSum();
}
});
num2Field.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
calculateSum();
}
});
setVisible(true);
}
// Method to calculate the sum of the two input numbers
private void calculateSum() {
try {
// Get values from the input fields
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
// Calculate the sum
int sum = num1 + num2;
// Display the result in the sumField
sumField.setText(String.valueOf(sum));
} catch (NumberFormatException ex) {
// If input is invalid (e.g., empty or non-numeric), clear the sumField
sumField.setText("");
}
}
public static void main(String[] args) {
// Run the program
SwingUtilities.invokeLater(SumCalculator::new);
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team