首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SWT创建两个连续滚动StyledText

SWT创建两个连续滚动StyledText
EN

Stack Overflow用户
提问于 2016-09-22 18:46:36
回答 1查看 73关注 0票数 1

我必须创建两个StyledText滚动在一起,所有我得到的是使用ScrolledComposites创建相同的代码。我有使用StyledText的限制,因为我还将它用于其他目的。我想创建同样的东西link here,但使用StyledText

我尝试使用相同的代码用StyledText替换ScrolledComposites,但它不允许我使用setOrigin(x , y)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-22 20:04:31

您可以使用方法StyledText#setHorizontalPixel()StyledText#setTopPixel() (以及它们各自的get方法)来获取和设置位置:

代码语言:javascript
复制
public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    StyledText one = new StyledText(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    StyledText two = new StyledText(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);

    one.setAlwaysShowScrollBars(true);
    two.setAlwaysShowScrollBars(true);

    one.setText("Scroll scroll scroll\ndown down down\nto to to\nsee see see\nthe the the\nstyled styled styled\ntexts texts texts\nscroll scroll scroll\nin in in\ntandem tandem tandem");
    two.setText("Scroll scroll scroll\ndown down down\nto to to\nsee see see\nthe the the\nstyled styled styled\ntexts texts texts\nscroll scroll scroll\nin in in\ntandem tandem tandem");

    handleVerticalScrolling(one, two);
    handleHorizontalScrolling(one, two);

    shell.pack();
    shell.setSize(200, 100);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

private static void handleHorizontalScrolling(StyledText one, StyledText two)
{
    ScrollBar hOne = one.getHorizontalBar();
    ScrollBar hTwo = two.getHorizontalBar();

    hOne.addListener(SWT.Selection, e -> {
        int x = one.getHorizontalPixel();
        two.setHorizontalPixel(x);
    });
    hTwo.addListener(SWT.Selection, e -> {
        int x = two.getHorizontalPixel();
        one.setHorizontalPixel(x);
    });
}

private static void handleVerticalScrolling(StyledText one, StyledText two)
{
    ScrollBar vOne = one.getVerticalBar();
    ScrollBar vTwo = two.getVerticalBar();

    vOne.addListener(SWT.Selection, e ->
    {
        int y = one.getTopPixel();
        two.setTopPixel(y);
    });
    vTwo.addListener(SWT.Selection, e ->
    {
        int y = two.getTopPixel();
        one.setTopPixel(y);
    });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39636950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档