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();
}
}
相关文章
- java swt griddata_SWT的GridData一些参数的图示
- Android TV androidx.leanback:leanback 开箱体验
- MySQL 5.5绿色版:全新体验,让更多人轻松上手!(mysql5.5绿色版)
- Linux上的网络浏览体验(linux的浏览器)
- 体验Linux之美:字符替换的秘密(linux字符替换)
- 足足8项新功能 安卓版微信8.0.9快速体验
- MySQL PHP实现安全登录体验(mysqlphp登陆)
- MySQL最新版本:极致数据库体验(mysql最高版本)
- Redis管理更轻松,使用可视化工具体验提升(redis可视化管理工具)
- 讯飞: Linux 上的智能体验(讯飞forlinux)
- 探索Redis周边一次性体验(查找redis附近的人)
- Redis优化网站静态页体验(redis 静态页)
- Redis读写分离极致用户体验的中间件之路(redis读写分离中间件)
- Oracle8i 的强大管理功能带来了更强大体验(oracle8irman)
- 【妹测】悄然亮剑,美的 H32 空气净化器体验
- SWT(JFace)体验之打开多个Form
- SWT(JFace)体验之StackLayout布局
- SWT(JFace)体验之Slider,Scale