【问题标题】:Thumb on Scale widget unresponsiveThumb on Scale 小部件无响应
【发布时间】:2012-10-10 22:18:32
【问题描述】:

在 Mac 上使用 SWT。我创建了一个统一的工具栏。在此工具栏上有一个 Scale 小部件和一个 Label 小部件。标签显示刻度的当前值,由刻度上的 SelectionListener 更新

程序启动时,比例小部件的拇指不会移动。标签显示值的变化完全符合预期,Scale 小部件正确跟踪光标移动,并报告更改的值。拇指不动。

关闭统一工具栏并重新打开它(使用右上角的小按钮)使拇指完全可操作。拇指跟踪光标。

简单、可编译、可运行的测试代码复制问题来了:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.*;


public class scaleTest
    {
        private static Shell shell;
        private static Display display;
        private ToolBar UnifiedToolBar;
        private Scale scale;


        private scaleTest()
            {
                UnifiedToolBar = shell.getToolBar();

            ToolItem containScale = new ToolItem( UnifiedToolBar, SWT.SEPARATOR );
            containScale.setWidth( 200 );

            scale = new Scale( UnifiedToolBar, SWT.HORIZONTAL );
            scale.setMaximum( 72 );
            scale.setSelection( 2 );
            scale.setMinimum( 6 );
            scale.setIncrement( 4 );
            scale.setPageIncrement( 4 );
            scale.setSize( 180, 24 );
            containScale.setControl( scale );


            ToolItem containLabel = new ToolItem( UnifiedToolBar, SWT.SEPARATOR );
            containLabel.setWidth( 20 );
            final Label label = new Label( UnifiedToolBar, SWT.NONE );
            label.setText( "32" );
            containLabel.setControl( label );


            scale.addSelectionListener( new SelectionAdapter()
            {
                @Override
                public void widgetSelected( SelectionEvent selectionEvent )
                    {
                        label.setText( String.valueOf( scale.getSelection() ) );

                    }
            } ); // end addSelectionListener


        } // end of constructor


    public static void main( String[] args )
        {


            display = Display.getDefault();
            shell = new Shell( display );


            shell.setText( "scaleTest App" );
            shell.setSize( 400, 200 );
            shell.setLocation( (display.getClientArea().width / 2) - 200
                                                    ,(display.getClientArea().height / 2) - 100 );
            shell.setLayout( new FormLayout() );


            scaleTest testExample = new scaleTest();

            shell.open();
            while( !shell.isDisposed() )
                {
                    if( !display.readAndDispatch() )
                        display.sleep();
                }
            display.dispose();
        }
} // end scaleTest class

我已经在外壳、工具栏和 Scale 小部件上尝试了 layout()、layout(true, true)、redraw()、paint()、pack() 等多种看似合理的组合。一个正常人会认为这是一个不合理的大量组合。

问题一:如何让拇指开机正常工作?

跟进问题,重要性要小得多:

问题 2:Scale 小部件似乎忽略了 pageIncrement 和 increment 设置。为什么?

非常感谢任何帮助。

更新:深夜玩耍。将 Scale 小部件移动到外壳 - 并且对上面包含的测试代码没有其他更改,小部件可以按需要工作 - thumb 可以立即工作。乍一看,它会看到 Scale 和统一的工具栏不太喜欢对方。

【问题讨论】:

  • @Question2:你的意思是可以用鼠标选择每个值吗?如果是这样,这取决于操作系统。在 Linux 上也是如此,而在 Windows 上,天平将“捕捉”预定义的步骤。
  • 是的 - 每个值。感谢您解开这个谜团!

标签: java macos widget swt


【解决方案1】:

问题 2

您可以通过将Listener 添加到SWT.Selection 来实现对步进值的“捕捉”。在下面的代码中,它将捕捉到5 的所有倍数:

scale.addListener(SWT.Selection, new Listener() {
    public void handleEvent(Event event) {
        // get the current selection and "round" it to the next 5er step
        int scaleValue = scale.getSelection();
        scaleValue = (int)(Math.round(scaleValue / 5.0) * 5);

        // update the label
        label.setText("" + (scaleValue));
        label.pack();

        // update the scale selection
        scale.setSelection(scaleValue);
    }
});

不过,我无法真正帮助您解决问题 1...

【讨论】:

  • 是的——好主意。我没有想到这一点,但这是个好主意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多