Wednesday, April 30, 2025

Creating Your First Java Swing Application Using Notepad

 Welcome back to My Java Journey! Today, we’re going old-school — no fancy IDEs, just Notepad, Command Prompt, and a simple Java Swing app you’ll code by hand.

We’re going to create a tiny desktop app that:
✅ Shows a label
✅ Has a textbox for you to enter your name
✅ Has a button that, when clicked, updates the label to say “Hello [your name]”

Let’s get started!


πŸ—‚️ Step 1: Prepare Your Project Folder

1️⃣ Open File Explorer and create a new folder at:

C:\hello

his is where you’ll save your .java file and compile the program.


πŸ“ Step 2: Write the Java Code

1️⃣ Open Notepad.

2️⃣ Copy and paste this Java code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import javax.swing.*;
import java.awt.event.*;

public class HelloSwing {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Hello Swing");
        JLabel label = new JLabel("Enter your name:");
        JTextField textField = new JTextField(15);
        JButton button = new JButton("Submit");

        label.setBounds(50, 50, 300, 20);
        textField.setBounds(50, 80, 150, 20);
        button.setBounds(50, 110, 100, 30);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name = textField.getText();
                label.setText("Hello " + name + "!");
            }
        });

        frame.add(label);
        frame.add(textField);
        frame.add(button);

        frame.setSize(400, 250);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

3️⃣ Save the file as:

C:\hello\HelloSwing.java

⚙️ Step 3: Compile the Code

1️⃣ Open Command Prompt.

  • Press Win + R, type cmd, and press Enter.

2️⃣ Navigate to your project folder:

cd C:\hello
3️⃣ Compile the Java file:
javac HelloSwing.java

✅ If successful, you’ll see no errors, and a HelloSwing.class file will appear in the folder.

▶️ Step 4: Run the Application

In the same Command Prompt window, type:

java HelloSwing

✅ A window should pop up with:

  • A label saying Enter your name:

  • A textbox

  • A button labeled Submit

When you type your name and press the button, the label updates to say:


Hello [your name]!

πŸ” Quick Recap

✔️ Create project folder C:\hello
✔️ Write Java Swing code in Notepad
✔️ Compile using javac
✔️ Run using java

You’ve just built your first Java Swing application without an IDE — and that’s a great hands-on way to understand what’s happening under the hood.

No comments:

Post a Comment

Deploying a React Frontend to Railway: A Complete Guide (To-Do App)

 If you've already deployed your Java Spring Boot backend to Railway, congratulations — the hardest part is done! πŸŽ‰ Now it’s time to ...