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

Prevent scrolling on text field? Java SWT

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

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