当前位置:编程学习 > 网站相关 >>

Vaadin Web应用开发教程(25):UI组件-Slider组件

Slider组件可以显示为垂直或是水平滑动条,可以使用鼠标拖动来设置其值。 其基本使用如下:

[java]
// Create a vertical slider  
final Slider vertslider = new Slider(1, 100); 
vertslider.setOrientation(Slider.ORIENTATION_HORIZONTAL); 
 
 
// Shows the value of the vertical slider  
final Label vertvalue = new Label(); 
vertvalue.setSizeUndefined(); 
 
// Handle changes in slider value.  
vertslider.addListener(new Property.ValueChangeListener() { 
 
    public void valueChange( 
            com.vaadin.data.Property.ValueChangeEvent event) { 
         double value = (Double) vertslider.getValue(); 
 
        vertvalue.setValue(String.valueOf(value)); 
         
    } 
}); 
  
// The slider has to be immediate to send the changes  
// immediately after the user drags the handle.  
vertslider.setImmediate(true); 

// Create a vertical slider
final Slider vertslider = new Slider(1, 100);
vertslider.setOrientation(Slider.ORIENTATION_HORIZONTAL);


// Shows the value of the vertical slider
final Label vertvalue = new Label();
vertvalue.setSizeUndefined();

// Handle changes in slider value.
vertslider.addListener(new Property.ValueChangeListener() {

 public void valueChange(
   com.vaadin.data.Property.ValueChangeEvent event) {
   double value = (Double) vertslider.getValue();

  vertvalue.setValue(String.valueOf(value));
  
 }
});
 
// The slider has to be immediate to send the changes
// immediately after the user drags the handle.
vertslider.setImmediate(true);

 

 

Slider组件也属于Field组件,因此可以通过ValueChangeListener来监听Slider组件值的变化。同样可以使用setValue 来修改Slider组件的值,此时要注意Catch可能的ValueOutOfBoundsException。

[java] 
// Set the initial value. This has to be set after the  
// listener is added if we want the listener to handle  
// also this value change.  
try { 
 vertslider.setValue(50.0); 
} catch (ValueOutOfBoundsException e) { 

// Set the initial value. This has to be set after the
// listener is added if we want the listener to handle
// also this value change.
try {
 vertslider.setValue(50.0);
} catch (ValueOutOfBoundsException e) {
}

 

 


补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,