`

SWT(JFace)体验之StyledText类

    博客分类:
  • JAVA
阅读更多
有的时候Text需要实现这种那种的样式。    
   
先提供在不使用StyledText类的情况:    
   
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.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    
   
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    
   
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类进行修饰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();    
 }    
}    
   
分享到:
评论

相关推荐

    SWT JFACE 编程笔记

    SWT JFACE 编程笔记 , 比较适合刚 解除 java swt jface 的开发人员

    Eclipse SWT JFace核心应用_pdf_含标签_目录

    《Eclipse SWT/Jface核心应用》全面介绍了SWT、JFace和RCP的相关知识。全书共分5篇,第1篇介绍了SWT产生的背景以及SWT的一些基本概念和基础知识。第2篇介绍了SWT基本控件的使用,以及事件处理、布局等SWT基本知识的...

    Eclipse SWT JFace核心应用

    Eclipse SWT JFace核心应用 PDF

    配套源码_可视化JAVA SWT JFACE GUI程序设计教程

    JFace 建立在 SWT 之上,提供了一层抽象,简化了开发过程,使得代码更简洁、可读性更强。JFace 引入了一些高级控件和数据模型,如视图、对话框、表编辑器等,并且实现了事件处理和数据绑定机制。通过使用 JFace,...

    SWT.rar_java swt_jface api_swt api_swt jface_swt jface 3.4 api

    JFace则是建立在SWT之上的高级抽象层,它简化了GUI开发的复杂性,提供了更简洁的API。 在"SWT-JFace-3.4-API"这个压缩包中,包含了SWT和JFace的3.4版本的API文档。这些文档对于开发者来说是非常重要的参考资料,...

    SWT/JFace专题 --- SWT/JFace概述

    JFace 建立在SWT之上,提供了一层抽象,简化了GUI开发的复杂性。JFace的目标是通过提供数据绑定、视图模型、对话框和服务等高级构造,来提高开发者的生产力。例如,它使用模型-视图-控制器(MVC)架构,将数据管理与...

    Swt JFace in Action 中文版

    JFace是Eclipse项目中的另一重要组件,它建立在Swt之上,为开发者提供了一层抽象,简化了UI的构建过程。JFace提供了一些高级组件,如视图(View)、编辑器(Editor)、对话框(Dialog)和表单(Form)等,并且包含...

    Eclipse SWT JFace核心应用 PDF.part2

    Eclipse SWT JFace核心应用 Eclipse SWT JFace核心应用

    SWT JFACE in Action(中文)

    JFace则是建立在SWT之上的更高层次的抽象,旨在简化GUI开发的复杂性。JFace提供了一组高级组件、视图、对话框和事件处理机制,使得开发者可以专注于业务逻辑,而无需过多关注底层的SWT细节。例如,JFace的数据绑定...

    swt/jface.jar

    JFace是建立在SWT之上的高级UI框架,简化了UI组件的创建和管理,提供了数据绑定、事件处理和视图模型等高级功能。 描述中提到的"swt/Jface用到的最基本的jar包"暗示了这个jar文件是构建基于SWT和JFace的应用程序的...

    Eclipse SWT JFace 核心应用

    声明:该文件不能以任何形式用于商业用途,仅供个人学习使用,如有侵权请联系我删除。如有不便,敬请原谅。

    SWT Jface知识点

    SWT Jface知识点,Eclipse SWT/JFace核心应用

    swt_jface.jar

    使用swt jface图形界面技术需要用到的jar文件。Swt,jface是一款非常优秀的javaUI库

    Eclipse SWT JFace核心应用.pdf

    Eclipse SWT JFace核心应用.pdf

    SWT_JFace.rar_ImageAnalyzer_SWT-jface_swt jface

    JFace,则是建立在 SWT 之上的更高层次的抽象,它简化了 SWT 的使用,减少了代码的复杂性。JFace 提供了数据绑定、视图、对话框等高级服务,使得开发者能够更专注于业务逻辑,而不是底层的 GUI 细节。JFace 还引入了...

    SWT JFace 3.4 API

    SWT/JFace 3.4 版本的 API,CHM格式,方便win平台开发人员查阅。

Global site tag (gtag.js) - Google Analytics