Java Tutorial/SWT/CLabel

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

CLabel: center and shadow out

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CLabelCenterShadowOut {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("CLabel Test");
    shell.setLayout(new GridLayout(1, false));
    CLabel center = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
    center.setText("Center and Shadow Out");
    center.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





CLabel: left and shadow in

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CLabelLeftShadowIn {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("CLabel Test");
    shell.setLayout(new GridLayout(1, false));
    CLabel left = new CLabel(shell, SWT.LEFT | SWT.SHADOW_IN);
    left.setText("Left and Shadow In");
    left.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





CLabel: right and shadow none

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CLabelRightShadowNone {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("CLabel Test");
    shell.setLayout(new GridLayout(1, false));
    CLabel right = new CLabel(shell, SWT.RIGHT | SWT.SHADOW_NONE);
    right.setText("Right and Shadow None");
    right.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Creating a CLabel

CLabel Styles

StyleDescriptionSWT.LEFTCreates a left-aligned CLabelSWT.CENTERCreates a center-aligned CLabelSWT.RIGHTCreates a right-aligned CLabelSWT.SHADOW_INCreates a CLabel that appears recessed into the screenSWT.SHADOW_OUTCreates a CLabel that appears to extrude from the screenSWT.SHADOW_NONECreates a CLabel thtat appears to shadow


Label vs. CLabel

Description                          Label      CLabel
Alignment (left, right, and center)  Yes        Yes
Shadow (in, out, and none)           Yes        Yes
Wrap                                 Yes        No
Text                                 Yes        Yes
Image                                Yes        Yes
Text and image                       No         Yes
Tool tip                             Yes        Yes
Background color                     Yes        Yes
Background color gradient            No         Yes
Background image                     No         Yes
Font                                 Yes        Yes
Automatically shorten text           No         Yes





Set the background image of a CLabel object

public void setBackground(Image image)





The CLabel Class

  1. The CLabel class provides some advanced features over the Label class.
  2. The CLabel class can display its text label and image label at the same time.



import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CLabelWithTextImage {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new FillLayout());
    CLabel label = new CLabel(shell, SWT.BORDER );
    label.setText("text on the label");
    label.setImage(new Image(display,"yourFile.gif"));
    shell.open();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
}





To draw a gradient background: three colors

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CLabelGradientBackgroundThreeColors {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("CLabel Gradient");
    shell.setLayout(new GridLayout(1, false));
    CLabel one = new CLabel(shell, SWT.LEFT);
    one.setText("Second Gradient Example");
    one.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    // Set the background gradient
    one.setBackground(new Color[] { shell.getDisplay().getSystemColor(SWT.COLOR_WHITE),
        shell.getDisplay().getSystemColor(SWT.COLOR_GRAY),
        shell.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY),
        shell.getDisplay().getSystemColor(SWT.COLOR_BLACK) }, new int[] { 33, 67, 100 });

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





To draw a gradient background: two colors

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CLabelGradientBackground {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("CLabel Gradient");
    shell.setLayout(new GridLayout(1, false));
    // Create the CLabels
    CLabel one = new CLabel(shell, SWT.LEFT);
    one.setText("First Gradient Example");
    one.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    one.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
    // Set the background gradient
    one.setBackground(new Color[] { shell.getDisplay().getSystemColor(SWT.COLOR_RED),
        shell.getDisplay().getSystemColor(SWT.COLOR_GREEN),
        shell.getDisplay().getSystemColor(SWT.COLOR_BLUE) }, new int[] { 50, 50 });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}