Inhaltsverzeichnis
Die Anleitung zu Java SWT Label
View more Tutorials:
SWT Label ist eine Schnittstelle Komponent (UI Component), der die Text und Image anzeigen kann. Aber er kann nicht beide zeigen. Sie sollen CLabel benutzen - eine untergeordnete class vom Label damit er die beide Text und Image gleichzeitig anzeigen kann

Das ist ein einfaches Beispiel über Label mit der Anzeige von Text. Manchmal wird Laben bennutzt um die vertikalen oder horizontalen Separatoren zu erstellen

LabelDemo.java
package org.o7planning.swt.label; 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 LabelDemo { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); RowLayout layout = new RowLayout(); layout.spacing = 20; shell.setLayout(layout); // Create a Label with Text Label label1 = new Label(shell, SWT.NONE); label1.setText("My Label"); // Create a Label is Horizontal Separator Label hSeparator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); // Create a Label is Vertical Separator Label vSeparator = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL); shell.setText("SWT Label Demo"); shell.setSize(450, 250); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Notiz: Die Class Label kann die Text oder Icon anzeigen. Wenn Sie möchten die beide von Text und Icon gleichzeitig anzeigen, sollen Sie die class CLabel benutzen.


MyImageUtils.java
package org.o7planning.swt.utils; import java.io.IOException; import java.io.InputStream; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; public class MyImageUtils { // resourcePath: "/org/o7planning/swt/icon/java-32.png" public static Image getImage(Display display, String resourcePath) { InputStream input = null; try { // /org/o7planning/swt/icon/java-32.png input = MyImageUtils.class.getResourceAsStream(resourcePath); Image image = new Image(display, input); return image; } finally { closeQuietly(input); } } private static void closeQuietly(InputStream is) { try { if (is != null) { is.close(); } } catch (IOException e) { } } }
Sehen Sie das Beispiel
LabelIconDemo.java
package org.o7planning.swt.label; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CLabel; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.o7planning.swt.utils.MyImageUtils; public class LabelIconDemo { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); RowLayout layout = new RowLayout(); layout.spacing = 20; shell.setLayout(layout); Image image = MyImageUtils.getImage(display, "/org/o7planning/swt/icon/java-48.png"); Image image2 = MyImageUtils.getImage(display, "/org/o7planning/swt/icon/java-32.png"); // Create a Label with Text Label label1 = new Label(shell, SWT.NONE); label1.setText("Label with Text"); // Create a Label with Icon Label label2 = new Label(shell, SWT.NONE); label2.setImage(image2); // Create a CLabel with Text & Icon CLabel cLabel = new CLabel(shell, SWT.NONE); cLabel.setImage(image); cLabel.setText("SWT CLabel"); shell.setText("SWT Label Demo (o7planning.org)"); shell.setSize(450, 250); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Sie können die Unterlagen von SWT Font & Color lesen bei :
- TODO Link!

Font
Sie können die Font ( einschließen Font und die Größe) für Label durch die Method setFont.setzen
// Tạo một đối tượng Font mới Font font = new Font(display, "Cambria", 22, SWT.ITALIC); label.setFont(font);
Color
benutzen Sie die Method setForeground um die Farbe der Schrift für Label.zu setzen
Color color = new Color(display, 0, 114, 135); // Set fore color label.setForeground(color);
Schauen Sie das Beispiel an
LabelFullDemo.java
package org.o7planning.swt.label; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; 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 LabelFontColorDemo { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); RowLayout layout = new RowLayout(); layout.spacing = 20; shell.setLayout(layout); // ------ Label ----- Label label = new Label(shell, SWT.NONE); label.setText("Label With Font and Color"); label.setFont(new Font(display, "Cambria", 22, SWT.ITALIC)); Color color = new Color(display, 0, 114, 135); // Set fore color label.setForeground(color); shell.setText("SWT Label Demo (o7planning.org)"); shell.setSize(450, 250); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }