|
1 | 1 | package com.iluwatar.bloc;
|
2 |
| -import javax.swing.*; |
3 |
| -import java.awt.*; |
| 2 | +import java.awt.BorderLayout; |
| 3 | +import java.awt.Font; |
| 4 | +import javax.swing.JButton; |
| 5 | +import javax.swing.JFrame; |
| 6 | +import javax.swing.JLabel; |
| 7 | +import javax.swing.SwingConstants; |
| 8 | +import javax.swing.WindowConstants; |
4 | 9 |
|
5 |
| -public class Main |
6 |
| -{ |
7 |
| - public static void main(String[] args) |
8 |
| - { |
9 |
| - Bloc bloC = new Bloc(); |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * The Main class demonstrates the use of the Bloc pattern in a simple GUI application. |
| 15 | + * It creates a JFrame with buttons to increment, decrement, and toggle a listener |
| 16 | + * that updates the counter value on the screen. |
| 17 | + */ |
| 18 | +public class Main { |
| 19 | + |
| 20 | + /** |
| 21 | + * The entry point of the application. Initializes the GUI and sets up actions |
| 22 | + * for the buttons and listener management. |
| 23 | + * |
| 24 | + * @param args command-line arguments (not used in this example) |
| 25 | + */ |
| 26 | + public static void main(String[] args) { |
| 27 | + // Create a Bloc instance to manage the state |
| 28 | + |
| 29 | + |
| 30 | + // Create and set up the JFrame |
10 | 31 | JFrame frame = new JFrame("BloC example");
|
11 | 32 | frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
12 | 33 | frame.setSize(400, 300);
|
| 34 | + |
| 35 | + // Create a label to display the counter value |
13 | 36 | JLabel counterLabel = new JLabel("Counter: 0", SwingConstants.CENTER);
|
14 | 37 | counterLabel.setFont(new Font("Arial", Font.BOLD, 20));
|
| 38 | + |
| 39 | + // Create buttons for increment, decrement, and toggling listener |
15 | 40 | JButton incrementButton = new JButton("Increment");
|
16 | 41 | JButton decrementButton = new JButton("Decrement");
|
17 | 42 | JButton toggleListenerButton = new JButton("Disable Listener");
|
| 43 | + |
| 44 | + // Set layout and add components to the frame |
18 | 45 | frame.setLayout(new BorderLayout());
|
19 | 46 | frame.add(counterLabel, BorderLayout.CENTER);
|
20 | 47 | frame.add(incrementButton, BorderLayout.NORTH);
|
21 | 48 | frame.add(decrementButton, BorderLayout.SOUTH);
|
22 | 49 | frame.add(toggleListenerButton, BorderLayout.EAST);
|
23 |
| - |
| 50 | + Bloc bloC = new Bloc(); |
| 51 | + // Create a state listener to update the counter label when the state changes |
24 | 52 | StateListener<State> stateListener = state -> counterLabel.setText("Counter: " + state.getValue());
|
25 | 53 |
|
| 54 | + // Add the listener to the Bloc instance |
26 | 55 | bloC.addListener(stateListener);
|
27 | 56 |
|
28 |
| - toggleListenerButton.addActionListener(e -> |
29 |
| - { |
30 |
| - if (bloC.getListeners().contains(stateListener)) |
31 |
| - { |
| 57 | + // Set action listeners for buttons |
| 58 | + toggleListenerButton.addActionListener(e -> { |
| 59 | + if (bloC.getListeners().contains(stateListener)) { |
32 | 60 | bloC.removeListener(stateListener);
|
33 | 61 | toggleListenerButton.setText("Enable Listener");
|
34 |
| - } else |
35 |
| - { |
| 62 | + } else { |
36 | 63 | bloC.addListener(stateListener);
|
37 | 64 | toggleListenerButton.setText("Disable Listener");
|
38 | 65 | }
|
39 |
| - } |
40 |
| - ); |
| 66 | + }); |
| 67 | + |
41 | 68 | incrementButton.addActionListener(e -> bloC.increment());
|
42 | 69 | decrementButton.addActionListener(e -> bloC.decrement());
|
| 70 | + |
| 71 | + // Make the frame visible |
43 | 72 | frame.setVisible(true);
|
44 | 73 | }
|
45 | 74 | }
|
0 commit comments