|
AsWing.org : The JLabel Component
This page last changed on Jun 13, 2006 by firdosh.
The JLabel ComponentThis tutorial goes in depth about the JLabel component The JLabel component enables the developer to create labels which can display text, images (Icons), or both. The figure below shows the component structure for JLabel.
From the figure above we can see that the JLabel component extends the Component class which is the base class for all components. Lets create a very simple example which will create a JLabel instance and display some text. 1. /* 2. Copyright aswing.org, see the LICENCE.txt. 3. */ 4. import org.aswing.BorderLayout; 5. import org.aswing.JFrame; 6. import org.aswing.JLabel; 7. /** 8. * 9. * @author firdosh 10. */ 11. class Main 12. { 13. public static function main () : Void 14. { 15. try 16. { 17. trace ("Create simple label"); 18. var frame : JFrame = new JFrame (); 19. frame.setClosable (false); 20. frame.setLocation (50, 50); 21. frame.setSize (200, 200); 22. frame.show (); 23. var label : JLabel = new JLabel ("Simple Text Example"); 24. frame.getContentPane ().append (label, BorderLayout.CENTER); 25. trace ("End simple label"); 26. } 27. catch (e) 28. { 29. trace ("error : " + e); 30. } 31. } 32. } 33. f you run the code above you should see the following example below. JLabel MethodsThe Constructor : The JLabel constructor takes three parameters . The first parameter is a String object which is the text to be displayed. The second parameter is a instance of the Icon class which is the symbol to be displayed next to text and the last parameter is the alignment of the label component. public function JLabel(text : String, icon : Icon , horizontalAlignment:Number) We can use the class properties of JLabel to align the component. The class properties for horizontal alignment are
For example : var label:JLabel=new JLabel("Simple Text Example",icon,JLabel.LEFT); Below is the example of a label instance aligned to the left. Now let us look at some of the other methods that the JLabel class has to offer. The setText (obj : String)
getText():String
The example below creates two JLabel instances and sets the text of the second instance the same as the first instance. 1. /* 2. Copyright aswing.org, see the LICENCE.txt. 3. */ 4. import org.aswing.BorderLayout; 5. import org.aswing.JFrame; 6. import org.aswing.JLabel; 7. import org.aswing.ASColor; 8. /** 9. * 10. * @author firdosh 11. */ 12. class Main 13. { 14. public static function main () : Void 15. { 16. try 17. { 18. trace ("Create simple label"); 19. var frame : JFrame = new JFrame (); 20. frame.setClosable (false); 21. frame.setLocation (50, 50); 22. frame.setSize (200, 200); 23. frame.show (); 24. var label1 : JLabel = new JLabel ("Simple Text Example", null, JLabel.LEFT); 25. var label2 : JLabel = new JLabel ("", null, JLabel.RIGHT); 26. label2.setText (label1.getText ()); 27. frame.getContentPane ().append (label1, BorderLayout.NORTH); 28. frame.getContentPane ().append (label2, BorderLayout.SOUTH); 29. trace ("End simple label"); 30. } 31. catch (e) 32. { 33. trace ("error : " + e); 34. } 35. } 36. } 37. In the above example we get the text of the label1 instance using the getText method and set the text of the label2 instance by using the setText method. setIcon (obj : Icon ) and getIcon():Icon These methods enable the developer to change the icon for a particular JLabel instance. setDisabledIcon (obj : Icon) and getDisabledIcon():Icon These methods enable the developer to change the disable icon for a particular JLabel instance. When the setEnabled(val:Boolean) method is set to true, this icon is displayed. If no disabled icon is specefied then a default grayscale version is displayed. setVerticalAlignment (alignment : Number ) and getVerticalAlignment() :Number setHorizontalAlignment(alignment: Number ) and getHorizontalAlignment() :Number The setVerticalAlignment and setHorizontalAlignment methods are used to specify the alignment of the label's content, both the text and icon. The values for these properties are defined in ASWingConstants, but are also available as class properties through the JLabel class for easy access. The values for horizontal alignment are
The values for vertical alignment are
The getVerticalAlignment and the getHorizontalAlignment methods return what kind of alignment was set for the component. setIconTextGap (obj : Number ) and getIconTextGap():Number The setIconTextGap method set the distance between the icon and text of the label. The default value of this property is 4 pixels. This is provided if both are defined. The getIconTextGap returns the distance between the text and the icon. 1. import org.aswing.BorderLayout; 2. import org.aswing.ASColor; 3. import org.aswing.JFrame; 4. import org.aswing.JLabel; 5. /* 6. Copyright aswing.org, see the LICENCE.txt. 7. */ 8. /** 9. * @author iiley 10. */ 11. class Main 12. { 13. private var label2 : JLabel; 14. public function Main (Void) 15. { 16. try 17. { 18. trace ("Create simple label"); 19. var frame : JFrame = new JFrame (); 20. frame.setClosable (false); 21. frame.setLocation (50, 50); 22. frame.setSize (200, 200); 23. frame.show (); 24. /** 25. *Create a new ColorIcon instances 26. */ 27. var icon : CircleIcon = new CircleIcon (new ASColor (0xff0000, 100) , 10, 10); 28. var disable : CircleIcon = new CircleIcon (new ASColor (0x333333, 100) , 10, 10); 29. var label1 : JLabel = new JLabel ("Simple Text Example", icon, JLabel.LEFT); 30. /** 31. * Set the disabled icon 32. */ 33. label1.setDisabledIcon (disable); 34. /** 35. * Disable the component. 36. */ 37. label1.setEnabled (false); 38. 39. /** 40. * Create a new JLabel instance 41. */ 42. label2 = new JLabel (null, null, JLabel.RIGHT); 43. 44. /** 45. * Set the icon for the bottom label 46. */ 47. 48. label2.setIcon (icon); 49. /** 50. * Sets the distance between the text and the icon 51. */ 52. label2.setIconTextGap (20); 53. /** 54. * Sets the orientation of the text with respect to the icon 55. */ 56. 57. label2.setHorizontalTextPosition (JLabel.LEFT ); 58. /** 59. * Sets the text to be displayed 60. */ 61. label2.setText (label1.getText ()); 62. /** 63. * Sets the alignment of the text and the icon. 64. */ 65. label2.setHorizontalAlignment (JLabel.RIGHT); 66. 67. frame.getContentPane ().append (label1, BorderLayout.NORTH); 68. frame.getContentPane ().append (label2, BorderLayout.SOUTH); 69. trace ("End simple label"); 70. } 71. catch (e : Error) 72. { 73. trace ("error : " + e); 74. } 75. } 76. public static function main () : Void 77. { 78. var t : Main = new Main (); 79. } 80. } 81. We create two instances of the JLabel component. We create two instances of the CircleIcon class, one for default icon and the othe for the disabled icon. We set the disabled iIcon by calling the setDisabledIcon method and then setting the enable property of the instance to false. Notice how the component turns grey. Now let us go through an example where all these properties are used. |
| Document generated by Confluence on Dec 19, 2006 14:17 |