Label.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Label.java -- Java label widget
  2. Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import java.awt.peer.LabelPeer;
  33. import java.awt.peer.ComponentPeer;
  34. import java.io.Serializable;
  35. import javax.accessibility.Accessible;
  36. /**
  37. * This component is used for displaying simple text strings that cannot
  38. * be edited.
  39. *
  40. * @author Aaron M. Renn (arenn@urbanophile.com)
  41. * @author Tom Tromey <tromey@cygnus.com>
  42. */
  43. public class Label extends Component implements Serializable, Accessible
  44. {
  45. /*
  46. * Static Variables
  47. */
  48. /**
  49. * Alignment constant aligning the text to the left of its window.
  50. */
  51. public static final int LEFT = 0;
  52. /**
  53. * Alignment constant aligning the text in the center of its window.
  54. */
  55. public static final int CENTER = 1;
  56. /**
  57. * Alignment constant aligning the text to the right of its window.
  58. */
  59. public static final int RIGHT = 2;
  60. // Serialization version constant:
  61. private static final long serialVersionUID = 3094126758329070636L;
  62. /*************************************************************************/
  63. /*
  64. * Instance Variables
  65. */
  66. /**
  67. * @serial Indicates the alignment of the text within this label's window.
  68. * This is one of the constants in this class. The default value is
  69. * <code>LEFT</code>.
  70. */
  71. private int alignment;
  72. /**
  73. * @serial The text displayed in the label
  74. */
  75. private String text;
  76. /*************************************************************************/
  77. /*
  78. * Constructors
  79. */
  80. /**
  81. * Initializes a new instance of <code>Label</code> with no text.
  82. *
  83. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  84. */
  85. public
  86. Label()
  87. {
  88. this("", LEFT);
  89. }
  90. /*************************************************************************/
  91. /**
  92. * Initializes a new instance of <code>Label</code> with the specified
  93. * text that is aligned to the left.
  94. *
  95. * @param text The text of the label.
  96. *
  97. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  98. */
  99. public
  100. Label(String text)
  101. {
  102. this(text, LEFT);
  103. }
  104. /*************************************************************************/
  105. /**
  106. * Initializes a new instance of <code>Label</code> with the specified
  107. * text and alignment.
  108. *
  109. * @param text The text of the label.
  110. * @param alignment The desired alignment for the text in this label,
  111. * which must be one of <code>LEFT</code>, <code>CENTER</code>, or
  112. * <code>RIGHT</code>.
  113. *
  114. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  115. */
  116. public
  117. Label(String text, int alignment)
  118. {
  119. setAlignment (alignment);
  120. setText (text);
  121. if (GraphicsEnvironment.isHeadless())
  122. throw new HeadlessException ();
  123. }
  124. /*************************************************************************/
  125. /*
  126. * Instance Variables
  127. */
  128. /**
  129. * Returns the constant indicating the alignment of the text in this
  130. * label. The value returned will be one of the alignment constants
  131. * from this class.
  132. *
  133. * @return The alignment of the text in the label.
  134. */
  135. public int
  136. getAlignment()
  137. {
  138. return(alignment);
  139. }
  140. /*************************************************************************/
  141. /**
  142. * Sets the text alignment of this label to the specified value.
  143. *
  144. * @param alignment The desired alignment for the text in this label,
  145. * which must be one of <code>LEFT</code>, <code>CENTER</code>, or
  146. * <code>RIGHT</code>.
  147. */
  148. public synchronized void
  149. setAlignment(int alignment)
  150. {
  151. if (alignment != CENTER && alignment != LEFT && alignment != RIGHT)
  152. throw new IllegalArgumentException ("invalid alignment: " + alignment);
  153. this.alignment = alignment;
  154. if (peer != null)
  155. {
  156. LabelPeer lp = (LabelPeer) peer;
  157. lp.setAlignment (alignment);
  158. }
  159. }
  160. /*************************************************************************/
  161. /**
  162. * Returns the text displayed in this label.
  163. *
  164. * @return The text for this label.
  165. */
  166. public String
  167. getText()
  168. {
  169. return(text);
  170. }
  171. /*************************************************************************/
  172. /**
  173. * Sets the text in this label to the specified value.
  174. *
  175. * @param text The new text for this label.
  176. */
  177. public synchronized void
  178. setText(String text)
  179. {
  180. this.text = text;
  181. if (peer != null)
  182. {
  183. LabelPeer lp = (LabelPeer) peer;
  184. lp.setText (text);
  185. }
  186. }
  187. /*************************************************************************/
  188. /**
  189. * Notifies this lable that it has been added to a container, causing
  190. * the peer to be created. This method is called internally by the AWT
  191. * system.
  192. */
  193. public void
  194. addNotify()
  195. {
  196. if (peer == null)
  197. peer = getToolkit ().createLabel (this);
  198. super.addNotify ();
  199. }
  200. /*************************************************************************/
  201. /**
  202. * Returns a parameter string useful for debugging.
  203. *
  204. * @param A debugging string.
  205. */
  206. protected String
  207. paramString()
  208. {
  209. return ("text=" + getText() + ",alignment=" +
  210. getAlignment() + "," + super.paramString());
  211. }
  212. } // class Label