Java Tutorial/SWT/CLabel

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

CLabel: center and shadow out

   <source lang="java">

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

}</source>





CLabel: left and shadow in

   <source lang="java">

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

}</source>





CLabel: right and shadow none

   <source lang="java">

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

}</source>





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

   <source lang="java">

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</source>





Set the background image of a CLabel object

   <source lang="java">

public void setBackground(Image image)</source>





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.



   <source lang="java">

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

}</source>





To draw a gradient background: three colors

   <source lang="java">

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

}</source>





To draw a gradient background: two colors

   <source lang="java">

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

}</source>