以前以为Slider和Scale之间只是外观的区别,今天发现不是这样的,因为Slider有一个特点:getSelection()能得到的最 大值并不是getMaximum()的值,要减去getThumb()值,后者是中间的滑块所拥有的值,缺省为10,最小为1。运行这个程序观察控制台的 输出。
import
org.eclipse.swt.
*
;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets. * ;
public class SliderTest {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
// Slider
final Slider slider = new Slider(shell, SWT.HORIZONTAL);
slider.setBounds( 10 , 10 , 200 , 32 );
slider.setMinimum( 0 );
slider.setMaximum( 100 );
slider.setThumb( 5 );
slider.addSelectionListener( new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println( " Slider Selection: " + slider.getSelection());
}
});
// Scale
final Scale scale = new Scale(shell, SWT.HORIZONTAL);
scale.setBounds( 10 , 50 , 200 , 72 );
scale.setMinimum( 0 );
scale.setMaximum( 100 );
scale.addSelectionListener( new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println( " Scale Selection: " + scale.getSelection());
}
});
shell.open();
while ( ! shell.isDisposed()) {
if ( ! display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets. * ;
public class SliderTest {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
// Slider
final Slider slider = new Slider(shell, SWT.HORIZONTAL);
slider.setBounds( 10 , 10 , 200 , 32 );
slider.setMinimum( 0 );
slider.setMaximum( 100 );
slider.setThumb( 5 );
slider.addSelectionListener( new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println( " Slider Selection: " + slider.getSelection());
}
});
// Scale
final Scale scale = new Scale(shell, SWT.HORIZONTAL);
scale.setBounds( 10 , 50 , 200 , 72 );
scale.setMinimum( 0 );
scale.setMaximum( 100 );
scale.addSelectionListener( new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println( " Scale Selection: " + scale.getSelection());
}
});
shell.open();
while ( ! shell.isDisposed()) {
if ( ! display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
本文转自博客园八进制的博客,原文链接:SWT里Slider和Scale的区别,如需转载请自行联系原博主。