gpt4 book ai didi

java - SWT 创建两个串联滚动的 StyledText

转载 作者:行者123 更新时间:2023-11-30 07:08:27 28 4
gpt4 key购买 nike

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

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

最佳答案

您可以使用方法StyledText#setHorizo​​ntalPixel()StyledText#setTopPixel()(及其各自的get方法)来获取和设置位置:

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);
});
}

关于java - SWT 创建两个串联滚动的 StyledText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39636950/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com