1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
/* Set Vertical Alignment of JLabel Example This java example shows how to set and get vertical alignment of label's content using Java Swing JLabel class. */ import javax.swing.JApplet; import javax.swing.JLabel; /* <applet code="JLabelVerticalAlignmentExample" width=200 height=200> </applet> */ public class JLabelVerticalAlignmentExample extends JApplet{ public void init(){ /* * To create JLabel use * JLabel (String caption) constructor * of JLabel class. */ JLabel label1 = new JLabel("JLabel Get Horizontal Alignment Example."); //add label to applet add(label1); /* * To get vertical alignment of label's content use, * int getVerticalAlignment() * method of JLabel's class. * * * Return value is one of the following constants, * LEFT, CENTER, RIGHT, LEADING or TRAILING. * */ int alignment = label1.getVerticalAlignment(); switch(alignment){ case JLabel.LEFT: label1.setText("Left"); break; case JLabel.CENTER: label1.setText("Center"); break; case JLabel.RIGHT: label1.setText("Right"); break; case JLabel.LEADING: label1.setText("Leading"); break; case JLabel.TRAILING: label1.setText("Trailing"); break; } /* * To set vertical alignment of JLabel use, * void setVerticalAlignment(int alignment) * method */ label1.setVerticalAlignment(JLabel.CENTER); } } |
Add Comment