Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/JavaSwingSimpleExercise.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions out/production/JavaSwingSimpleExercise/E1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import javax.swing.*; // For Swing components like JFrame, JButton, JLabel, etc.
import java.awt.*; // For layouts, colors, fonts, etc.
import java.awt.event.*; // For handling events like button clicks

public class E1 extends JFrame implements ActionListener {
private JLabel counterLabel; // Displays the click count
private JButton clickButton; // Button to register clicks
private int count = 0; // Stores the current count

public E1() {
setTitle("Counter App");
counterLabel = new JLabel("Click: 0", SwingConstants.CENTER);
clickButton = new JButton("Click Me");
clickButton.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setSize(300, 200);
add(counterLabel, BorderLayout.CENTER);
add(clickButton, BorderLayout.SOUTH);
setVisible(true);
}

@Override
public void actionPerformed(ActionEvent actionEvent) {
count++;
counterLabel.setText("Click :" + count);
}

public static void main (String[] args){
SwingUtilities.invokeLater(E1::new);
}
}
47 changes: 47 additions & 0 deletions out/production/JavaSwingSimpleExercise/E2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import javax.swing.*; // For Swing components like JFrame, JButton, JLabel, etc.
import java.awt.*; // For layouts, colors, fonts, etc.
import java.awt.event.*; // For handling events like button clicks

class E2 extends JFrame implements ActionListener {
private JTextField textInput;
private JButton button;
private JLabel resultLabel;
private JLabel inputLable;

public E2() {
JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
button = new JButton("Submit");
resultLabel = new JLabel("Please Login", SwingConstants.CENTER);
inputLable = new JLabel("Name");
textInput = new JTextField(20);

button.addActionListener(this);

setSize(400, 200);
setLocationRelativeTo(null);
setLayout(new BorderLayout());

inputPanel.add(inputLable);
inputPanel.add(textInput);
inputPanel.add(button);

add(inputPanel, BorderLayout.NORTH);
add(resultLabel, BorderLayout.CENTER);

setVisible(true);
}

@Override
public void actionPerformed(ActionEvent actionEvent) {
String name = textInput.getText().trim();
if (!name.isEmpty()) {
resultLabel.setText("Hello, " + name + "!");
} else {
resultLabel.setText("Please enter your name first.");
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(E2::new);
}
}
53 changes: 53 additions & 0 deletions out/production/JavaSwingSimpleExercise/E3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class E3 extends JFrame implements ActionListener {
private JButton redBtn;
private JButton blueBtn;
private JButton greenBtn;
private JPanel panel;

public E3() {
redBtn = new JButton("Change to red");
blueBtn = new JButton("Change to blue");
greenBtn = new JButton("Change to green");

redBtn.setActionCommand("SetToRed");
blueBtn.setActionCommand("SetToBlue");
greenBtn.setActionCommand("SetToGreen");

redBtn.addActionListener(this);
blueBtn.addActionListener(this);
greenBtn.addActionListener(this);

setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel buttonsPanel = new JPanel();
buttonsPanel.add(redBtn);
buttonsPanel.add(blueBtn);
buttonsPanel.add(greenBtn);

panel = new JPanel(); // <-- Initialize the panel!

setLayout(new BorderLayout());
add(buttonsPanel, BorderLayout.NORTH); // use BorderLayout constants
add(panel, BorderLayout.CENTER);

setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "SetToRed": panel.setBackground(Color.RED); break;
case "SetToGreen": panel.setBackground(Color.GREEN); break;
case "SetToBlue": panel.setBackground(Color.BLUE); break;
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(E3::new);
}
}
Binary file added out/production/JavaSwingSimpleExercise/E4.class
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions out/production/JavaSwingSimpleExercise/readme-part-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
## Exercise 1: Click Counter Application

### Description & Goal

Create a simple GUI application that tracks how many times a user clicks a button and displays the count in real time. This exercise reinforces the basics of Swing components and event handling.

### Code Breakdown

**Part 1: Field Declarations**
Define the UI components and a counter variable.

```java
public class ClickCounterApp extends JFrame implements ActionListener {
private JLabel counterLabel; // Displays the click count
private JButton clickButton; // Button to register clicks
private int count = 0; // Stores the current count
}
```

*Explanation:* We declare a `JLabel` for showing the count, a `JButton` for clicks, and an integer `count` initialized to 0.

**Part 2: Constructor Setup**
Initialize and arrange components in the window.

```java
public ClickCounterApp() {
setTitle("Click Counter");
counterLabel = new JLabel("Clicks: 0", SwingConstants.CENTER);
clickButton = new JButton("Click Me");
clickButton.addActionListener(this);

setLayout(new BorderLayout());
add(counterLabel, BorderLayout.CENTER);
add(clickButton, BorderLayout.SOUTH);
}
```

*Explanation:* In the constructor we set the title, create the label and button, attach an `ActionListener` to the button, choose a `BorderLayout`, and place the label at center and button at bottom.

**Part 3: Event Handling**
Update count and label text when the button is clicked.

```java
@Override
public void actionPerformed(ActionEvent e) {
count++; // Increment the click counter
counterLabel.setText("Clicks: " + count);
}
```

*Explanation:* The `actionPerformed` method is called on each click. We increase `count` by one and refresh the label text to reflect the new value.

**Part 4: Application Entry Point**
Launch the GUI on the Event Dispatch Thread.

```java
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ClickCounterApp().setVisible(true));
}
```

*Explanation:* Using `SwingUtilities.invokeLater` ensures thread safety by creating and showing the GUI on the Swing event thread.
---
### output of the code:

<img src="./images/img-1-1.png" alt="..." width="400"/>
<img src="./images/img-1-2.png" alt="..." width="400"/>
78 changes: 78 additions & 0 deletions out/production/JavaSwingSimpleExercise/readme-part-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
## Exercise 2: Greeting Form Application

### Description & Goal

Create a GUI form where the user enters their name into a text field and clicks a button to display a personalized greeting. This exercise practices input handling, text fields, and dynamic label updates.

### Code Breakdown

**Part 1: Field Declarations**
Define the input, button, and label components.

```java
public class GreetingFormApp extends JFrame implements ActionListener {
private JTextField nameField; // Field for user input
private JButton greetButton; // Button to trigger greeting
private JLabel messageLabel; // Label to display greeting
}
```

*Explanation:* We declare a `JTextField` for entering the name, a `JButton` for submitting, and a `JLabel` for showing messages.

**Part 2: Constructor Setup**
Initialize components and arrange them using a simple layout.

```java
public GreetingFormApp() {
setTitle("Greeting Form");
nameField = new JTextField(15);
greetButton = new JButton("Greet");
messageLabel = new JLabel("Enter your name and press Greet.", SwingConstants.CENTER);

greetButton.addActionListener(this);

JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("Name:"));
inputPanel.add(nameField);
inputPanel.add(greetButton);

setLayout(new BorderLayout());
add(inputPanel, BorderLayout.NORTH);
add(messageLabel, BorderLayout.CENTER);
}
```

*Explanation:* In the constructor, we set the title, instantiate the text field, button, and label, attach the listener, group the input components in a `JPanel`, and use `BorderLayout` to place the input panel at the top and the message label in the center.

**Part 3: Event Handling**
Read the text field and update the label when the button is clicked.

```java
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText().trim();
if (name.isEmpty()) {
messageLabel.setText("Please enter a name.");
} else {
messageLabel.setText("Hello, " + name + "!");
}
}
```

*Explanation:* On button click, we get the trimmed input. If empty, prompt the user; otherwise, display "Hello, {name}!".

**Part 4: Application Entry Point**
Start the form on the Event Dispatch Thread.

```java
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GreetingFormApp().setVisible(true));
}
```

*Explanation:* We use `invokeLater` to ensure the GUI is created and manipulated on the Event Dispatch Thread, then display the frame.
---
### output of the code:
<img src="./images/img-2-1.png" alt="..." width="400"/>
<img src="./images/img-2-2.png" alt="..." width="400"/>
<img src="./images/img-2-3.png" alt="..." width="400"/>
Loading