我正在为我的项目所需的java swt创建一个自定义滑块小部件。我的目标是创建一个控制3个数量的滑块。我实际上已经设计了GUI,但功能给了我一些问题。滑块包含2个要滑动的按钮,其中的错误是两个按钮不能相互独立工作。 所以请帮助找到纠正我的错误。
public class Snippet46 {
public static void main (String [] args) {
Display display = new Display ();
final Shell shell = new Shell (display);
final Canvas canvas = new Canvas(shell, SWT.BORDER);
canvas.setBounds(84, 70, 56, 195);
final Composite composite = new Composite (canvas, SWT.BORDER);
composite.setEnabled (false);
composite.setLayout (new FillLayout ());
Button button = new Button (composite, SWT.PUSH);
button.setText ("1");
composite.pack ();
composite.setLocation (0,10);
System.out.println("com:"+composite.getBounds());
composite.setSize(52, 19);
final Composite composite_1 = new Composite(canvas, SWT.BORDER);
composite_1.setEnabled(false);
composite_1.setLayout(new FillLayout());
Button btnNewButton = new Button(composite_1, SWT.PUSH);
btnNewButton.setText("2");
composite_1.pack();
composite_1.setLocation(0, 162);
composite_1.setSize(52, 19);
final int default_y_up=10;
final int default_y_down=162;
final Point [] offset = new Point [1];
Listener listener = new Listener () {
public void handleEvent (Event event) {
Rectangle rect = canvas.getBounds ();
Rectangle rect_canvas=composite.getBounds();
switch (event.type) {
case SWT.MouseDown:
System.out.println("compo:"+rect_canvas);
rect.x=0;
rect.y=0;
System.out.println("bounds:"+rect+"rec:"+event.x+" "+event.y);
if (rect.contains (event.x, event.y)) {
Point pt1 = canvas.toDisplay (0, 0);
Point pt2 = composite.toDisplay (event.x, event.y);
offset [0] = new Point (0,pt2.y - pt1.y );
System.out.println("offset:"+offset[0]);
}
/*if((rect_canvas.y)<0)//195
offset[0]=null;
*/
break;
case SWT.MouseMove:
System.out.println("move:"+(rect_canvas.y));
if((rect_canvas.y)>0)//195
if (offset [0] != null) {
Point pt = offset [0];
composite.setLocation (pt.x,event.y);
}
break;
case SWT.MouseUp:
if((rect_canvas.y)<=canvas.getBounds().height)
{
composite.setLocation (0,default_y_up);
}
offset [0] = null;
break;
}
}
};
canvas.addListener (SWT.MouseDown, listener);
canvas.addListener (SWT.MouseUp, listener);
canvas.addListener (SWT.MouseMove, listener);
//**************************slider 2**************
final Point [] offset_slider2 = new Point [1];
Listener listener_slider2 = new Listener () {
public void handleEvent (Event event) {
Rectangle rect1 = canvas.getBounds ();
Rectangle rect_canvas1=composite_1.getBounds();
switch (event.type) {
case SWT.MouseDown:
System.out.println("compo:"+rect_canvas1);
rect1.x=0;
rect1.y=0;
System.out.println("bounds:"+rect1+"rec:"+event.x+" "+event.y);
if (rect1.contains (event.x, event.y)) {
Point pt1 = canvas.toDisplay (0, 0);
Point pt2 = composite_1.toDisplay (event.x, event.y);
offset_slider2 [0] = new Point (0,pt2.y - pt1.y);
System.out.println("offset:"+offset[0]);
}
if((rect_canvas1.y+rect_canvas1.height)>canvas.getBounds().height)
offset_slider2[0]=null;
break;
case SWT.MouseMove:
System.out.println("move:"+(rect_canvas1.y+rect_canvas1.height));
if((rect_canvas1.y+rect_canvas1.height)<195)
if (offset_slider2 [0] != null) {
Point pt = offset_slider2 [0];
composite_1.setLocation (pt.x,event.y - pt.y);
}
break;
case SWT.MouseUp:
if((rect_canvas1.y+rect_canvas1.height)>=195)
{
composite_1.setLocation (0, default_y_down);
}
offset_slider2 [0] = null;
break;
}
}
};
canvas.addListener (SWT.MouseDown, listener_slider2);
canvas.addListener (SWT.MouseUp, listener_slider2);
canvas.addListener (SWT.MouseMove, listener_slider2);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}