ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-06-02 16:02:24  阅读:250  来源: 互联网

标签:shell setSelection Text eclipse swt text1 new SWT


在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' + " 注意:文本项不能为空!!!……");
                } else
                    text3.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();
  }
}

标签:shell,setSelection,Text,eclipse,swt,text1,new,SWT
来源: https://www.cnblogs.com/ant-xu/p/10963175.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有