Java by API/org.eclipse.swt.widgets/Label
Содержание
- 1 Label: setBounds(Rectangle rect)
- 2 Label: setFont(Font f)
- 3 Label: setForeground(Color c)
- 4 Label: setImage(Image img)
- 5 Label: setLayoutData(Object layoutData)
- 6 Label: setText(String text)
- 7 new Label(Shell shell, SWT.BORDER)
- 8 new Label(Shell shell, SWT.NONE)
- 9 new Label(Shell shell, SWT.SEPARATOR)
Label: setBounds(Rectangle rect)
<source lang="java">
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(); }
}
</source>
Label: setFont(Font f)
<source lang="java">
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(); }
}
</source>
Label: setForeground(Color c)
<source lang="java">
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(); }
}
</source>
Label: setImage(Image img)
<source lang="java">
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(); }
}
</source>
Label: setLayoutData(Object layoutData)
<source lang="java">
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(); }
}
</source>
Label: setText(String text)
<source lang="java">
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(); }
}
</source>
new Label(Shell shell, SWT.BORDER)
<source lang="java">
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(); }
}
</source>
new Label(Shell shell, SWT.NONE)
<source lang="java">
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(); }
}
</source>
new Label(Shell shell, SWT.SEPARATOR)
<source lang="java">
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(); }
}
</source>