java - 防止在文本字段上滚动? Java SWT

标签 java text scroll swt scrolledcomposite

我在 ScrolledComposite 中有一个多行文本字段。如果我的鼠标位于文本字段之外,则滚动工作正常,但如果我的鼠标位于文本字段上,它将停止滚动 ScrolledComposite。我没有在文本字段上设置 V_Scroll,因此它不会滚动文本,而是向上或向下移动一点。我如何才能继续滚动 ScrolledComposite?平台:MacOS

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Application {

public static void main (String [] args)
{
    Display display = new Display ();
    Shell shell = new Shell (display);
    shell.setText("Application");
    shell.setLayout(new FillLayout());

    ScrolledComposite c1 = new ScrolledComposite(shell, SWT.BORDER | SWT.V_SCROLL);

    Composite content = new Composite(c1, SWT.NONE);
    content.setLayout(new GridLayout(1,false));
    Text field = new Text(content, SWT.BORDER | SWT.MULTI | SWT.WRAP);
    field.setText("\n\n\n\n\nsome text some text some text some text some text some text\n\n\n\n");

    c1.setContent(content);
    
    c1.setExpandHorizontal(true);
    c1.setExpandVertical(true);
    c1.setMinSize(600, 600);
    
    shell.setSize(600, 300);
    shell.open ();
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}

}

最佳答案

添加以下内容:

ScrollBar vbar = c1.getVerticalBar();
field.addMouseWheelListener(new MouseWheelListener(){
  public void mouseScrolled(MouseEvent e){
    int pos = vbar.getSelection();
    int increment = vbar.getIncrement() * e.count;
    pos -= increment;
    if (pos < vbar.getMinimum()){
      pos = vbar.getMinimum();
    }
    if (pos > vbar.getMaximum()){
      pos = vbar.getMaximum();
    }
    vbar.setSelection(pos);
    c1.setOrigin(0,pos);
  }
});

您必须将监听器添加到组合中的每个(可滚动)小部件中。另外,这并没有考虑到如果文本字段上有滚动条会发生什么,尽管这也可以处理。

关于java - 防止在文本字段上滚动? Java SWT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62611247/

相关文章:

java - Spring 启动 : accessing command line args from a bean

java - Intent 发送 MenuItem 或任何对象到启动 Activity

java - 使用 UTF-16 将文本绘制到 Canvas

javascript - 如何在 Canvas 上绘制适合给定矩形的文本?

javascript - 如果有办法做 background-size : cover and still be able to scroll around the image

java - JFreeChart 使用将一个系列移动给定的偏移值

java - 在 Java EE 7 应用程序中使用 ThreadPoolExecutor 的 Drools 导致重新部署时出现问题

javascript - 在文本框中概述用户文本输入

javascript - 用户交互后取消滚动

Javascript:检测 OS X "natural scroll"设置