Le Tutoriel de Java SWT Label
View more Tutorials:
SWT Label est un composant de l'interface d'utilisateur (UI Component), qui peut afficher du texte ou de l'image mais ne peut pas les afficher simultanément. Pour les afficher simultanément, vous pouvez utiliser CLabel, une sous-classe de Label.

C'est un simple exemple de Label qui affiche le texte. Notamment la classe Label est utilisé afin de créer des separateurs (Separator) horizontalement ou verticalement.

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(); } }
Notez que la classe Label peut afficher le texte ou de la icône, si vous voulez afficher tous les deux Texte & Icône, vous devriez utiliser de la classe CLabel.


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) { } } }
Observez l'exemple complet:
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(); } }
Vous pouvez trouver plus de documents sur SWT Font & Color à:
- TODO Link!

Font
Vous pouvez définir la police (y compris le nom de la police et la taille) pour Label via la méthode setFont.
// Tạo một đối tượng Font mới Font font = new Font(display, "Cambria", 22, SWT.ITALIC); label.setFont(font);
Color
Utilisez de la méthode setForeground afin de définir la couleur des lettres de Label.
Color color = new Color(display, 0, 114, 135); // Set fore color label.setForeground(color);
Voyez l'exemple complet:
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(); } }