Practical 22
Practical 22
Conclusion
In this practical, we wrote a program to handle text events on Swing components. We learned
how to capture and respond to text input in components like text fields and text areas.
The JTextArea component in Java Swing is used to accept multiline input from the user.
Key Features of JTextArea:
3. What is the difference between Swing text field and text area?
import javax.swing.*;
import java.awt.event.*;
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(text1.getText());
int num2 = Integer.parseInt(text2.getText());
int sum = num1 + num2;
resultLabel.setText("Result: " + sum);
}
});
frame.add(label1);
frame.add(text1);
frame.add(label2);
frame.add(text2);
frame.add(addButton);
frame.add(resultLabel);
frame.setVisible(true);
}