zl程序教程

您现在的位置是:首页 >  其它

当前栏目

SWT(JFace)体验之StyledText类

体验 swt JFace
2023-06-13 09:14:10 时间
WrapLines.java
复制代码代码如下:

packageswt_jface.demo4;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.layout.GridData;
importorg.eclipse.swt.layout.GridLayout;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Label;
importorg.eclipse.swt.widgets.Shell;
importorg.eclipse.swt.widgets.Text;
publicclassWrapLines{

Displaydisplay=newDisplay();
Shellshell=newShell(display);

Texttext1;
Texttext2;
Stringline="abcdefghijklmnopqrstuvwxyz0123456789";

privatevoidinit(){

text1=newText(shell,SWT.BORDER|SWT.MULTI);
//text.setTextLimit(12);
text1.setText(line);
text2=newText(shell,SWT.BORDER|SWT.WRAP);
text2.setText(line);
}
publicWrapLines(){

shell.setLayout(newGridLayout(2,true));
(newLabel(shell,SWT.NULL)).setText("SWT.BORDER|\nSWT.MUTLI");
(newLabel(shell,SWT.NULL)).setText("SWT.BORDER|\nSWT.WRAP");
init();
GridDatagridData=newGridData(GridData.FILL_BOTH);
text1.setLayoutData(gridData);

gridData=newGridData(GridData.FILL_BOTH);
text2.setLayoutData(gridData);
shell.pack();
shell.open();

while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
publicstaticvoidmain(String[]args){
newWrapLines();
}
}

RemarksText.java
复制代码代码如下:

packageswt_jface.demo4;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.events.ModifyEvent;
importorg.eclipse.swt.events.ModifyListener;
importorg.eclipse.swt.events.VerifyEvent;
importorg.eclipse.swt.events.VerifyListener;
importorg.eclipse.swt.layout.GridData;
importorg.eclipse.swt.layout.GridLayout;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Label;
importorg.eclipse.swt.widgets.Shell;
importorg.eclipse.swt.widgets.Text;
publicclassRemarksText{

Displaydisplay=newDisplay();
Shellshell=newShell(display);

Texttext;
publicRemarksText(){

shell.setLayout(newGridLayout(1,false));
(newLabel(shell,SWT.NULL)).setText("Remarks:");
text=newText(shell,SWT.MULTI|SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
text.setText("123456789");
text.setLayoutData(newGridData(GridData.FILL_BOTH));
System.out.println("getText:"+text.getText(1,6));
char[]chars=text.getLineDelimiter().toCharArray();
for(inti=0;i<chars.length;i++){
System.out.println("Linedelimiter#"+i+":"+Integer.toHexString(chars[i]));
}
text.getOrientation();
System.out.println("Numberofchars:"+text.getCharCount());
System.out.println("Tabs:"+text.getTabs());
text.addVerifyListener(newVerifyListener(){
publicvoidverifyText(VerifyEvente){
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(newModifyListener(){
publicvoidmodifyText(ModifyEvente){
System.out.println("Newcharactercount:"+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();
}
publicstaticvoidmain(String[]args){
newRemarksText();
}
}

再比如密码框:
UserPassword.java
复制代码代码如下:
packageswt_jface.demo4;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.layout.GridData;
importorg.eclipse.swt.layout.GridLayout;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Label;
importorg.eclipse.swt.widgets.Shell;
importorg.eclipse.swt.widgets.Text;
publicclassUserPassword{

Displaydisplay=newDisplay();
Shellshell=newShell(display);

TexttextUser;
TexttextPassword;
privatevoidinit(){

(newLabel(shell,SWT.NULL)).setText("Username:");
textUser=newText(shell,SWT.SINGLE|SWT.BORDER);
textUser.setText("default_user");
textUser.setTextLimit(16);
(newLabel(shell,SWT.NULL)).setText("Password:");
textPassword=newText(shell,SWT.SINGLE|SWT.BORDER);
System.out.println(textPassword.getEchoChar());
textPassword.setEchoChar("*");
}

publicUserPassword(){

shell.setLayout(newGridLayout(2,false));
init();

textUser.setLayoutData(newGridData(GridData.FILL_HORIZONTAL));
textPassword.setLayoutData(newGridData(GridData.FILL_HORIZONTAL));

shell.pack();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
publicstaticvoidmain(String[]args){
newUserPassword();
}
}

下面演示使用StyledText类进行修饰Text的几个例子:

HighlightOddLine.java
复制代码代码如下:
packageswt_jface.demo4;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.custom.LineBackgroundEvent;
importorg.eclipse.swt.custom.LineBackgroundListener;
importorg.eclipse.swt.custom.StyledText;
importorg.eclipse.swt.layout.GridData;
importorg.eclipse.swt.layout.GridLayout;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassHighlightOddLine{

Displaydisplay=newDisplay();
Shellshell=newShell(display);

StyledTextstyledText;

publicHighlightOddLine(){

shell.setLayout(newGridLayout());
styledText=newStyledText(shell,SWT.MULTI|SWT.WRAP|SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
styledText.setLayoutData(newGridData(GridData.FILL_BOTH));
styledText.addLineBackgroundListener(newLineBackgroundListener(){
publicvoidlineGetBackground(LineBackgroundEventevent){
if(styledText.getLineAtOffset(event.lineOffset)%2==1)
event.lineBackground=shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
}
});

styledText.setText("Line0\r\nLine1\r\nLine2\r\nLine3\r\nLine4\r\nLine5\r\nLine6");
shell.setSize(300,150);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
publicstaticvoidmain(String[]args){
newHighlightOddLine();
}
}

SetLineBackground.java
复制代码代码如下:
packageswt_jface.demo4;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.custom.StyleRange;
importorg.eclipse.swt.custom.StyledText;
importorg.eclipse.swt.graphics.Font;
importorg.eclipse.swt.layout.GridData;
importorg.eclipse.swt.layout.GridLayout;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassSetLineBackground{

Displaydisplay=newDisplay();
Shellshell=newShell(display);

StyledTextstyledText;
publicSetLineBackground(){
shell.setLayout(newGridLayout());
styledText=newStyledText(shell,SWT.BORDER|SWT.MULTI|SWT.WRAP|SWT.H_SCROLL|SWT.V_SCROLL);
styledText.setLayoutData(newGridData(GridData.FILL_BOTH));
Fontfont=newFont(shell.getDisplay(),"CourierNew",12,SWT.NORMAL);
styledText.setFont(font);
styledText.setText("abcdefg\r\nhijklmn");
StyleRangestyleRange1=newStyleRange();
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();
}

publicstaticvoidmain(String[]args){
newSetLineBackground();
}
}

SearchStyleText.java
复制代码代码如下:
packageswt_jface.demo4;
importjava.util.LinkedList;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.custom.LineStyleEvent;
importorg.eclipse.swt.custom.LineStyleListener;
importorg.eclipse.swt.custom.StyleRange;
importorg.eclipse.swt.custom.StyledText;
importorg.eclipse.swt.events.SelectionAdapter;
importorg.eclipse.swt.events.SelectionEvent;
importorg.eclipse.swt.graphics.Font;
importorg.eclipse.swt.layout.GridData;
importorg.eclipse.swt.layout.GridLayout;
importorg.eclipse.swt.widgets.Button;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
importorg.eclipse.swt.widgets.Text;
publicclassSearchStyleText{

Displaydisplay=newDisplay();
Shellshell=newShell(display);
StyledTextstyledText;
TextkeywordText;
Buttonbutton;

Stringkeyword;

publicSearchStyleText(){

shell.setLayout(newGridLayout(2,false));
styledText=newStyledText(shell,SWT.MULTI|SWT.WRAP|SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
GridDatagridData=newGridData(GridData.FILL_BOTH);
gridData.horizontalSpan=2;
styledText.setLayoutData(gridData);
keywordText=newText(shell,SWT.SINGLE|SWT.BORDER);
keywordText.setLayoutData(newGridData(GridData.FILL_HORIZONTAL));
Fontfont=newFont(shell.getDisplay(),"CourierNew",12,SWT.NORMAL);
styledText.setFont(font);
button=newButton(shell,SWT.PUSH);
button.setText("Search");
button.addSelectionListener(newSelectionAdapter(){
publicvoidwidgetSelected(SelectionEvente){
keyword=keywordText.getText();
styledText.redraw();
}
});

styledText.addLineStyleListener(newLineStyleListener(){
publicvoidlineGetStyle(LineStyleEventevent){
if(keyword==null||keyword.length()==0){
event.styles=newStyleRange[0];
return;
}
Stringline=event.lineText;
intcursor=-1;
LinkedListlist=newLinkedList();
while((cursor=line.indexOf(keyword,cursor+1))>=0){
list.add(getHighlightStyle(event.lineOffset+cursor,keyword.length()));
}
event.styles=(StyleRange[])list.toArray(newStyleRange[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();
}

privateStyleRangegetHighlightStyle(intstartOffset,intlength){

StyleRangestyleRange=newStyleRange();
styleRange.start=startOffset;
styleRange.length=length;
styleRange.background=shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
returnstyleRange;
}
publicstaticvoidmain(String[]args){
newSearchStyleText();
}
}

SampleStyledText.java
复制代码代码如下:
packageswt_jface.demo4;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.custom.StyleRange;
importorg.eclipse.swt.custom.StyledText;
importorg.eclipse.swt.graphics.Font;
importorg.eclipse.swt.layout.GridData;
importorg.eclipse.swt.layout.GridLayout;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;
publicclassSampleStyledText{

Displaydisplay=newDisplay();
Shellshell=newShell(display);
StyledTextstyledText;
publicSampleStyledText(){

shell.setLayout(newGridLayout());
styledText=newStyledText(shell,SWT.MULTI|SWT.WRAP|SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
styledText.setLayoutData(newGridData(GridData.FILL_BOTH));
Fontfont=newFont(shell.getDisplay(),"CourierNew",12,SWT.NORMAL);
styledText.setFont(font);
styledText.setText("123456789\r\nABCDEFGHI");

StyleRangestyleRange1=newStyleRange();
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;

StyleRangestyleRange2=newStyleRange();
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(newStyleRange[]{styleRange1,styleRange2});
//styledText.setStyleRanges(newStyleRange[]{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(newStyleRange[]{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();
}

privateStringprintStyleRanges(StyleRange[]styleRanges){
if(styleRanges==null)
return"null";
elseif(styleRanges.length==0)
return"[]";
StringBuffersb=newStringBuffer();
for(inti=0;i<styleRanges.length;i++){
sb.append(styleRanges[i]+"\n");
}
returnsb.toString();
}
publicstaticvoidmain(String[]args){
newSampleStyledText();
}
}