Java Tutorial/SWT/Label

Материал из Java эксперт
Перейти к: навигация, поиск

Adding Image to Label

To get and set the image label for a label, you can use the following



   <source lang="java">

public Image getImage()

   public void setImage(Image image)</source>
   
  
 
  



Add Label to Shell

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class PlainLabelExample {

 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell();
   shell.setLayout(new GridLayout(1, false));
   // Create a label
   new Label(shell, SWT.NONE).setText("This is a plain label.");
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Horizontal Separator Label

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class HorizontalSeparatorLabelExample {

 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell();
   shell.setLayout(new GridLayout(1, false));
   // Create a horizontal separator
   Label separator = new Label(shell, SWT.HORIZONTAL | SWT.SEPARATOR);
   separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Introducing Label

Labels are non-selectable user interface components that display strings or images.

Styles supported by the Label class are as follows: SEPARATOR, HORIZONTAL, VERTICAL

The SEPARATOR style causes a label to appear as a single line whose orientation can be either HORIZONTAL (default) or VERTICAL.

To create a Label, use its only constructor:



   <source lang="java">

Label(Composite parent, int style)</source>



  1. parent: the Composite to house the Label,
  2. style : the style.

Labels display either text or an image, but not both.


Label Styles

  1. You can combine them using the bitwise OR operator,
  2. You can specify only one alignment constant (SWT.LEFT, SWT.CENTER, or SWT.RIGHT).
  3. Many of the styles have an effect only when creating separators

StyleDescriptionSWT.SEPARATORCreates a visual divider.SWT.HORIZONTALUsed with SWT.SEPARATOR to create a horizontal separator.SWT.VERTICALUsed with SWT.SEPARATOR to create a vertical separator. This is the default.SWT.SHADOW_INUsed with SWT.SEPARATOR to draw a separator that appears recessed.SWT.SHADOW_OUTUsed with SWT.SEPARATOR to draw a separator that appears extruded.SWT.SHADOW_NONEUsed with SWT.SEPARATOR to draw a separator that appears unshadowed.SWT.CENTEROrients the text or image in the center of this Label.SWT.LEFTOrients the text or image to the left of this Label.SWT.RIGHTOrients the text or image to the right of this Label.SWT.WRAPCreates a Label that can wrap. Support for wrapped labels depends on the layout manager and is spotty.


Label with border

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class BorderLabelExample {

 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell();
   shell.setLayout(new GridLayout(1, false));
   // Create a label with a border
   new Label(shell, SWT.BORDER).setText("This is a label with a border.");
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Label with border(SWT.BORDER)

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class LabelWithBorder {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display, SWT.SHELL_TRIM);
   shell.setLayout(new RowLayout());
   Label label = new Label(shell, SWT.BORDER);
   label.setText("text on the label");
   shell.open();
   // Set up the event loop.
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       // If no more entries in event queue
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Label WRAP style

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class WrapStyleLabel {

 public static void main(String[] args) {
   final Display display = new Display();
   Shell shell = new Shell(display);
   String text = "This is a long long long long long long long text.";
   
   Label labelNoWrap = new Label(shell, SWT.BORDER);
   labelNoWrap.setText(text);
   labelNoWrap.setBounds(10, 10, 100, 100);
   
   Label labelWrap = new Label(shell, SWT.WRAP | SWT.BORDER);
   labelWrap.setText(text);
   labelWrap.setBounds(120, 10, 100, 100);
     
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
 }

}</source>





LEFT, RIGHT, CENTER: These styles specify the text/image alignment in labels.

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class LabelTextImageAlignment {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display, SWT.SHELL_TRIM);
   shell.setLayout(new FillLayout());
   Label label = new Label(shell, SWT.BORDER | SWT.RIGHT);
   label.setText("text on the label");
   shell.open();
   // Set up the event loop.
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       // If no more entries in event queue
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





SHADOW_IN, SHADOW_OUT, SHADOW_NONE: These three styles specify the shadow behavior of labels with the SEPARATOR style.

For the Label class, the style constants SHADOW_IN, SHADOW_OUT, SHADOW_NONE are applicable to label separators only.



   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class LabelShadowInOut {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display, SWT.SHELL_TRIM);
   shell.setLayout(new FillLayout());
   Label label = new Label(shell, SWT.BORDER|SWT.SHADOW_OUT);
   label.setText("text on the label");
   shell.open();
   // Set up the event loop.
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       // If no more entries in event queue
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Use Label as a separator

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class SeparatorLabelExample {

 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell();
   shell.setLayout(new GridLayout(1, false));
   // Create a vertical separator
   new Label(shell, SWT.SEPARATOR);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>