|
| 1 | +package a8_javax.swing; |
1 | 2 |
|
| 3 | +import javax.accessibility.AccessibleContext; |
| 4 | +import javax.swing.*; |
| 5 | +import javax.swing.border.*; |
| 6 | +import javax.swing.plaf.LabelUI; |
| 7 | +import java.awt.*; |
| 8 | + |
| 9 | +/* JLabel Example: |
| 10 | + * A comprehensive demonstration implementing all constructors, fields, methods, |
| 11 | + * and functionalities for JLabel. */ |
| 12 | +public class b14_JLabelExample { |
| 13 | + |
| 14 | + public static void main(String[] args) { |
| 15 | + // Master JFrame for JLabel demonstration |
| 16 | + JFrame frame = new JFrame("Ultimate JLabel Comprehensive Example"); |
| 17 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 18 | + frame.setSize(1200, 1200); |
| 19 | + frame.setLayout(new BorderLayout()); |
| 20 | + |
| 21 | + // Main JPanel to hold the examples |
| 22 | + JPanel labelPanel = new JPanel(); |
| 23 | + labelPanel.setLayout(new GridLayout(20, 1, 10, 10)); // Enough rows for all examples |
| 24 | + |
| 25 | + // Add all comprehensive examples |
| 26 | + demonstrateAllConstructors(labelPanel); // All JLabel constructors |
| 27 | + demonstrateTextAndIconGap(labelPanel); // Demonstrate text/icon gap |
| 28 | + demonstrateAlignment(labelPanel); // All alignment methods |
| 29 | + demonstrateDisabledIcon(labelPanel); // Disabled icon usage |
| 30 | + demonstrateInteractiveFieldUsage(labelPanel); // JLabel's "labelFor" field example |
| 31 | + demonstrateAccessibilityDetails(labelPanel); // Accessibility and Look and Feel details |
| 32 | + demonstrateCustomGraphicsAndUpdate(labelPanel); // Custom `update()` repaint |
| 33 | + demonstrateAllIconFunctions(labelPanel); // All icon-related methods |
| 34 | + demonstrateParamStringUsage(labelPanel); // `paramString()` method usage |
| 35 | + |
| 36 | + // Scrollable content |
| 37 | + JScrollPane scrollPane = new JScrollPane(labelPanel); |
| 38 | + frame.add(scrollPane, BorderLayout.CENTER); |
| 39 | + |
| 40 | + // User-friendly description |
| 41 | + JTextArea description = new JTextArea( |
| 42 | + "Welcome to the Ultimate JLabel Example Application.\n\n" + |
| 43 | + "This program showcases:\n" + |
| 44 | + "- All JLabel constructors and configurations.\n" + |
| 45 | + "- Accessibility, Look and Feel, alignment, and icon properties.\n" + |
| 46 | + "- Comprehensive coverage of JLabel's methods for custom development needs.\n\n" + |
| 47 | + "Each example is carefully crafted to cover theoretical and practical aspects of JLabel." |
| 48 | + ); |
| 49 | + description.setEditable(false); |
| 50 | + description.setBorder(new TitledBorder("Description")); |
| 51 | + frame.add(description, BorderLayout.SOUTH); |
| 52 | + |
| 53 | + // Display the frame |
| 54 | + frame.setLocationRelativeTo(null); // Center the frame |
| 55 | + frame.setVisible(true); |
| 56 | + } |
| 57 | + |
| 58 | + /* Demonstrates all JLabel constructors. */ |
| 59 | + private static void demonstrateAllConstructors(JPanel panel) { |
| 60 | + // JLabel(): Empty label |
| 61 | + JLabel emptyLabel = new JLabel(); |
| 62 | + emptyLabel.setFont(new Font("Arial", Font.PLAIN, 16)); |
| 63 | + emptyLabel.setText("1. JLabel(): Empty label with text set later."); |
| 64 | + emptyLabel.setHorizontalAlignment(JLabel.CENTER); |
| 65 | + emptyLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY)); |
| 66 | + panel.add(emptyLabel); |
| 67 | + |
| 68 | + // JLabel(String text): Label with only text |
| 69 | + JLabel textLabel = new JLabel("2. JLabel(String text): Label with text only"); |
| 70 | + textLabel.setFont(new Font("Arial", Font.BOLD, 16)); |
| 71 | + textLabel.setForeground(Color.BLUE); |
| 72 | + panel.add(textLabel); |
| 73 | + |
| 74 | + // JLabel(String text, int horizontalAlignment): Text with alignment |
| 75 | + JLabel alignedLabel = new JLabel("3. JLabel(String text, int horizontalAlignment): Right-aligned text", JLabel.RIGHT); |
| 76 | + alignedLabel.setFont(new Font("Arial", Font.ITALIC, 16)); |
| 77 | + panel.add(alignedLabel); |
| 78 | + |
| 79 | + // JLabel(String text, Icon icon, int horizontalAlignment) |
| 80 | + Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon"); |
| 81 | + JLabel textIconLabel = new JLabel("4. Text + Icon (Right-Aligned)", infoIcon, JLabel.RIGHT); |
| 82 | + textIconLabel.setFont(new Font("Arial", Font.PLAIN, 14)); |
| 83 | + textIconLabel.setBackground(Color.LIGHT_GRAY); |
| 84 | + textIconLabel.setOpaque(true); |
| 85 | + panel.add(textIconLabel); |
| 86 | + |
| 87 | + // JLabel(Icon image): Icon only |
| 88 | + Icon warningIcon = UIManager.getIcon("OptionPane.warningIcon"); |
| 89 | + JLabel iconOnlyLabel = new JLabel(warningIcon); |
| 90 | + iconOnlyLabel.setHorizontalAlignment(JLabel.CENTER); |
| 91 | + iconOnlyLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); |
| 92 | + panel.add(iconOnlyLabel); |
| 93 | + |
| 94 | + // JLabel(Icon image, int horizontalAlignment): Icon with alignment |
| 95 | + Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon"); |
| 96 | + JLabel iconAlignLabel = new JLabel(errorIcon, JLabel.LEFT); |
| 97 | + panel.add(iconAlignLabel); |
| 98 | + } |
| 99 | + |
| 100 | + /* Demonstrates the `setIconTextGap` method for controlling the spacing between icons and text. */ |
| 101 | + private static void demonstrateTextAndIconGap(JPanel panel) { |
| 102 | + Icon questionIcon = UIManager.getIcon("OptionPane.questionIcon"); |
| 103 | + JLabel gapLabel = new JLabel("Icon-Text Gap Example", questionIcon, JLabel.LEFT); |
| 104 | + gapLabel.setFont(new Font("Arial", Font.PLAIN, 14)); |
| 105 | + gapLabel.setIconTextGap(50); // Set custom gap between text and icon |
| 106 | + gapLabel.setBorder(BorderFactory.createLineBorder(Color.MAGENTA)); |
| 107 | + panel.add(gapLabel); |
| 108 | + } |
| 109 | + |
| 110 | + /* Demonstrates all alignment methods: horizontal and vertical alignments. */ |
| 111 | + private static void demonstrateAlignment(JPanel panel) { |
| 112 | + JLabel alignmentLabel = new JLabel("Alignment Example"); |
| 113 | + alignmentLabel.setFont(new Font("Arial", Font.BOLD, 14)); |
| 114 | + alignmentLabel.setHorizontalAlignment(JLabel.CENTER); // Horizontal alignment |
| 115 | + alignmentLabel.setVerticalAlignment(JLabel.BOTTOM); // Vertical alignment |
| 116 | + alignmentLabel.setBorder(BorderFactory.createTitledBorder("Custom Alignments")); |
| 117 | + panel.add(alignmentLabel); |
| 118 | + } |
| 119 | + |
| 120 | + /* Demonstrates the disabled icon functionality. */ |
| 121 | + private static void demonstrateDisabledIcon(JPanel panel) { |
| 122 | + Icon enabledIcon = UIManager.getIcon("OptionPane.informationIcon"); |
| 123 | + Icon disabledIcon = UIManager.getIcon("OptionPane.errorIcon"); |
| 124 | + JLabel disabledLabel = new JLabel("Disabled Icon Example", enabledIcon, JLabel.CENTER); |
| 125 | + disabledLabel.setDisabledIcon(disabledIcon); // Set the disabled version of the icon |
| 126 | + disabledLabel.setEnabled(false); // Disable the label to observe the effect |
| 127 | + disabledLabel.setFont(new Font("Arial", Font.PLAIN, 14)); |
| 128 | + disabledLabel.setBorder(BorderFactory.createDashedBorder(Color.RED)); |
| 129 | + panel.add(disabledLabel); |
| 130 | + } |
| 131 | + |
| 132 | + /* Demonstrates the JLabel "labelFor" field with a form component. */ |
| 133 | + private static void demonstrateInteractiveFieldUsage(JPanel panel) { |
| 134 | + JLabel labelFor = new JLabel("Enter your username: "); |
| 135 | + JTextField textField = new JTextField(20); |
| 136 | + labelFor.setLabelFor(textField); // Associate the label with the text field |
| 137 | + JPanel formPanel = new JPanel(); |
| 138 | + formPanel.add(labelFor); |
| 139 | + formPanel.add(textField); |
| 140 | + formPanel.setBorder(BorderFactory.createTitledBorder("Form Field Example")); |
| 141 | + panel.add(formPanel); |
| 142 | + } |
| 143 | + |
| 144 | + /* Demonstrates accessibility methods and Look and Feel features. */ |
| 145 | + private static void demonstrateAccessibilityDetails(JPanel panel) { |
| 146 | + JLabel accessibilityLabel = new JLabel("Accessibility Example"); |
| 147 | + accessibilityLabel.setFont(new Font("Arial", Font.PLAIN, 14)); |
| 148 | + |
| 149 | + // Retrieve Accessibility Context |
| 150 | + AccessibleContext ac = accessibilityLabel.getAccessibleContext(); |
| 151 | + System.out.println("AccessibleContext: " + ac); |
| 152 | + |
| 153 | + // Look and Feel |
| 154 | + LabelUI ui = accessibilityLabel.getUI(); |
| 155 | + System.out.println("LabelUI: " + ui); |
| 156 | + String uiClassID = accessibilityLabel.getUIClassID(); |
| 157 | + System.out.println("UIClassID: " + uiClassID); |
| 158 | + |
| 159 | + panel.add(accessibilityLabel); |
| 160 | + } |
| 161 | + |
| 162 | + /* Demonstrates custom repainting with `update(Graphics)` and custom painting. */ |
| 163 | + @SuppressWarnings("serial") |
| 164 | + private static void demonstrateCustomGraphicsAndUpdate(JPanel panel) { |
| 165 | + JLabel customLabel = new JLabel() { |
| 166 | + @Override |
| 167 | + protected void paintComponent(Graphics g) { |
| 168 | + super.paintComponent(g); |
| 169 | + g.setColor(Color.RED); |
| 170 | + g.drawString("Custom Painted Text", 25, 25); |
| 171 | + } |
| 172 | + }; |
| 173 | + customLabel.setFont(new Font("Arial", Font.PLAIN, 14)); |
| 174 | + customLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); |
| 175 | + panel.add(customLabel); |
| 176 | + } |
| 177 | + |
| 178 | + /* Demonstrates icon management methods. */ |
| 179 | + private static void demonstrateAllIconFunctions(JPanel panel) { |
| 180 | + Icon originalIcon = UIManager.getIcon("OptionPane.informationIcon"); |
| 181 | + JLabel iconLabel = new JLabel("Icon Management Example"); |
| 182 | + iconLabel.setIcon(originalIcon); |
| 183 | + iconLabel.setFont(new Font("Arial", Font.PLAIN, 14)); |
| 184 | + panel.add(iconLabel); |
| 185 | + } |
| 186 | + |
| 187 | + /* Demonstrates the usage of the `paramString()` method with a custom class. */ |
| 188 | + private static void demonstrateParamStringUsage(JPanel panel) { |
| 189 | + // Create a custom UILabel that exposes the paramString() method |
| 190 | + CustomJLabel paramStringLabel = new CustomJLabel("paramString() Method Example"); |
| 191 | + paramStringLabel.setFont(new Font("Arial", Font.PLAIN, 14)); |
| 192 | + |
| 193 | + // Print paramString in terminal using the new getParamString() method |
| 194 | + System.out.println(paramStringLabel.getParamString()); |
| 195 | + panel.add(paramStringLabel); |
| 196 | + } |
| 197 | + |
| 198 | + // Create a custom JLabel subclass to expose the protected paramString() method |
| 199 | + @SuppressWarnings("serial") |
| 200 | + private static class CustomJLabel extends JLabel { |
| 201 | + public CustomJLabel(String text) { |
| 202 | + super(text); |
| 203 | + } |
| 204 | + |
| 205 | + public String getParamString() { |
| 206 | + // Access the protected method |
| 207 | + return paramString(); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments