为什么JavaFX ScrollPane没有工作,以及为什么盒子会绕圈?

时间:2016-04-12 21:50:52

标签: javafx fxml

这是我的主要班级......

package software;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.fxml.FXMLLoader;

    public class FXMLExample extends Application {

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

        @Override
        public void start(Stage primaryStage) {
            try {
                ScrollPane root = (ScrollPane)FXMLLoader.load(getClass().getResource("fxml_example.fxml"));
                Scene scene = new Scene(root, 200, 200);
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
}

FXML文件......

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.shape.*?>
<?import javafx.scene.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<ScrollPane fx:id="sp" hbarPolicy="NEVER" maxHeight="-Infinity"
    maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
    pannable="true" prefHeight="400.0" prefWidth="600.0" vbarPolicy="NEVER"
    xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="software.FXMLExampleController">
    <content>
        <Group>
            <children>
                <Circle fill="DODGERBLUE" radius="100.0" stroke="BLACK"
                    strokeType="INSIDE" />
            </children>
        </Group>
    </content>
</ScrollPane>

这是我的控制器...

package software;

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.ScrollEvent;
import javafx.stage.Stage;

public class FXMLExampleController {

    @FXML
    ScrollPane sp;

    final double SCALE_DELTA = 1.1;

    @FXML
    protected void initialize() {

        double scrollDelta = 0;

        System.out.println("In initialize() method");

        System.out.println("sp:" + sp);

        // boardersp.sceneProperty().addListener( (o, oldEvent, newEvent) ->
        // {
        System.out.println("Gleep000");
        System.out.println("Gleep000.0");
        System.out.println("sp: " + sp.getClass().getName());
        sp.sceneProperty();
        System.out.println("Gleep001");
        System.out.println("Gleep002");
        sp.sceneProperty().addListener((o, oldEvent, newEvent) -> {
            if (oldEvent != null) {
                // remove your stuff from the scene
                System.out.println("In addListner() remove");
            }
            if (newEvent != null) {
                // add your stuff to the scene
                System.out.println("In addListner() add");
                newEvent.setOnScroll(new EventHandler() {
                    @Override
                    public void handle(Event event) {
                        System.out.println("In handle scroll event");

                        ScrollEvent se = (ScrollEvent) event;
                        double scrollDelta = ((ScrollEvent) event).getDeltaY();
                        System.out.println("\tscrollDelta: " + scrollDelta);

                        if (scrollDelta == 0) {
                            return;
                        }

                        double scaleFactor = (se.getDeltaY() > 0) ? SCALE_DELTA : 1 / SCALE_DELTA;

                        System.out.println("\tscaleFactor: " + scaleFactor);
                        // amount of scrolling in each direction in
                        // scrollContent coordinate
                        // units
                        // Point2D scrollOffset =
                        // figureScrollOffset(topScrollPaneGroup,
                        // topScrollPane);

                        System.out.println("\tBefore::boardersp.getTranslateX: " + sp.getTranslateX());
                        System.out.println("\tBefore::boardersp.sp.getScaleX: " + sp.getScaleX());
                        sp.setScaleX(sp.getScaleX() * scaleFactor);
                        System.out.println("\tAfter::boardersp.getTranslateX: " + sp.getTranslateX());
                        System.out.println("\tAfter::boardersp.sp.getScaleX: " + sp.getScaleX());

                        System.out.println("\tBefore::boardersp.getTranslateY: " + sp.getTranslateY());
                        System.out.println("\tBefore::boardersp.sp.getScaleY: " + sp.getScaleY());
                        sp.setScaleY(sp.getScaleY() * scaleFactor);
                        System.out.println("\tAfter::boardersp.getTranslateY: " + sp.getTranslateY());
                        System.out.println("\tAfter::boardersp.sp.getScaleY: " + sp.getScaleY());
                    }
                });
            }
        });
    }

}

为什么鼠标滚轮缩小时实心圆周围的框? 为什么&#34; pannable&#34;设置不起作用?

0 个答案:

没有答案