Swing GUI
Swing GUI
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:
Copy
import javax.swing.*;
import java.awt.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
panel.add(textField);
panel.add(button);
frame.add(panel);
frame.setVisible(true);
Copy
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
});
Copy
import javax.swing.*;
import java.awt.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
// 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