Java Swing GUI — Quick Notes for
Open-Notes Exam
⚙️ SWING BASICS
JFrame: The main application window.
java
CopyEdit
JFrame frame = new JFrame("Title");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](800, 600);
[Link](true);
●
● JPanel: A container used to group components.
○ Override paintComponent(Graphics g) to draw custom graphics or
background.
JLabel: Displays text or images.
java
CopyEdit
JLabel label = new JLabel("Hello!");
●
JButton: A clickable button.
java
CopyEdit
JButton button = new JButton("Attack");
[Link](e -> doSomething());
●
● JTextArea / JTextField:
○ JTextArea: Multi-line, good for logs.
○ JTextField: Single-line, for input.
ImageIcon: Used to load images for backgrounds, icons, or sprites.
java
CopyEdit
ImageIcon icon = new
ImageIcon(getClass().getResource("/images/[Link]"));
●
● Layouts:
○ BorderLayout: NORTH, SOUTH, CENTER, EAST, WEST.
○ BoxLayout: Horizontal or vertical stacking.
○ FlowLayout: Default layout, left-to-right.
🎮 GAME-SPECIFIC SWING CONCEPTS
📺 Screens
● Use different JPanels for game screens:
○ StartScreenGUI
○ SetupScreenGUI
○ BattleScreenGUI
● Use [Link](panel) to switch between screens.
🎨 Custom Backgrounds
Use a custom JPanel to paint a scaled image:
java
CopyEdit
class BackgroundPanel extends JPanel {
private Image bg;
public BackgroundPanel(String path) {
bg = new ImageIcon(getClass().getResource(path)).getImage();
}
protected void paintComponent(Graphics g) {
[Link](g);
// Scale and draw bg
}
}
●
🕹️ Buttons & Actions
Buttons trigger turn logic:
java
CopyEdit
[Link](e -> handlePlayerTurn(1));
[Link](e -> handlePlayerTurn(2));
[Link](e -> handlePlayerTurn(3));
●
● Game state (like health, charging, etc.) updates through executeTurn().
🔁 GAME LOOP LOGIC (IN GUI)
● Player clicks an action → triggers handlePlayerTurn(int action)
● executeTurn(playerAction, opponentAction, isPlayer)
● Opponent AI uses [Link]() to decide move.
● Output is shown using a JTextArea log.
🔤 DISPLAY ELEMENTS
● Health bars can be simple JLabels, optionally with progress bar visuals.
● Use setText() to update player/opponent info dynamically.
🧠 GUI HELPER TIPS
● Use revalidate() and repaint() after dynamic GUI updates.
● Organize logic to separate GUI code from core game logic when possible.
● Group related components (e.g., action buttons in a JPanel with FlowLayout).
✅ COMPONENT QUICK LIST
Componen Purpose Notes
t
JFrame Main game window Only one per app
JPanel Screen container Can override
paintComponent
JButton User input (attack/defend) Attach ActionListener
JLabel Display text/images setText() to update
JTextArea Battle log output Use .append() for new lines
ImageIcon Background or sprites Load with getResource()
💬 MISC TIPS
Always run GUI on the Event Dispatch Thread (EDT):
java
CopyEdit
[Link](() -> new GameWindow());
●
Set fonts and styles for consistency:
java
CopyEdit
[Link](new Font("Arial", [Link], 16));
●
Disable buttons when needed (e.g., during opponent's turn):
java
CopyEdit
[Link](false);
●
● Use padding and layout properly to prevent cluttered UI.
🧪 EXAMPLE: Creating a Button Panel
java
CopyEdit
JPanel buttonPanel = new JPanel(new FlowLayout());
[Link](attackButton);
[Link](defendButton);
[Link](chargeButton);
[Link](buttonPanel, [Link]);
🚨 COMMON EXAM GOTCHAS
● Forgetting to call repaint() or revalidate() after updating UI
● Not using .getResource() properly for image paths (must be in resources)
● Using absolute paths or invalid layouts (e.g., nested panels without layouts)
● Blocking the Event Dispatch Thread with long-running code