View on GitHub

Eclipse Tutorial

Ecsoya

online tutorialsWIKIoffline tutorialsPPT

Scale Tutorial

刻度条(Scale)比较简单,分为水平的(SWT.HORIZONTAL)和垂直的(SWT.VERTICAL)两种。

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

    shell.setText("Scale Tutorial");
    shell.setSize(300, 200);

    shell.setLayout(new RowLayout(SWT.VERTICAL));

    Scale vScale = new Scale(shell, SWT.VERTICAL);

    Scale hScale = new Scale(shell, SWT.HORIZONTAL);

    shell.open();

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

如图:


需要注意的问题


事件监听

1. Selection通过addSelectionListener(SelectionListener)或addListener(SWT.Selection, Listener)添加。

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

    shell.setText("Scale Tutorial");
    shell.setSize(300, 200);

    shell.setLayout(new GridLayout());

    final Label label = new Label(shell, SWT.NONE);
    label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final Scale scale = new Scale(shell, SWT.HORIZONTAL);
    scale.setMaximum(100);
    scale.setPageIncrement(10);
    scale.setIncrement(5);
    scale.setSelection(20);
    scale.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            label.setText("Selection: " + scale.getSelection());
        }
    });
    scale.setFocus();
    shell.open();

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

参考资料: * Scale snippets * 如果想了解更多的关于设置颜色,字体等其它属性的相关内容,请移步至控件的通用设置 * 如果想了解更多的关于LayoutLayoutData的相关内容,请移步至布局管理器 * SWT Example: ControlExample * Sample code and further information


Back to Home 上一篇:Combo Tutorial 下一篇:Slider Tutorial