热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

swt中关于Text.setSelection()的记录

在Text中setSelect()的作用是选中文本,和按住鼠标左键一直移动的选中的结果一样selectAll()是全选如果直接使用,是看不到效果的&#x

在Text中setSelect()的作用是选中文本,和按住鼠标左键一直移动的选中的结果一样

selectAll()是全选

如果直接使用,是看不到效果的,需要结合setFocus()一起使用。两者的顺序不影响实际的结果。

测试使用的代码1

package test;import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.jface.dialogs.*;
public class testlabel {public testlabel() {final Display display = Display.getDefault();final Shell shell = new Shell();shell.setSize(370, 235);shell.setText("Text 综合实例");final Label label1 = new Label(shell, SWT.NONE);label1.setBounds(25, 25, 60, 25);label1.setText("User Name:");final Text text1 = new Text(shell, SWT.BORDER);text1.setBounds(90, 20, 80, 20);// 当光标停留在该文本框时将出现提示信息text1.setToolTipText("文本项不能为空");text1.selectAll();final Label labe2 = new Label(shell, SWT.NONE);labe2.setBounds(190, 25, 55, 25);labe2.setText("PassWord:");final Text text2 = new Text(shell, SWT.PASSWORD | SWT.BORDER);text2.setBounds(250, 20, 80, 20);// setTextLimit(int x)为常用组件方法,用来设置文本框中最多可输入的字符数。text2.setTextLimit(8);text2.setToolTipText("文本项不能为空,且输入不超过8 位密码");text2.showSelection();final Text text3 = new Text(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);text3.setBounds(50, 55, 250, 90);final Button button1 = new Button(shell, SWT.NONE);button1.setBounds(140, 160, 80, 20);button1.setText("OK");button1.setToolTipText("单击OK 按钮,姓名将显示在下面的文本中");button1.addSelectionListener(new SelectionAdapter() {public void widgetSelected(SelectionEvent e) {String AD = text1.getText();// 获得输入的文本内容。String BD = text2.getText();// 判断输入文本是否为空if (AD == null || AD.equals("") || BD == null || BD.equals("")){MessageDialog.openInformation(shell, " 信息提示", "失败信息!"+ '\n' + '\n' + " 注意:文本项不能为空!!!……");} elsetext3.append("User Name: " + '\n' + "" + AD + '\n'+ "PassWord:" + '\n' + "" + BD);/*** append()方法用来在文本框中显示内容。* 将append()方法改为insert()方法可达到同样的效果*/}});final Button button2 = new Button(shell, SWT.NONE);button2.setBounds(250, 160, 80, 20);button2.setText("Cancel");button2.setToolTipText("单击Cancel 按钮,清除文本中的内容");button2.addSelectionListener(new SelectionAdapter() {public void widgetSelected(SelectionEvent e) {text1.setFocus();text1.setSelection(0,1); //测试的地方//text1.clearSelection();// 清除文本内容方法//text1.showSelection();text2.setText("");text3.setText("");}});shell.open();shell.layout();while (!shell.isDisposed()) {if (!display.readAndDispatch())display.sleep();}}public static void main(String[] args) {new testlabel();}
}

在输入内容后点击Cancel,结果为第一个字被选中

当使用text1.clearSelection()后,会取消选中显示光标,截不出来图,在“阿”后面即第一个位置,阿前是第0个位置。

最后一个是showSelection(),从官网找来的说明https://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fwidgets%2FText.html

即显示选中的内容,如果没有显示则通过滚动到选中位置来显示。

showSelection

public void showSelection()

Shows the selection.

If the selection is already showing in the receiver, this method simply returns. Otherwise, lines are scrolled until the selection is visible.

Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

还有一个例子http://www.java2s.com/Code/JavaAPI/org.eclipse.swt.widgets/TextclearSelection.htm

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;public class MainClass {public static void main(String[] a) {Display d = new Display();Shell s = new Shell(d);Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);text1.setBounds(100, 50, 100, 20);text1.setTextLimit(5);text1.setText("12345");Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);text2.setBounds(100, 75, 100, 20);text2.setTextLimit(30);FocusListener focusListener = new FocusListener() {public void focusGained(FocusEvent e) {Text t = (Text) e.widget;t.selectAll();}public void focusLost(FocusEvent e) {Text t = (Text) e.widget;if (t.getSelectionCount() > 0) {t.clearSelection();}}};text1.addFocusListener(focusListener);text2.addFocusListener(focusListener);s.open();while (!s.isDisposed()) {if (!d.readAndDispatch())d.sleep();}d.dispose();}
}

 

还有一个getSelection()

public Point getSelection()

Returns a Point whose x coordinate is the character position representing the start of the selected text, and whose y coordinate is the character position representing the end of the selection. An "empty" selection is indicated by the x and y coordinates having the same value.

Indexing is zero based. The range of a selection is from 0..N where N is the number of characters in the widget.

Returns:
a point representing the selection start and end
Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

转载于:https://www.cnblogs.com/ant-xu/p/10963175.html


推荐阅读
author-avatar
蜜爱女_201
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有