First Swing Application - potatoscript/JavaSwing GitHub Wiki
๐จ Creating Your First Swing Application (VS Code) ๐ป
๐ Step 1: Prerequisites
๐ 1. Install Java Development Kit (JDK)
-
๐ Check if JDK is Installed:
- Open Command Prompt or Terminal.
- Type:
java -version
- If JDK is installed, it should display something like:
java version "17.0.1" 2023-10-12 LTS
- If JDK is not installed, proceed to the next step.
-
๐ฅ Download and Install JDK:
- Go to Oracle JDK Download Page.
- Select the appropriate version for your operating system.
- Follow the installation steps.
๐งโ๐ป 2. Install Visual Studio Code (VS Code)
-
๐ฅ Download VS Code:
- Go to Visual Studio Code Download Page.
- Download the appropriate version for your OS.
-
๐๏ธ Install VS Code:
- Follow the installation instructions.
- Launch VS Code after installation.
๐ฅ 3. Install Java Extension Pack in VS Code
- Open VS Code.
- Click on the Extensions icon (๐ฆ) on the left sidebar.
- Search for
Java Extension Pack
. - Click Install to install the following essential extensions:
- โ Java Extension Pack
- โ Debugger for Java
- โ Java Test Runner
- โ Maven for Java
- โ Java Language Support
- Wait for the installation to complete.
๐ Step 2: Setting Up Your Java Project in VS Code
๐ 1. Create a New Java Project
- Open VS Code.
- Press
Ctrl + Shift + P
to open the Command Palette. - Type:
Java: Create Java Project
- Select Java: Create Java Project.
- Choose No Build Tools (for beginners).
- Select a folder where you want to create your project.
- Name the project
MySwingApp
. - Click Create.
๐ 2. Create a Java Class
- Open your project folder in VS Code.
- Right-click on the
src
folder. - Select New File and name it:
HelloSwing.java
- Open
HelloSwing.java
and add the following code:
import javax.swing.*;
public class HelloSwing {
public static void main(String[] args) {
// Set Look and Feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
// Create JFrame window
JFrame frame = new JFrame("Hello Swing!");
// Create JLabel with text
JLabel label = new JLabel("๐ Hello, Swing World!", SwingConstants.CENTER);
// Add label to the frame
frame.add(label);
// Set frame size and default close operation
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make the frame visible
frame.setVisible(true);
}
}
โถ๏ธ Step 3: Run Your First Swing Application
๐ 1. Open the Terminal in VS Code
- Press
Ctrl +
` (or go to Terminal โ New Terminal). - Make sure the terminal is in the project directory.
cd /path/to/MySwingApp
๐ฆ 2. Compile the Java File
- Run the following command:
javac HelloSwing.java
- If no errors appear, it means the code was successfully compiled. A
HelloSwing.class
file will be generated.
โถ๏ธ 3. Run the Application
- Run the application using:
java HelloSwing
- A window will pop up with the text:
+----------------------------------+
| |
| ๐ Hello, Swing World! |
| |
+----------------------------------+
๐จ Step 4: Understanding Basic Swing Components
๐ Explanation of the Code
โ JFrame:
JFrame
creates the main window of your application.setSize(400, 200)
sets the size of the window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
closes the application when the window is closed.
โ JLabel:
JLabel
displays text and images.SwingConstants.CENTER
centers the label content horizontally.
โ setLookAndFeel:
- This changes the appearance of the application to match the systemโs look.
โ setVisible(true):
- This makes the frame visible on the screen.
๐ Common Swing Components Overview
Component | Description | Example Code |
---|---|---|
JFrame |
Main window | new JFrame("Title") |
JLabel |
Display text/icons | new JLabel("Hello") |
JButton |
Button for actions | new JButton("Click Me") |
JTextField |
Single-line text input | new JTextField(20) |
JTextArea |
Multi-line text input | new JTextArea(5, 20) |
JCheckBox |
Checkbox for options | new JCheckBox("Accept") |
JComboBox |
Dropdown list for selection | new JComboBox(options) |
JList |
List of selectable items | new JList(items) |
JPanel |
Container for grouping items | new JPanel() |
JMenuBar |
Menu bar for the application | new JMenuBar() |
๐ฎ Step 5: Adding a Button and Handling Events
๐ฅ Example: Button with Click Event
- Open
HelloSwing.java
and replace the code with:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class HelloSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("Button Click Example");
// Create a JButton
JButton button = new JButton("Click Me! ๐");
// Add ActionListener to the button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "๐ฏ Button Clicked!");
}
});
// Add button to frame
frame.add(button);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
โถ๏ธ 2. Compile and Run Again
- Compile:
javac HelloSwing.java
- Run:
java HelloSwing
๐ฉโ๐จ Output:
+----------------------------+
| |
| ๐ Click Me! |
| |
+----------------------------+
Clicking the button shows:
๐ฏ Button Clicked!
๐ Step 6: Create a JAR File and Run
๐ฆ 1. Compile and Create JAR File
- Compile the project:
javac HelloSwing.java
- Create a JAR file:
jar cfe HelloSwing.jar HelloSwing *.class
โถ๏ธ 2. Run the JAR File
java -jar HelloSwing.jar
๐ Your Swing application will open in a window!
๐จ Bonus: Customize Look and Feel
๐จ 1. Set Native Look and Feel
Add the following code before creating Swing components:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
๐จ 2. Change Look and Feel Options
Popular Look and Feel Options:
javax.swing.plaf.metal.MetalLookAndFeel
com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
com.sun.java.swing.plaf.motif.MotifLookAndFeel
UIManager.getSystemLookAndFeelClassName()
(Default system look)