WrapLines.java
复制代码 代码如下:
package swt_jface.demo4;
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;
import org.eclipse.swt.widgets.Text;
public class WrapLines {
Display display = new Display();
Shell shell = new Shell(display);
Text text1;
Text text2;
String line = "abcdefghijklmnopqrstuvwxyz0123456789";
private void init() {
text1 = new Text(shell, SWT.BORDER | SWT.MULTI);
//text.setTextLimit(12);
text1.setText(line);
text2 = new Text(shell, SWT.BORDER | SWT.WRAP);
text2.setwww.devze.comText(line);
}
public WrapLines() {
shell.setLayout(new GridLayout(2, true));
(new Label(shell, SWT.NULL)).setText("SWT.BORDER |\nSWT.MUTLI");
(new Label(shell, SWT.NULL)).setText("SWT.BORDER |\nSWT.WRAP");
init();
GridData gridData = new GridData(GridData.FILL_BOTH);
text1.setLayoutData(gridData);
gridData = new
dbRFQGridData(GridData.FILL_BOTH);
text2.setLayoutData(gridData);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new WrapLines();
}
}
RemarksText.java
复制代码 代码如下:
packa
开发者_JAV培训ge swt_jface.demo4;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
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;
import org.eclipse.swt.widgets.Text;
public class RemarksText {
Display display = new Display();
Shell shell = new Shell(display);
Text text;
public RemarksText() {
shell.setLayout(new GridLayout(1, false));
(new Label(shell, SWT.NULL)).setText("Remarks:");
text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
text.setText("123456789");
text.setLayoutData(new GridData(GridData.FILL_BOTH));
System.out.println("getText: " + text.getText(1, 6));
char[] chars = text.getLineDelimiter().toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println("Line delimiter #" + i + ": " + Integer.toHexString(chars[i]) );
}
text.getOrientation();
System.out.println("Number of chars: " + text.getCharCount());
System.out.println("Tabs: " + text.getTabs());
text.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent e) {
if(e.end == e.start) {
if( e.character == ' ' && (e.stateMask & SWT.CTRL) != 0 ) {
if(text.getText(e.end-1, e.end-1).equals("V")) {
e.text = "erifyListener";
}else{
e.doit = false;
}
}
}
}
});
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
System.out.println("New character count: " + text.getCharCount());
}
});
// text.append("a");
text.setSelection(1, 4);
System.out.println("getSelection:\t" + text.getSelection());
System.out.println("getSelectionCount:\t" + text.getSelectionCount());
System.out.println("getSelectionText:\t" + text.getSelectionText());
//text.insert("ABC");
// shell.pack();
shell.setSize(300, 150);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new RemarksText();
}
}
再比如密码框:
UserPassword.java
复制代码 代码如下:
package swt_jface.demo4;
import org.eclipse.swt.SWT;
import orgwww.devze.com.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;
import org.eclipse.swt.widgets.Text;
public class UserPassword {
Display display = new Display();
Shell shell = new Shell(display);
Text textUser;
Text textPassword;
private void init() {
(new Label(shell, SWT.NULL)).setText("User name: ");
textUser = new Text(shell, SWT.SINGLE | SWT.BORDER);
textUser.setText("default_user");
textUser.setTextLimit(16);
(new Label(shell, SWT.NULL)).setText("Password: ");
textPassword = new Text(shell, SWT.SINGLE | SWT.BORDER);
System.out.println(textPassword.getEchoChar());
textPassword.setEchoChar('*');
}
public UserPassword() {
shell.setLayout(new GridLayout(2, false));
init();
textUser.setLayoutData(newwww.devze.com GridData(GridData.FILL_HORIZONTAL));
textPassword.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new UserPassword();
}
}
下面演示使用StyledText类进行修饰Text的几个例子:
HighlightOddLine.java
复制代码 代码如下:
package swt_jface.demo4;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.LineBackgroundEvent;
import org.eclipse.swt.custom.LineBackgroundListener;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HighlightOddLine {
Display display 编程客栈= new Display();
Shell shell = new Shell(display);
StyledText styledText;
public HighlightOddLine() {
shell.setLayout(new GridLayout());
styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
styledText.addLineBackgroundListener(new LineBackgroundListener() {
public void lineGetBackground(LineBackgroundEvent event) {
if(styledText.getLineAtOffset(event.lineOffset) % 2 == 1)
event.lineBackground = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
}
});
styledText.setText("Line 0\r\nLine 1\r\nLine 2\r\nLine 3\r\nLine 4\r\nLine 5\r\nLine 6");
shell.setSize(300, 150);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new HighlightOddLine();
}
}
SetLineBackground.java
复制代码 代码如下:
package swt_jface.demo4;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SetLineBackground {
Display display = new Display();
Shell shell = new Shell(display);
StyledText styledText;
public SetLineBackground() {
shell.setLayout(new GridLayout());
styledText = new StyledText(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL);
styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
Font font = new Font(shell.getDisplay(), "Courier New", 12, SWT.NORMAL);
styledText.setFont(font);
styledText.setText("abcdefg\r\nhijklmn");
StyleRange styleRange1 = new StyleRange();
styleRange1.start = 2;
styleRange1.length = 3;
styleRange1.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
styleRange1.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
styleRange1.fontStyle = SWT.BOLD;
styledText.setStyleRange(styleRange1);
styledText.setLineBackground(0, 1, shell.getDisplay().getSystemColor(SWT.COLOR_GREEN));
styledText.setLineBackground(1, 1, shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW));
shell.setSize(300, 120);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new SetLineBackground();
}
}
SearchStyleText.java
复制代码 代码如下:
package swt_jface.demo4;
import java.util.LinkedList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.LineStyleEvent;
import org.eclipse.swt.custom.LineStyleListener;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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 SearchStyleText {
Display display = new Display();
Shell shell = new Shell(display);
StyledText styledText;
Text keywordText;
Button button;
String keyword;
public SearchStyleText() {
shell.setLayout(new GridLayout(2, false));
styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
styledText.setLayoutData(gridData);
keywordText = new Text(shell, SWT.SINGLE | SWT.BORDER);
keywordText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Font font = new Font(shell.getDisplay(), "Courier New", 12, SWT.NORMAL);
styledText.setFont(font);
button = new Button(shell, SWT.PUSH);
button.setText("Search");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
keyword = keywordText.getText();
styledText.redraw();
}
});
styledText.addLineStyleListener(new LineStyleListener() {
public void lineGetStyle(LineStyleEvent event) {
if(keyword == null || keyword.length() == 0) {
event.styles = new StyleRange[0];
return;
}
String line = event.lineText;
int cursor = -1;
LinkedList list = new LinkedList();
while( (cursor = line.indexOf(keyword, cursor+1)) >= 0) {
list.add(getHighlightStyle(event.lineOffset+cursor, keyword.length()));
}
event.styles = (StyleRange[]) list.toArray(new StyleRange[list.size()]);
}
});
keyword = "SW";
styledText.setText("AWT, SWING \r\nSWT & JFACE");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private StyleRange getHighlightStyle(int startOffset, int length) {
StyleRange styleRange = new StyleRange();
styleRange.start = startOffset;
styleRange.length = length;
styleRange.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
return styleRange;
}
public static void main(String[] args) {
new SearchStyleText();
}
}
SampleStyledText.java
复制代码 代码如下:
package swt_jface.demo4;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SampleStyledText {
Display display = new Display();
Shell shell = new Shell(display);
StyledText styledText;
public SampleStyledText() {
shell.setLayout(new GridLayout());
styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
Font font = new Font(shell.getDisplay(), "Courier New", 12, SWT.NORMAL);
styledText.setFont(font);
styledText.setText("123456789\r\nABCDEFGHI");
StyleRange styleRange1 = new StyleRange();
styleRange1.start = 2;
styleRange1.length = 16;
styleRange1.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
styleRange1.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
styleRange1.fontStyle = SWT.BOLD;
StyleRange styleRange2 = new StyleRange();
styleRange2.start = 14;
styleRange2.length = 3;
styleRange2.fontStyle = SWT.NORMAL;
styleRange2.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
styleRange2.background = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
// styledText.setStyleRange(styleRange1);
// styledText.setStyleRange(styleRange2);
//styledText.setStyleRanges(new StyleRange[]{styleRange1, styleRange2});
//styledText.setStyleRanges(new StyleRange[]{styleRange2, styleRange1});
//styledText.setLineBackground(1, 1, shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
// styledText.setSelection(4);
// System.out.println(printStyleRanges(styledText.getStyleRanges()) );
// styledText.insert("000");
System.out.println(printStyleRanges(styledText.getStyleRanges()) );
// styledText.setStyleRanges(new StyleRange[]{styleRange1});
// System.out.println(printStyleRanges(styledText.getStyleRanges()) );
//shell.pack();
shell.setSize(300, 120);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private String printStyleRanges(StyleRange[] styleRanges) {
if(styleRanges == null)
return "null";
else if(styleRanges.length == 0)
return "[]";
StringBuffer sb = new StringBuffer();
for(int i=0; i<styleRanges.length; i++) {
sb.append(styleRanges[i] + "\n");
}
return sb.toString();
}
public static void main(String[] args) {
new SampleStyledText();
}
}
精彩评论