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
8 changes: 8 additions & 0 deletions part-1/.idea/.gitignore

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

6 changes: 6 additions & 0 deletions part-1/.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 part-1/.idea/modules.xml

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

11 changes: 11 additions & 0 deletions part-1/.idea/part-1.iml

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

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

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

23 changes: 23 additions & 0 deletions part-1/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import javax.swing.*;
import java.awt.*;

public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Click Counter");
JLabel label = new JLabel("Clicks: 0");
label.setHorizontalAlignment(SwingConstants.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 100);
frame.setLayout(new BorderLayout());
frame.setResizable(false);
JButton button = new JButton("Click Me");
frame.add(button, BorderLayout.SOUTH);
frame.add(label, BorderLayout.CENTER);
frame.setVisible(true);
final int[] count = {0};
button.addActionListener( e-> {
count[0]++;
label.setText("clicks: " + count[0]);
});
}
}
8 changes: 8 additions & 0 deletions part-1/out/production/part-1/.idea/.gitignore

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

6 changes: 6 additions & 0 deletions part-1/out/production/part-1/.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 part-1/out/production/part-1/.idea/modules.xml

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

11 changes: 11 additions & 0 deletions part-1/out/production/part-1/.idea/part-1.iml

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

6 changes: 6 additions & 0 deletions part-1/out/production/part-1/.idea/vcs.xml

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

Binary file added part-1/out/production/part-1/Main.class
Binary file not shown.
Binary file added part-1/out/production/part-1/images/img-1-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added part-1/out/production/part-1/images/img-1-2.png
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 part-1/out/production/part-1/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"/>
6 changes: 6 additions & 0 deletions part-2/.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 part-2/.idea/modules.xml

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

11 changes: 11 additions & 0 deletions part-2/.idea/part-2.iml

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

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

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

91 changes: 91 additions & 0 deletions part-2/.idea/workspace.xml

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

38 changes: 38 additions & 0 deletions part-2/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
public static void main(String[] args) {

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setLayout(null);


JLabel label = new JLabel("Enter your name and press greet");
label.setBounds(100, 100, 200, 50);
frame.add(label);

JLabel label2 = new JLabel("Name :");
label2.setBounds(50, 50, 100, 50);
frame.add(label2);

JTextField textField = new JTextField();
textField.setBounds(100, 50, 100, 50);
textField.setOpaque(true);
frame.add(textField);

JButton button = new JButton("Greet");
button.setBounds(200, 50, 80, 40);
frame.add(button);
frame.setVisible(true);
button.addActionListener(e -> {
String name = textField.getText();
label.setText("Hello "+name);
});


}
}
6 changes: 6 additions & 0 deletions part-3/.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 part-3/.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 part-3/.idea/vcs.xml

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

Loading