无法从场景中删除刚添加的组件

时间:2013-07-14 18:45:39

标签: user-interface javafx-2 javafx-8

我在两个不同的文件中有以下代码:

   @FXML
    public void openFileMenu() throws IOException {


        FXMLLoader loader = new FXMLLoader(getClass().getResource("FileMenu.fxml"), resourceBundle);
        AnchorPane root = (AnchorPane) loader.load();
        MenuController menuController = loader.getController();
        if (toolbarStack.getWidth() < menuController.getContentWidth()) {
            menuController.addManipulator();
        }

        ResizeListener.widthProperty.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
                if (toolbarStack.getWidth() < menuController.getContentWidth()) {
                    menuController.addManipulator();
                } else {
                    menuController.removeManipulator();
                }
            }
        });
}

package UI;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.animation.*;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.util.Duration;

public class MenuController implements Initializable {


    @Override
    public void initialize(URL url, ResourceBundle rb) {}

    public void addManipulator() {
        AnchorPane.setLeftAnchor(menuScrollPane, 50.0);
        AnchorPane.setRightAnchor(menuScrollPane, 50.0);

        leftButton = new Button("<");
        leftButton.setId("leftButton");
        leftButton.setPrefSize(50.0, 100.0);
        AnchorPane.setLeftAnchor(leftButton, 0.0);
        leftButton.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                slideTo(LEFT);
            }
        });
        leftButton.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                stopSlide();
            }
        });

        rightButton = new Button(">");
        rightButton.setId("rightButton");
        rightButton.setPrefSize(50.0, 100.0);
        AnchorPane.setRightAnchor(rightButton, 0.0);
        rightButton.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
               slideTo(RIGHT);
            }
        });
        rightButton.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                stopSlide();
            }
        });

        mainMenuPane.getChildren().addAll(leftButton, rightButton);

    }

    public void removeManipulator() {
        mainMenuPane.getChildren().removeAll(leftButton, rightButton);
        AnchorPane.setLeftAnchor(menuScrollPane, 0.0);
        AnchorPane.setRightAnchor(menuScrollPane, 0.0);

    }

    private void slideTo(boolean direction) {
        value.setValue(menuScrollPane.getHvalue());
        double realSpeed = direction ? animationSpeed*(1-menuScrollPane.getHvalue()) : animationSpeed*menuScrollPane.getHvalue();
        timeline = new Timeline();
        KeyValue kv  = direction ? new KeyValue(value, 1.0) : new KeyValue(value, 0.0);
        KeyFrame frame = new KeyFrame(Duration.millis(realSpeed), kv);
        timeline.getKeyFrames().add(frame);
        timeline.play();
        value.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                menuScrollPane.setHvalue(newValue.doubleValue());
            }
        });
    }

    private void stopSlide() {
        timeline.stop();
        timeline.getKeyFrames().removeAll(timeline.getKeyFrames());
    }

    public double getContentWidth() {
        return menuContentPane.prefWidthProperty().doubleValue();
    }

    @FXML
    private AnchorPane mainMenuPane;
    @FXML
    private AnchorPane menuContentPane;
    @FXML
    private ScrollPane menuScrollPane;

    private Button leftButton;
    private Button rightButton;
    private Timeline timeline;
    private DoubleProperty value = new SimpleDoubleProperty();
    private final boolean LEFT = true;
    private final boolean RIGHT = false;

    private double animationSpeed = 1000.0;
}

问题是,在按menuController.addManipulator();添加按钮后,无法删除它们 - menuController.removeManipulator();不起作用。但最奇怪的是只有mainMenuPane.getChildren().removeAll(leftButton, rightButton);不起作用,同时AnchorPane.setLeftAnchor(menuScrollPane, 0.0); AnchorPane.setRightAnchor(menuScrollPane, 0.0);做他们必须做的一切。有什么问题?

0 个答案:

没有答案