Java by API/org.eclipse.swt.widgets/Label

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

Label: setBounds(Rectangle rect)

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 MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Label label = new Label(shell, SWT.CENTER);
    label.setText("Hello, World");
    label.setBounds(shell.getClientArea());
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Label: setFont(Font f)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    // Create the main window
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, false));
    final Label fontLabel = new Label(shell, SWT.NONE);
    fontLabel.setText("The selected font");
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Font...");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        // Create the color-change dialog
        FontDialog dlg = new FontDialog(shell);
        Font font = null;
        Color color = null;
        if (font != null)
          dlg.setFontList(fontLabel.getFont().getFontData());
        if (color != null)
          dlg.setRGB(color.getRGB());
        if (dlg.open() != null) {
          if (font != null)
            font.dispose();
          if (color != null)
            color.dispose();
          font = new Font(shell.getDisplay(), dlg.getFontList());
          fontLabel.setFont(font);
          color = new Color(shell.getDisplay(), dlg.getRGB());
          fontLabel.setForeground(color);
          shell.pack();
        }
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Label: setForeground(Color c)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    // Create the main window
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, false));
    final Label fontLabel = new Label(shell, SWT.NONE);
    fontLabel.setText("The selected font");
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Font...");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        // Create the color-change dialog
        FontDialog dlg = new FontDialog(shell);
        Font font = null;
        Color color = null;
        if (font != null)
          dlg.setFontList(fontLabel.getFont().getFontData());
        if (color != null)
          dlg.setRGB(color.getRGB());
        if (dlg.open() != null) {
          if (font != null)
            font.dispose();
          if (color != null)
            color.dispose();
          font = new Font(shell.getDisplay(), dlg.getFontList());
          fontLabel.setFont(font);
          color = new Color(shell.getDisplay(), dlg.getRGB());
          fontLabel.setForeground(color);
          shell.pack();
        }
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Label: setImage(Image img)

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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 MainClass {
  public static void main(String[] a) {
    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.");
    // Create a vertical separator
    new Label(shell, SWT.SEPARATOR);
    // Create a label with a border
    new Label(shell, SWT.BORDER).setText("This is a label with a border.");
    // Create a horizontal separator
    Label separator = new Label(shell, SWT.HORIZONTAL | SWT.SEPARATOR);
    separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    // Create a label with an image
//    Image image = new Image(display, "interspatial.gif");
//    Label imageLabel = new Label(shell, SWT.NONE);
//    imageLabel.setImage(image);
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Label: setLayoutData(Object layoutData)

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250,250);
    s.setText("A FormLayout Example");
    s.setLayout(new FormLayout());
    
    final Label l1 = new Label(s, SWT.RIGHT);
    l1.setText("First Name");
    FormData fd = new FormData();
    fd.top = new FormAttachment(10, 10);
    fd.left = new FormAttachment(0, 10);
    fd.bottom = new FormAttachment(30,0);
    fd.right = new FormAttachment(40,0);
    l1.setLayoutData(fd);
    
    final Label l2 = new Label(s, SWT.RIGHT);
    l2.setText("Last Name");
    fd = new FormData();
    fd.top = new FormAttachment(l1, 5);
    fd.left = new FormAttachment(0, 10);
    fd.bottom = new FormAttachment(40,0);
    fd.right = new FormAttachment(40,0);
    l2.setLayoutData(fd);
    
    final Text t1 = new Text(s, SWT.BORDER | SWT.SINGLE);
    fd = new FormData();
    fd.top = new FormAttachment(l1, 0, SWT.TOP);
    fd.left = new FormAttachment(l1, 10);
    t1.setLayoutData(fd);
    
    final Text t2 = new Text(s, SWT.BORDER | SWT.SINGLE);
    fd = new FormData();
    fd.top = new FormAttachment(l2, 0, SWT.TOP);
    fd.left = new FormAttachment(l2, 10);
    t2.setLayoutData(fd);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Label: setText(String text)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    // Create the main window
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, false));
    final Label fontLabel = new Label(shell, SWT.NONE);
    fontLabel.setText("The selected font");
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Font...");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        // Create the color-change dialog
        FontDialog dlg = new FontDialog(shell);
        Font font = null;
        Color color = null;
        if (font != null)
          dlg.setFontList(fontLabel.getFont().getFontData());
        if (color != null)
          dlg.setRGB(color.getRGB());
        if (dlg.open() != null) {
          if (font != null)
            font.dispose();
          if (color != null)
            color.dispose();
          font = new Font(shell.getDisplay(), dlg.getFontList());
          fontLabel.setFont(font);
          color = new Color(shell.getDisplay(), dlg.getRGB());
          fontLabel.setForeground(color);
          shell.pack();
        }
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





new Label(Shell shell, SWT.BORDER)

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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 MainClass {
  public static void main(String[] a) {
    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.");
    // Create a vertical separator
    new Label(shell, SWT.SEPARATOR);
    // Create a label with a border
    new Label(shell, SWT.BORDER).setText("This is a label with a border.");
    // Create a horizontal separator
    Label separator = new Label(shell, SWT.HORIZONTAL | SWT.SEPARATOR);
    separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    // Create a label with an image
//    Image image = new Image(display, "interspatial.gif");
//    Label imageLabel = new Label(shell, SWT.NONE);
//    imageLabel.setImage(image);
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





new Label(Shell shell, SWT.NONE)

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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 MainClass {
  public static void main(String[] a) {
    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.");
    // Create a vertical separator
    new Label(shell, SWT.SEPARATOR);
    // Create a label with a border
    new Label(shell, SWT.BORDER).setText("This is a label with a border.");
    // Create a horizontal separator
    Label separator = new Label(shell, SWT.HORIZONTAL | SWT.SEPARATOR);
    separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    // Create a label with an image
//    Image image = new Image(display, "interspatial.gif");
//    Label imageLabel = new Label(shell, SWT.NONE);
//    imageLabel.setImage(image);
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





new Label(Shell shell, SWT.SEPARATOR)

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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 MainClass {
  public static void main(String[] a) {
    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.");
    // Create a vertical separator
    new Label(shell, SWT.SEPARATOR);
    // Create a label with a border
    new Label(shell, SWT.BORDER).setText("This is a label with a border.");
    // Create a horizontal separator
    Label separator = new Label(shell, SWT.HORIZONTAL | SWT.SEPARATOR);
    separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    // Create a label with an image
//    Image image = new Image(display, "interspatial.gif");
//    Label imageLabel = new Label(shell, SWT.NONE);
//    imageLabel.setImage(image);
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}