最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2009年06月25日 11:29:32   作者:  
有的時(shí)候Text需要實(shí)現(xiàn)這種那種的樣式。先提供在不使用StyledText類(lèi)的情況:
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類(lèi)進(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();
}
}

相關(guān)文章

  • Java中的引用和動(dòng)態(tài)代理的實(shí)現(xiàn)詳解

    Java中的引用和動(dòng)態(tài)代理的實(shí)現(xiàn)詳解

    這篇文章主要介紹了Java中的引用和動(dòng)態(tài)代理的實(shí)現(xiàn)詳解,涉及Java中的引用類(lèi)型,JVMGC的可達(dá)性分析,代理模式等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • springboot?ConfigurationProperties的綁定源碼示例解析

    springboot?ConfigurationProperties的綁定源碼示例解析

    這篇文章主要為大家介紹了springboot?ConfigurationProperties的綁定源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Spring Cloud Consul實(shí)現(xiàn)選舉機(jī)制的代碼工程

    Spring Cloud Consul實(shí)現(xiàn)選舉機(jī)制的代碼工程

    Spring Cloud Consul 是 Spring Cloud 提供的對(duì) HashiCorp Consul 的支持,它是一種基于服務(wù)網(wǎng)格的工具,用于實(shí)現(xiàn)服務(wù)注冊(cè)、發(fā)現(xiàn)、配置管理和健康檢查,本文給大家介紹了如何用Spring Cloud Consul實(shí)現(xiàn)選舉機(jī)制,需要的朋友可以參考下
    2024-11-11
  • Springboot利用Redis實(shí)現(xiàn)接口冪等性攔截

    Springboot利用Redis實(shí)現(xiàn)接口冪等性攔截

    這篇文章主要為大家介紹了Springboot如何利用Redis實(shí)現(xiàn)接口冪等性攔截。本文將通過(guò)自定義注解+redis+攔截器+MD5?實(shí)現(xiàn),感興趣的可以了解一下
    2022-06-06
  • java處理解析帶有反斜杠的json

    java處理解析帶有反斜杠的json

    在Java中操作JSON數(shù)據(jù)是一項(xiàng)常見(jiàn)的任務(wù),其中一個(gè)常見(jiàn)的問(wèn)題是如何在JSON字符串中包含反斜杠,本文主要介紹了java處理解析帶有反斜杠的json,感興趣的可以了解一下
    2024-01-01
  • SpringMVC前后端傳值的幾種實(shí)現(xiàn)方式

    SpringMVC前后端傳值的幾種實(shí)現(xiàn)方式

    本文主要介紹了SpringMVC前后端傳值的方式實(shí)現(xiàn),包括使用HttpServletRequest、HttpSession、Model和ModelAndView等方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-02-02
  • Java中HashMap的put過(guò)程詳解

    Java中HashMap的put過(guò)程詳解

    這篇文章主要介紹了Java中HashMap的put過(guò)程詳解,HashMap有4個(gè)構(gòu)造器,其他構(gòu)造器如果用戶(hù)沒(méi)有傳入initialCapacity?和loadFactor這兩個(gè)參數(shù),會(huì)使用默認(rèn)值一般如果new?HashMap()不傳值,需要的朋友可以參考下
    2023-07-07
  • SpringBoot 防御 CSRF 攻擊的流程及原理解析

    SpringBoot 防御 CSRF 攻擊的流程及原理解析

    CSRF是一種非常常見(jiàn)的Web攻擊方式,其實(shí)是很好防御的,但是由于經(jīng)常被很多開(kāi)發(fā)者忽略,進(jìn)而導(dǎo)致很多網(wǎng)站實(shí)際上都存在 CSRF 攻擊的安全隱患,這篇文章主要介紹了SpringBoot 如何防御 CSRF 攻擊,需要的朋友可以參考下
    2023-05-05
  • 基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲(chóng)

    基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲(chóng)

    這篇文章主要介紹了基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲(chóng),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • 面試官:詳細(xì)談?wù)凧ava對(duì)象的4種引用方式

    面試官:詳細(xì)談?wù)凧ava對(duì)象的4種引用方式

    這篇文章主要給大家介紹了java面試官常會(huì)問(wèn)到的,關(guān)于Java對(duì)象的4種引用方式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05

最新評(píng)論

炎陵县| 虞城县| 红安县| 璧山县| 崇信县| 射阳县| 张家港市| 镇坪县| 卫辉市| 通化县| 建始县| 贵南县| 尚志市| 台东市| 方山县| 北海市| 盐边县| 隆子县| 镇宁| 屯留县| 桦川县| 武义县| 永泰县| 鸡东县| 沛县| 鸡西市| 周至县| 长海县| 卢湾区| 邹平县| 高淳县| 雷波县| 祁东县| 中宁县| 扎兰屯市| 迁西县| 疏附县| 遂川县| 江都市| 宜都市| 含山县|