场景:我有两个面板,其中一个有滑块,另一个有拖动山墙窗格。
案例I:如果窗格(即拖拽山墙)向前或向后移动。滑块上的节点移动,各个尺寸在移动窗格的相同方向上。
案例II:此功能反之亦然。
(拖动带滑块的山墙窗格)
(带拖曳山墙的滑块)。
所以我怎么能实现它.. 谢谢!
答案 0 :(得分:0)
您可以为水平方向创建属性。然后,只要滑块或窗格移动,您就可以更改属性。根据属性更改,您可以移动窗格。
public class Main extends Application {
DoubleProperty xProperty = new SimpleDoubleProperty();
double min = 0;
double max = 100;
@Override
public void start(Stage primaryStage) {
try {
Pane root = new Pane();
// rectangle
Pane pane = new Pane();
pane.setStyle("-fx-background-color:blue");
pane.setPrefSize(50, 50);
pane.relocate(min, 50);
// make rectangle movable
MouseGestures mg = new MouseGestures();
mg.makeDraggable(pane);
// slider
Slider slider = new Slider(min, max, min);
slider.valueProperty().bindBidirectional( xProperty);
root.getChildren().addAll(slider, pane);
// move horizontally, clamp horizontal movement
xProperty.addListener(new ChangeListener<Number>() {
@Override
public void changed( ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
// clamp x
double x = newValue.doubleValue();
if( x < min) {
x = min;
}
if( x > max) {
x = max;
}
pane.relocate( x, pane.getBoundsInParent().getMinY());
}
});
Scene scene = new Scene(root,1024,768);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
public class MouseGestures {
final DragContext dragContext = new DragContext();
public void makeDraggable(final Node node) {
node.setOnMousePressed(onMousePressedEventHandler);
node.setOnMouseDragged(onMouseDraggedEventHandler);
}
EventHandler<MouseEvent> onMousePressedEventHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Node node = (Node) event.getSource();
dragContext.x = node.getBoundsInParent().getMinX() - event.getScreenX();
}
};
EventHandler<MouseEvent> onMouseDraggedEventHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
double offsetX = event.getScreenX() + dragContext.x;
xProperty.set(offsetX);
}
};
class DragContext {
double x;
}
}
}