Inhaltsverzeichnis
Die Anleitung zu Java SWT Text
View more Tutorials:
Die Class Text ist ein Control zur Akzeptierung und Anzeige der Textinput. Sie bezeichnet die Fähigkeit der Aufnahme der Text von einem Benutzer

Die class Text kann die Eingabe der Text erstellen. Mit der kann der Benutzer in einer Zeile eingeben. Sie kann auch die Eingabe der Text, die in viele Zeile eingegeben kann oder eine Umwelt der Passwort (Password field) bauen.
Lesen Sie mehr die Umwelt des Passwort (Password Field) im SWT:
// Style .... int style = SWT.BORDER; int style = SWT.BORDER | SWT.PASSWORD; Text text = new Text(parent, style);
Die Stile (style) können die Text angewendet werden
Style | Die Bezeichnung |
SWT.BORDER | Border anzeigen. |
SWT.MULTI | Die Inhalt auf die Zeile zu typen erlauben |
SWT.PASSWORD | Den Benutzer erlauben, das Passwort zu typen |
SWT.V_SCROLL | Den vertikalen Skrollbar anzeigen, normalerweise mit SWT.MULTI benutzen |
SWT.H_SCROLL | Den horizontalen Skrollbar anzeigen, normalerweise mit SWT.MULTI.benutzen |
SWT.WRAP | Automatisch einpacken (wrap) wenn die Text zu lang ist |
SWT.READ_ONLY | Nicht bearbeiten dürfen, es ist ähnlich wie die Aufruf auf die Methode text.setEditable(false); |
SWT.LEFT | Die Text left ausrichten |
SWT.RIGHT | Die Text recht ausrichten |
SWT.CENTER | Die Text mittel ausrichten |
Sehen Sie einige nutzliche Methode, die Sie mit Text benutzen
text.setEnabled(enabled); text.setFont(font); text.setForeground(color); text.setEditable(editable); text.setTextLimit(limit); text.setToolTipText(string); text.setTextDirection(textDirection); text.addSegmentListener(listener);

TextDemo.java
package org.o7planning.swt.text; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowData; 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.eclipse.swt.widgets.Text; public class TextDemo { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("SWT Text (o7planning.org)"); RowLayout rowLayout = new RowLayout(); rowLayout.marginLeft = 10; rowLayout.marginTop = 10; rowLayout.spacing = 15; shell.setLayout(rowLayout); // Label Label label = new Label(shell, SWT.NONE); label.setText("Your Name: "); // Text Text text = new Text(shell, SWT.BORDER); RowData layoutData = new RowData(); layoutData.width = 150; text.setLayoutData(layoutData); text.setText("Tran"); shell.setSize(400, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Das folgende Beispiel: Erstellung der unterschiedlichen SWT Text mit den Stile von SWT.BORDER, SWT.PASSWORD, SWT.MULTI, SWT.WRAP, SWT.READ_ONLY, ...

TextStylesDemo.java
package org.o7planning.swt.text; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowData; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class TextStylesDemo { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("SWT Text (o7planning.org)"); RowLayout rowLayout = new RowLayout(SWT.VERTICAL); rowLayout.marginLeft = 10; rowLayout.marginTop = 10; rowLayout.spacing = 15; shell.setLayout(rowLayout); // Text without border Text text1 = new Text(shell, SWT.NONE); text1.setText("SWT.NONE"); text1.setLayoutData(new RowData(150, SWT.DEFAULT)); // Text with border Text text2 = new Text(shell, SWT.BORDER); text2.setText("SWT.BORDER"); text2.setLayoutData(new RowData(150, SWT.DEFAULT)); // Text with border and readonly. Text text3 = new Text(shell, SWT.BORDER | SWT.READ_ONLY); text3.setText("SWT.BORDER | SWT.READ_ONLY"); text3.setLayoutData(new RowData(200, SWT.DEFAULT)); // Password field. Text text4 = new Text(shell, SWT.BORDER | SWT.PASSWORD); text4.setText("12345"); text4.setLayoutData(new RowData(150, SWT.DEFAULT)); // Text with multi lines and show vertiacal scroll. Text text5 = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL); text5.setText("SWT.BORDER | SWT.MULTI | SWT.V_SCROLL \n\n Hello World"); text5.setLayoutData(new RowData(250, 80)); // Wrap Text text6 = new Text(shell, SWT.BORDER | SWT.WRAP); text6.setLayoutData(new RowData(120, 80)); text6.setText("SWT.BORDER | SWT.WRAP, This is a text, it very long, and need to warp it"); shell.setSize(400, 400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Das folgende Beispiel stellt die Benutzung der Methode von clear(), copy(), paste(), cut(),dar. Sie sind die notwendigen Methode von TextField.

TextDemo2.java
package org.o7planning.swt.text; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.RowData; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class TextDemo2 { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("SWT Text (o7planning.org)"); RowLayout rowLayout = new RowLayout(); rowLayout.marginLeft = 10; rowLayout.marginTop = 10; rowLayout.spacing = 5; shell.setLayout(rowLayout); // Text Text text = new Text(shell, SWT.BORDER); text.setLayoutData(new RowData(150, SWT.DEFAULT)); text.setText("This is a Text"); // Clear Button Button clearButton = new Button(shell, SWT.PUSH); clearButton.setText("Clear"); // Copy Button Button copyButton = new Button(shell, SWT.PUSH); copyButton.setText("Copy"); // Cut Button Button cutButton = new Button(shell, SWT.PUSH); cutButton.setText("Cut"); // Paste Button Button pasteButton = new Button(shell, SWT.PUSH); pasteButton.setText("Paste"); clearButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(""); text.forceFocus(); } }); copyButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.copy(); text.forceFocus(); } }); cutButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.cut(); text.forceFocus(); } }); pasteButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.paste(); text.forceFocus(); } }); shell.setSize(400, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }