Java Tutorial/SWT/Label — различия между версиями

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

Текущая версия на 15:20, 31 мая 2010

Adding Image to Label

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



public Image getImage()
    public void setImage(Image image)





Add Label to Shell

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();
  }
}





Horizontal Separator Label

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();
  }
}





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:



Label(Composite parent, int style)



  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

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();
  }
}





Label with border(SWT.BORDER)

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();
  }
}





Label WRAP style

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();
      }
    }
  }
}





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

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();
  }
}





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.



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();
  }
}





Use Label as a separator

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();
  }
}