Label alignment : Label « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » LabelScreenshots 
Label alignment
Label alignment


import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AlignLabels extends JPanel {
  JLabel[] labels = new JLabel[9];

  public AlignLabels() {
    JLabel label;

    setLayout(new GridLayout(33));

    label = createLabel("NW alignment");
    setNWalignment(label);
    label.setEnabled(false);
    labels[0= label;
    label = createLabel("N alignment");
    setNalignment(label);
    labels[1= label;
    label = createLabel("NE alignment");
    setNEalignment(label);
    labels[2= label;
    label = createLabel("W alignment");
    label.setText("<html><i>html based<br></i><font color=blue>W alignment</font>");
    setWalignment(label);
    labels[3= label;
    label = createLabel("C alignment");
    setCalignment(label);
    label.setEnabled(false);
    labels[4= label;
    label = createLabel("E alignment");
    setEalignment(label);
    labels[5= label;
    label = createLabel("SW alignment");
    setSWalignment(label);
    labels[6= label;
    label = createLabel("S alignment");
    setSalignment(label);
    labels[7= label;
    label = createLabel("SE alignment");
    setSEalignment(label);
    label.setEnabled(false);
    labels[8= label;
  }

  public static void main(String[] a) {
    JFrame mainFrame = new JFrame();
    mainFrame.getContentPane().add(new AlignLabels());
    
    mainFrame.setSize(500,500);

    mainFrame.setVisible(true);

  }

  JLabel[] getLabels() {
    return labels;
  }

  JLabel createLabel(String text) {
    String separator = System.getProperty("line.separator");
    JLabel label = new JLabel(text + separator + "multiline" + separator
        "label");
    label.setToolTipText(text + "\n\ndoubled space\n\ntooltip");
    label.setBorder(BorderFactory.createEtchedBorder());
    this.add(label);
    label.setPreferredSize(new Dimension(125125));
    return label;
  }

  void setNWalignment(JLabel b) {
    b.setHorizontalAlignment(JLabel.LEFT);
    b.setVerticalAlignment(JLabel.TOP);
  }

  void setNalignment(JLabel b) {
    b.setHorizontalAlignment(JLabel.CENTER);
    b.setVerticalAlignment(JLabel.TOP);
  }

  void setNEalignment(JLabel b) {
    b.setHorizontalAlignment(JLabel.RIGHT);
    b.setVerticalAlignment(JLabel.TOP);
  }

  void setWalignment(JLabel b) {
    b.setHorizontalAlignment(JLabel.LEFT);
    b.setVerticalAlignment(JLabel.CENTER);
  }

  void setCalignment(JLabel b) {
    b.setHorizontalAlignment(JLabel.CENTER);
    b.setVerticalAlignment(JLabel.CENTER);
  }

  void setEalignment(JLabel b) {
    b.setHorizontalAlignment(JLabel.RIGHT);
    b.setVerticalAlignment(JLabel.CENTER);
  }

  void setSWalignment(JLabel b) {
    b.setHorizontalAlignment(JLabel.LEFT);
    b.setVerticalAlignment(JLabel.BOTTOM);
  }

  void setSalignment(JLabel b) {
    b.setHorizontalAlignment(JLabel.CENTER);
    b.setVerticalAlignment(JLabel.BOTTOM);
  }

  void setSEalignment(JLabel b) {
    b.setHorizontalAlignment(JLabel.RIGHT);
    b.setVerticalAlignment(JLabel.BOTTOM);
  }
}

class ColoredSquare implements Icon {
  Color color;

  public ColoredSquare(Color color) {
    this.color = color;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    Color oldColor = g.getColor();
    g.setColor(color);
    g.fill3DRect(x, y, getIconWidth(), getIconHeight()true);
    g.setColor(oldColor);
  }

  public int getIconWidth() {
    return 12;
  }

  public int getIconHeight() {
    return 12;
  }
}
           
       
Related examples in the same category
1. Putting HTML text on Swing componentsPutting HTML text on Swing components
2. Shows how displayed Mnemonic and labelFor properties work togetherShows how displayed Mnemonic and labelFor properties work together
3. A quick application to show a simple JLabelA quick application to show a simple JLabel
4. A JLabel that uses inline HTML to format its textA JLabel that uses inline HTML to format its text
5. Variations on a text and icon labelVariations on a text and icon label
6. A simple demonstration of text alignment in JLabelsA simple demonstration of text alignment in JLabels
7. Label with Image Label with Image
8. Label with HTML SampleLabel with HTML Sample
9. Label Text PositionLabel Text Position
10. LabelFor DemoLabelFor Demo
11. Label with IconLabel with Icon
12. Active Label SampleActive Label Sample
13. Swing Label DemoSwing Label Demo
14. HTML labelHTML label
15. Label background, icon, alignLabel background, icon, align
16. Scrollable LabelScrollable Label
17. Grab and Drag image scroll labelGrab and Drag image scroll label
18. Animation label
19. Label with various effectsLabel with various effects
20. Label for Label for
21. LabelFocusSample: setLabelForLabelFocusSample: setLabelFor
w_w_w___.___ja__v__a___2s.__com___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.