Skip to content

Commit de15449

Browse files
Update b12_ButtonExample.java
1 parent 7955e2e commit de15449

1 file changed

Lines changed: 296 additions & 0 deletions

File tree

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,297 @@
1+
package a7_java.awt;
12

3+
import java.awt.*;
4+
import java.awt.event.*;
5+
import javax.accessibility.AccessibleContext;
6+
7+
/* Comprehensive demonstration of all Button class features
8+
* Features demonstrated:
9+
* - All constructors
10+
* - Event management methods
11+
* - State and property methods
12+
* - Event processing methods
13+
* - Accessibility features */
14+
public class b12_ButtonExample {
15+
16+
private Frame mainFrame;
17+
private Panel buttonPanel, statusPanel, controlPanel;
18+
private Label statusLabel, eventLabel;
19+
private TextArea eventLog;
20+
private int clickCount = 0;
21+
22+
/* Main constructor initializing the demonstration */
23+
public b12_ButtonExample() {
24+
prepareGUI();
25+
demonstrateButtonFeatures();
26+
}
27+
28+
public static void main(String[] args) {
29+
System.out.println("Starting Button Demonstration Application...");
30+
EventQueue.invokeLater(() -> {
31+
b12_ButtonExample demo = new b12_ButtonExample();
32+
demo.showFrame();
33+
});
34+
}
35+
36+
/* Prepares the main GUI components with a professional layout */
37+
private void prepareGUI() {
38+
// Main Frame Setup
39+
mainFrame = new Frame("Advanced Button Class Demonstration");
40+
mainFrame.setSize(1024, 768);
41+
mainFrame.setLayout(new BorderLayout(10, 10));
42+
mainFrame.setBackground(new Color(240, 240, 240));
43+
44+
// Window Listener
45+
mainFrame.addWindowListener(new WindowAdapter() {
46+
@Override
47+
public void windowClosing(WindowEvent we) {
48+
System.out.println("Application Terminated");
49+
System.exit(0);
50+
}
51+
});
52+
53+
// Header Label
54+
Label headerLabel = new Label("Button Class Features & Methods Demonstration", Label.CENTER);
55+
headerLabel.setFont(new Font("Arial", Font.BOLD, 20));
56+
mainFrame.add(headerLabel, BorderLayout.NORTH);
57+
58+
// Button Panel
59+
buttonPanel = new Panel(new GridLayout(0, 2, 10, 10));
60+
buttonPanel.setBackground(new Color(250, 250, 250));
61+
62+
// Status Panel
63+
statusPanel = new Panel(new BorderLayout());
64+
statusLabel = new Label("Status: Ready", Label.LEFT);
65+
eventLabel = new Label("Event Log:", Label.LEFT);
66+
eventLog = new TextArea(10, 50);
67+
eventLog.setEditable(false);
68+
69+
statusPanel.add(statusLabel, BorderLayout.NORTH);
70+
statusPanel.add(eventLabel, BorderLayout.CENTER);
71+
statusPanel.add(eventLog, BorderLayout.SOUTH);
72+
73+
// Control Panel
74+
controlPanel = new Panel(new BorderLayout());
75+
controlPanel.add(buttonPanel, BorderLayout.CENTER);
76+
controlPanel.add(statusPanel, BorderLayout.SOUTH);
77+
78+
// Add to Main Frame
79+
mainFrame.add(controlPanel, BorderLayout.CENTER);
80+
}
81+
82+
/* Logs events to the event log text area */
83+
private void logEvent(String event) {
84+
eventLog.append(event + "\n");
85+
System.out.println(event);
86+
}
87+
88+
/* Creates a custom button with specific properties */
89+
private Button createCustomButton(String label, Color bgColor) {
90+
Button button = new Button(label);
91+
button.setBackground(bgColor);
92+
button.setForeground(Color.BLACK);
93+
return button;
94+
}
95+
96+
/* Comprehensive demonstration of all Button class features */
97+
private void demonstrateButtonFeatures() {
98+
logEvent("Initializing Button demonstrations...\n");
99+
100+
// Constructor demonstrations
101+
demonstrateConstructors();
102+
103+
// Event management demonstrations
104+
demonstrateEventManagement();
105+
106+
// State and property demonstrations
107+
demonstrateStateAndProperties();
108+
109+
// Event processing demonstrations
110+
demonstrateEventProcessing();
111+
112+
// Accessibility demonstrations
113+
demonstrateAccessibility();
114+
115+
logEvent("\nAll button features have been initialized.");
116+
}
117+
118+
/* Demonstrates Button constructors
119+
* Methods demonstrated: Constructor 1 and 2 */
120+
private void demonstrateConstructors() {
121+
logEvent("CONSTRUCTOR DEMONSTRATION");
122+
123+
// Constructor 1: Button()
124+
Button emptyButton = new Button();
125+
emptyButton.setLabel("Empty Constructor");
126+
buttonPanel.add(emptyButton);
127+
logEvent("1. Created button using empty constructor");
128+
129+
// Constructor 2: Button(String label)
130+
Button labelButton = new Button("Labeled Constructor");
131+
buttonPanel.add(labelButton);
132+
logEvent("2. Created button using label constructor");
133+
134+
// Add basic action listeners
135+
emptyButton.addActionListener(e ->
136+
logEvent("Empty Constructor Button clicked"));
137+
138+
labelButton.addActionListener(e ->
139+
logEvent("Labeled Constructor Button clicked"));
140+
}
141+
142+
/* Demonstrates event management methods
143+
* Methods demonstrated: 1-5 (Event Management Methods) */
144+
private void demonstrateEventManagement() {
145+
logEvent("\nEVENT MANAGEMENT DEMONSTRATION");
146+
147+
// Create button for event management demo
148+
Button eventButton = createCustomButton("Event Management", new Color(200, 230, 255));
149+
buttonPanel.add(eventButton);
150+
151+
// Method 1: addActionListener
152+
ActionListener listener1 = e -> logEvent("Listener 1: Button clicked");
153+
ActionListener listener2 = e -> logEvent("Listener 2: Button clicked");
154+
eventButton.addActionListener(listener1);
155+
eventButton.addActionListener(listener2);
156+
logEvent("3. Added multiple action listeners");
157+
158+
// Method 4 & 5: setActionCommand & getActionCommand
159+
eventButton.setActionCommand("EVENT_BUTTON_COMMAND");
160+
logEvent("4. Set action command: " + eventButton.getActionCommand());
161+
162+
// Method 2: removeActionListener
163+
Button removeListenerButton = createCustomButton("Remove Listener", new Color(255, 220, 220));
164+
buttonPanel.add(removeListenerButton);
165+
removeListenerButton.addActionListener(e -> {
166+
eventButton.removeActionListener(listener2);
167+
logEvent("5. Removed listener2 from Event Management button");
168+
});
169+
170+
// Method 3: getActionListeners
171+
Button checkListenersButton = createCustomButton("Check Listeners", new Color(220, 255, 220));
172+
buttonPanel.add(checkListenersButton);
173+
checkListenersButton.addActionListener(e -> {
174+
ActionListener[] listeners = eventButton.getActionListeners();
175+
logEvent("Current listeners count: " + listeners.length);
176+
});
177+
}
178+
179+
/* Demonstrates state and property methods
180+
* Methods demonstrated: 6-9 (State and Property Methods) */
181+
private void demonstrateStateAndProperties() {
182+
logEvent("\nSTATE AND PROPERTY DEMONSTRATION");
183+
184+
// Create button for state/property demo
185+
Button propertyButton = createCustomButton("Properties", new Color(255, 240, 200));
186+
buttonPanel.add(propertyButton);
187+
188+
// Methods 6 & 7: getLabel & setLabel
189+
propertyButton.addActionListener(e -> {
190+
String currentLabel = propertyButton.getLabel();
191+
propertyButton.setLabel("Label Changed: " + clickCount++);
192+
logEvent("6-7. Label changed from '" + currentLabel +
193+
"' to '" + propertyButton.getLabel() + "'");
194+
});
195+
196+
// Method 8: getAccessibleContext
197+
Button accessibilityButton = createCustomButton("Get Accessibility", new Color(240, 200, 255));
198+
buttonPanel.add(accessibilityButton);
199+
accessibilityButton.addActionListener(e -> {
200+
AccessibleContext ac = accessibilityButton.getAccessibleContext();
201+
logEvent("8. AccessibleContext: " + ac.toString());
202+
});
203+
204+
// Method 9: Create custom button to demonstrate state information
205+
class StateButton extends Button {
206+
private static final long serialVersionUID = 1L;
207+
208+
public StateButton(String label) {
209+
super(label);
210+
}
211+
212+
// Make state information publicly accessible
213+
public String getStateInfo() {
214+
return paramString();
215+
}
216+
}
217+
218+
StateButton stateButton = new StateButton("State Info");
219+
stateButton.setBackground(new Color(220, 220, 255));
220+
buttonPanel.add(stateButton);
221+
stateButton.addActionListener(e -> {
222+
StateButton btn = (StateButton) e.getSource();
223+
logEvent("9. Button state: " + btn.getStateInfo());
224+
});
225+
}
226+
227+
/* Demonstrates event processing methods
228+
* Methods demonstrated: 10-12 (Event Processing Methods) */
229+
private void demonstrateEventProcessing() {
230+
logEvent("\nEVENT PROCESSING DEMONSTRATION");
231+
232+
// Create custom button extending Button to override protected methods
233+
class CustomButton extends Button {
234+
private static final long serialVersionUID = 1L;
235+
236+
public CustomButton(String label) {
237+
super(label);
238+
}
239+
240+
// Method 10: processActionEvent
241+
@Override
242+
protected void processActionEvent(ActionEvent e) {
243+
logEvent("10. Processing action event: " + e.toString());
244+
super.processActionEvent(e);
245+
}
246+
247+
// Method 11: processEvent
248+
@Override
249+
protected void processEvent(AWTEvent e) {
250+
logEvent("11. Processing AWT event: " + e.toString());
251+
super.processEvent(e);
252+
}
253+
}
254+
255+
CustomButton customButton = new CustomButton("Process Events");
256+
customButton.setBackground(new Color(200, 255, 200));
257+
buttonPanel.add(customButton);
258+
259+
// Method 12: addNotify
260+
customButton.addActionListener(e -> {
261+
customButton.addNotify();
262+
logEvent("12. Added native peer via addNotify()");
263+
});
264+
}
265+
266+
/* Demonstrates accessibility features */
267+
private void demonstrateAccessibility() {
268+
logEvent("\nACCESSIBILITY DEMONSTRATION");
269+
270+
// Create accessible button
271+
Button accessibleButton = new Button("Accessibility Demo") {
272+
private static final long serialVersionUID = 1L;
273+
274+
@Override
275+
public AccessibleContext getAccessibleContext() {
276+
AccessibleContext ac = super.getAccessibleContext();
277+
ac.setAccessibleName("Demo Accessible Button");
278+
ac.setAccessibleDescription("This button demonstrates accessibility features");
279+
return ac;
280+
}
281+
};
282+
283+
accessibleButton.setBackground(new Color(255, 220, 255));
284+
buttonPanel.add(accessibleButton);
285+
accessibleButton.addActionListener(e -> {
286+
AccessibleContext ac = accessibleButton.getAccessibleContext();
287+
logEvent("Accessibility Name: " + ac.getAccessibleName());
288+
logEvent("Accessibility Description: " + ac.getAccessibleDescription());
289+
});
290+
}
291+
292+
/* Displays the main frame */
293+
private void showFrame() {
294+
mainFrame.setVisible(true);
295+
logEvent("GUI Application window is now visible");
296+
}
297+
}

0 commit comments

Comments
 (0)