Introduction To Swing
Introduction To Swing
Used to accept input from the user in the form of a single line of
text.
Can be set to editable or non-editable.
Common Methods:
o getText(): Retrieves the text.
o setText(String text): Sets the text.
Code Example:
import javax.swing.*;
frame.add(textField);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
Real-Life Example:
2. Buttons (JButton)
Code Example:
import javax.swing.*;
import java.awt.event.*;
button.addActionListener(e ->
JOptionPane.showMessageDialog(frame, "Button Clicked!"));
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
Real-Life Example:
import javax.swing.*;
toggleButton.addItemListener(e -> {
});
frame.add(toggleButton);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
Real-Life Example:
4. Checkboxes (JCheckBox)
import javax.swing.*;
frame.add(checkBox1);
frame.add(checkBox2);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
Real-Life Example:
import javax.swing.*;
bg.add(r1);
bg.add(r2);
frame.add(r1);
frame.add(r2);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
Real-Life Example: