Building a GUI Using Swing in Java (For Exams)
When you need to create a Swing GUI for an exam, it's important to focus on the core
components and structure. Here's a concise guide to help you write Swing programs easily
during exams:
Basic Structure of a Swing Program
java
Copy
import javax.swing.*;
import java.awt.*;
public class SimpleSwingApp {
public static void main(String[] args) {
// Create the frame (main window)
JFrame frame = new JFrame("My Swing App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// Create a panel to hold components
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout()); // Simple layout
// Add components to the panel
JLabel label = new JLabel("Enter your name:");
JTextField textField = new JTextField(15);
JButton button = new JButton("Click Me");
panel.add(label);
panel.add(textField);
panel.add(button);
// Add panel to frame
frame.add(panel);
// Make the frame visible
frame.setVisible(true);
Common Swing Components to Remember
1. Containers:
JFrame - Main window
JPanel - Container for other components
2. Basic Components:
JLabel - Displays text
JTextField - Single-line text input
JTextArea - Multi-line text input
JButton - Clickable button
JCheckBox - Checkbox
JRadioButton - Radio button (use with ButtonGroup)
JComboBox - Dropdown list
3. Layout Managers (for organizing components):
FlowLayout - Components flow left to right (default for JPanel)
BorderLayout - North, South, East, West, Center (default for JFrame)
GridLayout - Equal-sized cells in a grid
Event Handling (For Button Clicks)
java
Copy
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = textField.getText();
JOptionPane.showMessageDialog(frame, "Hello, " + name + "!");
});
Common Exam Patterns
1. Simple Form with Input and Button:
Text fields + button that displays input in a dialog
2. Calculator Layout:
GridLayout with buttons for digits and operations
3. Menu System:
JMenuBar with JMenu and JMenuItem
Example: Calculator Layout (Common Exam Question)
java
Copy
import javax.swing.*;
import java.awt.*;
public class Calculator {
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4, 5, 5)); // 5 rows, 4 columns, gaps
// Add components
panel.add(new JTextField());
panel.add(new JButton("C"));
panel.add(new JButton("⌫"));
panel.add(new JButton("/"));
panel.add(new JButton("7"));
panel.add(new JButton("8"));
panel.add(new JButton("9"));
panel.add(new JButton("*"));
panel.add(new JButton("4"));
panel.add(new JButton("5"));
panel.add(new JButton("6"));
panel.add(new JButton("-"));
panel.add(new JButton("1"));
panel.add(new JButton("2"));
panel.add(new JButton("3"));
panel.add(new JButton("+"));
panel.add(new JButton("0"));
panel.add(new JButton("."));
panel.add(new JButton("="));
frame.add(panel);
frame.setVisible(true);
}
Tips for Exams
1. Start with the basic structure (JFrame, JPanel)
2. Add components one by one
3. Use simple layouts (FlowLayout or GridLayout are easiest)
4. For event handling, remember the ActionListener pattern
5. Keep variable names simple (btnSubmit, txtName, etc.)
6. If you forget exact syntax, write comments explaining what you intend to do
Remember that in exams, they usually look for:
Correct component usage
Proper layout management
Basic event handling
Following the problem requirements exactly