麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

SWT(JFace)體驗(yàn)之StyledText類

2020-01-31 16:51:32
字體:
供稿:網(wǎng)友
WrapLines.java
復(fù)制代碼 代碼如下:

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.setText(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 GridData(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
復(fù)制代碼 代碼如下:

package 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
復(fù)制代碼 代碼如下:

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 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(new 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類進(jìn)行修飾Text的幾個(gè)例子:

HighlightOddLine.java
復(fù)制代碼 代碼如下:

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
復(fù)制代碼 代碼如下:

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
復(fù)制代碼 代碼如下:

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
復(fù)制代碼 代碼如下:

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();
}
}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 中文字幕在线观看视频www | 国产一级毛片高清视频完整版 | 黄污网站在线 | 成人三级免费电影 | 国产人成免费爽爽爽视频 | 国产无遮挡一区二区三区毛片日本 | 久久精品无码一区二区日韩av | 成年人黄色片视频 | 久在线观看福利视频69 | 4480午夜| 亚洲一区二区三区视频 | 国产在线一级片 | 久草在线视频福利 | 国产精品一区久久久久 | 国产精品一区在线观看 | 全黄毛片 | 在线成人看片 | 久久国产亚洲视频 | 欧美精品a∨在线观看不卡 午夜精品影院 | 99在线免费观看视频 | 久久激情国产 | 精品99在线视频 | 成人国产精品色哟哟 | 一级黄色大片在线观看 | 成人福利在线免费观看 | 午夜视频在线免费观看 | 精品视频一区二区三区四区 | 情侣啪啪网站 | 久草手机视频在线观看 | 色淫网站免费视频 | 国产在线区 | 成人毛片免费视频 | 久久精品国产清自在天天线 | 久久久久久麻豆 | 极品国产91在线网站 | 久久精品亚洲一区 | 日韩毛片免费观看 | 久久久麻豆 | 国产一区二区精品在线观看 | 福利在线免费 | 91精品国产日韩91久久久久久360 |