JavaFX自定义滚动窗格

时间:2015-10-29 09:54:46

标签: javafx custom-controls scrollpane

我正在尝试在javafx中实现自定义滚动窗格。主要原因是,我想修改一些视觉部分以及滚动速度。 通过视觉部分,我指的是一段时间后我想要隐藏的滚动条。

我确实实现了带有剪切功能的scrollpane。 因此我有一个带有conent节点的StackPane,我剪切了StackPane。滚动是通过拖动内容节点完成的。到目前为止没问题。我想要实现的是我的scollpane适应其父级的大小(在我的例子中是一个AnchorPane)。因此,我“锚定”了我的实现的所有方面,并听取了scrollpanes布局边界。 虽然我的父窗格限制为大小(300,300),但滚动窗格占据了所有位置。 我认为主要的问题是layoutbounds。如果调整窗口大小,您将看到只有在边界超过圆的直径时才会写入打印输出。

我对如何最好地实现自定义滚动窗格的任何提示持开放态度。

    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.geometry.Orientation;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;

    public class TestCase extends Application{

        public static void main(String[] args) {
            launch(args);
        }

        private double NUM_IMAGES = 4.0;
        private Color col;



        @Override
        public void start(Stage primaryStage) throws Exception {

            HBox hb = new HBox();


            hb.getChildren().add(new Rectangle(300,300));
    //      ScrollPane sp = new ScrollPane();

            TestScrollPane sp = new TestScrollPane();
            hb.getChildren().add(sp);

            sp.setContent(new StackPane(new Circle(400)));

            AnchorPane.setTopAnchor(sp, 0.0);
            AnchorPane.setBottomAnchor(sp, 0.0);
            AnchorPane.setLeftAnchor(sp, 0.0);
            AnchorPane.setRightAnchor(sp, 0.0);



            Scene scene = new Scene(hb,800,800);
            scene.getStylesheets().add("imdvistav5.css");



            primaryStage.setScene(scene);

            primaryStage.show();




        }


        public class TestScrollPane extends StackPane{

            private AnchorPane content;
            private Rectangle clipRect;
            private MouseCoord coords;

            public TestScrollPane() {

                clipRect = new Rectangle(500,500);
                clipRect.setStyle("-fx-background-color:pink");
                setClip(clipRect);
                content = new AnchorPane();

                content.setOnMousePressed(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        coords.set(event.getScreenX(), event.getScreenY());
                    }
                });

                content.setOnMouseDragged(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        content.setTranslateX( content.getTranslateX()+ event.getScreenX()-coords.getX());
                        content.setTranslateY( content.getTranslateY()+ event.getScreenY()-coords.getY());
                        coords.set(event.getScreenX(), event.getScreenY());

                        System.out.println(getWidth());
                    }
                });
                coords = new MouseCoord();

                getChildren().add(content);



                layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
                    System.out.println(newValue.getWidth());
                    System.out.println(newValue.getHeight());
                    clipRect.setWidth(newValue.getWidth());
                    clipRect.setHeight(newValue.getHeight());
                  });   




            }

            public void setContent(Node node){
                content.getChildren().clear();
                content.getChildren().add(node);

                content.setTranslateX(0);content.setTranslateY(0);
            }

        }


        public class MouseCoord {


            private double x;
            private  double y;


            public MouseCoord() {

            }

            public MouseCoord(double x, double y) {
                this.x = x;
                this.y = y;
            }
            public double getX() {
                return x;
            }
            public void setX(double x) {
                this.x = x;
            }
            public double getY() {
                return y;
            }
            public void setY(double y) {
                this.y = y;
            }

            public void set(double x, double y){
                this.x = x;
                this.y = y;
            }

        }

   }

0 个答案:

没有答案
相关问题