Java Swing – Login Class
Word-by-Word Code Notes
[Link] | Class 1 of many | Revision Ready ■
These notes explain every keyword, symbol and method used in the [Link] class of the Quiz
Application project. Each section shows the actual code line followed by a word-by-word breakdown so
you can revise quickly.
1 | Package Declaration
package [Link];
package Keyword that organises .java files into named groups (like folders).
quiz The top-level folder / package name chosen for this project.
. Separator — means 'inside'. Reads as: quiz → inside → application.
application Sub-folder / sub-package name inside quiz.
; Semicolon — every Java statement must end with this.
■ Java expects this file to be saved at: quiz/application/[Link]
2 | Import Statements
import [Link].*;
import Keyword — brings classes from another package into this file.
javax Java extension package (extra/advanced tools beyond core java.*).
swing Sub-package providing all GUI (window) components.
.* Wildcard — import ALL classes inside [Link] at once.
; End of statement.
■ swing gives you: JFrame, JButton, JLabel, JTextField, JOptionPane, ImageIcon …
import [Link].*;
java Core Java package namespace.
Java Swing – Login Class Notes | Page 1
awt Abstract Window Toolkit — provides Color, Font, Graphics tools.
.* Import all classes inside [Link].
import [Link].*;
event Sub-package of awt — handles user actions like button clicks and key presses.
.* Imports ActionListener, ActionEvent, and all other event classes.
3 | Class Declaration
public class Login extends JFrame implements ActionListener {
public Access modifier — this class can be used from anywhere in the project.
class Keyword used to define / create a new class.
Login The name of this class (must match the filename [Link]).
extends Inheritance keyword — Login inherits all features of JFrame.
JFrame Built-in Swing class that creates a desktop window.
implements Used to adopt an interface — the class must provide its methods.
ActionListener Interface from [Link] — requires actionPerformed() method.
{ Opening brace — starts the body of the class.
■ extends JFrame → Login IS a window. implements ActionListener → Login can handle button clicks.
4 | Instance Variables (Fields)
private JButton rules, back;
private Access modifier — only code inside Login can see these variables.
JButton Swing class that creates a clickable button.
rules Variable name for the 'Rules' button object.
, Comma — lets you declare multiple variables of the same type in one line.
back Variable name for the 'Back' button object.
; End of statement.
private JTextField tfname;
JTextField Swing class — provides a single-line text input box for the user.
tfname Variable name. 'tf' prefix = text field, 'name' = what it collects.
5 | Constructor
Java Swing – Login Class Notes | Page 2
public Login() {
public The constructor is accessible from outside — needed to call new Login().
Login() Constructor name must match class name exactly. No return type.
{ Starts the constructor body — everything here runs when new Login() is called.
■ Constructors run automatically when you write: new Login();
6 | Background Colour & Layout
getContentPane().setBackground([Link]);
getContentPane() Returns the main drawing area of the JFrame window.
.setBackground() Method — changes the background colour of that area.
[Link] Built-in constant from [Link] representing white.
setLayout(null);
setLayout() Sets how components are arranged inside the window.
No automatic layout manager — you place every component manually with
null
setBounds().
7 | Image Loading (try-catch)
try {
ImageIcon i1 = new
ImageIcon([Link]("icons/[Link]"));
JLabel image = new JLabel(i1);
[Link](0, 0, 600, 500);
add(image);
} catch (Exception e) {
[Link]("Icon not found, continuing with text only.");
}
try { } Block that attempts code which might cause an error (exception).
ImageIcon Swing class used to hold / represent an image file.
i1 Variable name for the ImageIcon object.
new Keyword — creates a new object in memory.
ClassLoader Java class that loads resources (files) bundled with the project.
.getSystemResource() Searches the classpath for the given file path and returns it.
"icons/[Link]" Relative path to the image file inside the project folder.
Java Swing – Login Class Notes | Page 3
JLabel Swing component — can display text OR an image.
image Variable name for the JLabel that will show the picture.
new JLabel(i1) Creates a JLabel and puts the ImageIcon i1 inside it.
.setBounds(0,0,600,500
Sets x=0, y=0 (top-left corner), width=600px, height=500px.
)
add(image) Adds the image label to the JFrame window so it becomes visible.
catch (Exception e) Catches any error that occurred inside try { }. 'e' stores the error.
[Link]() Prints a message to the console/terminal for debugging.
8 | Heading Label
JLabel heading = new JLabel("Simple Minds");
[Link](750, 60, 300, 45);
[Link](new Font("Viner Hand ITC", [Link], 40));
[Link](new Color(30, 144, 254));
add(heading);
JLabel heading Creates a label that displays the text 'Simple Minds'.
.setBounds(750,60,300,
Positions label: x=750, y=60 from top-left; 300px wide, 45px tall.
45)
.setFont() Changes the font style of the label's text.
new Font("Viner Hand
"Viner Hand ITC" = font family | [Link] = bold style | 40 = size in points.
ITC", [Link], 40)
.setForeground() Sets the TEXT colour of the label.
new Color(30, 144, Creates a custom colour using RGB values: Red=30, Green=144, Blue=254 (bright
254) blue).
add(heading) Adds the heading label to the window.
9 | Name Label & Text Field
JLabel name = new JLabel("Enter your name");
[Link](810, 150, 300, 20);
[Link](new Font("Mongolian Baiti", [Link], 18));
[Link](new Color(30, 144, 254));
add(name);
tfname = new JTextField();
[Link](735, 200, 300, 25);
[Link](new Font("Times New Roman", [Link], 20));
Java Swing – Login Class Notes | Page 4
add(tfname);
new JLabel("Enter your
Creates a label with the prompt text.
name")
tfname = new
Creates an empty input box. tfname was declared earlier as a field.
JTextField()
.setFont(new
Font("Times New
Sets the font of text the user types in the box.
Roman", [Link],
20))
10 | Rules & Back Buttons
rules = new JButton("Rules");
[Link](735, 270, 120, 25);
[Link](new Color(30, 144, 254));
[Link]([Link]);
[Link](this);
add(rules);
new JButton("Rules") Creates a button labelled 'Rules'.
.setBackground() Sets the button's background colour.
.setForeground(Color.W
Sets the button's text colour to white.
HITE)
.addActionListener(thi
Registers a listener so clicks are detected. 'this' = current Login object.
s)
add(rules) Adds the button to the window.
■ The Back button uses identical code with variable name 'back' and label 'Back'. Both buttons call addActionListener(this)
so both are handled in actionPerformed().
11 | Window Settings
setSize(1200, 500);
setLocation(200, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(1200, 500) Sets window width=1200px and height=500px.
setLocation(200, 150) Positions the window 200px from left and 150px from top of screen.
setDefaultCloseOperati
When user closes the window, the entire Java program also stops.
on(EXIT_ON_CLOSE)
JFrame.EXIT_ON_CLOSE Constant value (0) that tells JFrame to call [Link]() on close.
Java Swing – Login Class Notes | Page 5
setVisible(true) Makes the window actually appear on screen. Without this, window is hidden.
12 | @Override & actionPerformed Method
@Override
public void actionPerformed(ActionEvent ae) {
Annotation — tells the compiler this method replaces a method from an
@Override
interface/parent.
public Accessible anywhere — required by ActionListener interface.
void This method returns no value.
actionPerformed The exact method name required by ActionListener. Runs when a button is clicked.
ActionEvent Class that stores info about which button was clicked, when, etc.
ae Parameter variable that holds the ActionEvent object for this click.
13 | Rules Button Logic
if ([Link]() == rules) {
String name = [Link]().trim();
if ([Link]()) {
[Link](this, "Error: Name field cannot be empty!",
"Validation Error", JOptionPane.ERROR_MESSAGE);
} else {
setVisible(false);
new Rules(name);
}
}
[Link]() Returns the object (button) that triggered the click event.
== rules Checks if that object IS the rules button variable.
[Link]() Reads whatever the user typed inside the text field.
.trim() Removes leading and trailing spaces from the text.
[Link]() Returns true if the string has no characters (length = 0).
[Link]
Displays a pop-up dialog box with a message.
eDialog()
this Refers to the current Login window (parent for the dialog).
"Validation Error" Title shown in the dialog's title bar.
Java Swing – Login Class Notes | Page 6
JOptionPane.ERROR_MESS
Constant that shows a red error icon on the dialog.
AGE
setVisible(false) Hides the current Login window.
new Rules(name) Creates a new Rules window and passes the player's name to it.
14 | Back Button Logic
} else if ([Link]() == back) {
setVisible(false);
[Link](0);
}
else if Checks this condition only if the earlier 'if' was false.
[Link]() == back Checks whether the Back button was clicked.
setVisible(false) Hides the window.
[Link](0) Completely shuts down the Java Virtual Machine (JVM). Program ends. 0 = no error.
15 | main Method — Program Entry Point
public static void main(String[] args) {
new Login();
}
public Accessible by the JVM from outside — required for main.
static Belongs to the class, not an object — JVM can call it without creating an instance.
void Returns nothing.
main Special reserved name — JVM always looks for this method to start the program.
Array of strings — stores any command-line arguments passed when running the
String[] args
program.
new Login() Creates a Login object → constructor runs → window opens. Program starts here.
■ Quick Revision Summary
Concept One-line Reminder
package Organises files into folders
import Brings classes from other packages
extends JFrame Makes Login a window
implements ActionListener Enables button-click handling
Java Swing – Login Class Notes | Page 7
Constructor Login() Runs when new Login() is called; builds the whole UI
setLayout(null) Manual positioning with setBounds()
setBounds(x,y,w,h) Positions a component at (x,y) with size w×h
addActionListener(this) Connects a button to actionPerformed()
actionPerformed(ae) Runs when any registered button is clicked
[Link]() Tells you which button was clicked
[Link]() Shows a pop-up message/error dialog
[Link](0) Closes the entire program
new Rules(name) Opens the next screen and passes the player name
Java Swing – Login Class Notes | Page 8