Java Tutorial/SWT/Label
Содержание
- 1 Adding Image to Label
- 2 Add Label to Shell
- 3 Horizontal Separator Label
- 4 Introducing Label
- 5 Label Styles
- 6 Label with border
- 7 Label with border(SWT.BORDER)
- 8 Label WRAP style
- 9 LEFT, RIGHT, CENTER: These styles specify the text/image alignment in labels.
- 10 SHADOW_IN, SHADOW_OUT, SHADOW_NONE: These three styles specify the shadow behavior of labels with the SEPARATOR style.
- 11 Use Label as a separator
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)
- parent: the Composite to house the Label,
- style : the style.
Labels display either text or an image, but not both.
Label Styles
- You can combine them using the bitwise OR operator,
- You can specify only one alignment constant (SWT.LEFT, SWT.CENTER, or SWT.RIGHT).
- 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();
}
}