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
, typecmd
, and press Enter.
2️⃣ Navigate to your project folder: