JavaFX ---如何在可观察数组列表中访问(和更改)对象的属性?

时间:2016-11-12 19:34:23

标签: java javafx

我目前在我的JavaFX程序中有三个类,它们一起显示Balls(存储在一个可观察的数组列表中)到我的界面上。现在,界面输出正是我想要的(三个球具有正确的位置,大小,颜色等。

但是,我似乎无法使用我实现的getter和setter更改属性,例如x和y位置,颜色,半径等。它在背景上有三个Ball。我想知道如何访问这些属性。

Ball是一个类,由圆circleRepresentation表示(因为Ball不是可直接在我的窗格中显示的节点)。

package application;


import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;

public class Ball {
    private String name;
    pivate int xPosition;
    private int yPosition;
    private int radius;
    Color color;
    private Circle circleRepresentation;

    public Ball(String name, int xPosition, int yPosition, int radius, Color color) {
        this.name = name;
        this.xPosition = xPosition;
        this.yPosition = yPosition;
        this.radius = radius;
        this.color = color;
        circleRepresentation = new Circle(xPosition, yPosition, radius, color);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getxPosition() {
        return xPosition;
    }

    public void setxPosition(int xPosition) {
        this.xPosition = xPosition;
    }

    public int getyPosition() {
        return yPosition;
    }

    public void setyPosition(int yPosition) {
        this.yPosition = yPosition;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }


    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public Circle getCircle() {
        return circleRepresentation;
    }

}

我有一个BallManager,它将我在Main方法中创建的所有Ball存储到名为elements的ObservableList中。

package application;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;

public class BallManager {

    private Pane  group = new Pane ();
    private ObservableList<Ball> elements = FXCollections.observableArrayList();

    public BallManager() {
        elements.addListener(new ListChangeListener<Ball>() {

            @Override
            public void onChanged(javafx.collections.ListChangeListener.Change<? extends Ball> c) {
                while (c.next()) {
                    for (Ball remitem : c.getRemoved()) {
                        group.getChildren().remove(remitem.getCircle());
                    }
                    for (Ball additem : c.getAddedSubList()) {
                        Circle circle = additem.getCircle();

                        group.getChildren().add(circle);

                        circle.relocate(additem.getxPosition(), additem.getyPosition());
                    }
                }
            }
        });
    }



    public ObservableList<Ball> getElements() {
        return elements;
    }

    public Pane getPane() {
        return group;
    }
}

我的主要方法目前允许在我的界面上输出所有元素,如添加元素时想象的那样。但是稍后当我尝试将第一个球ball1的颜色设置为白色时,使用bm.getElements().get(0).setColor(Color.WHITE);它不允许它。

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;


public class Main extends Application {


    @Override
    public void start(Stage primaryStage) {
        try {

            BorderPane root = new BorderPane();
            final Scene scene = new Scene(root, 500, 500);

            primaryStage.setTitle("Boucing Balls");
            primaryStage.setScene(scene);
            primaryStage.show();
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

            BallManager bm = new BallManager();

            bm.getElements().add(new Ball("ball1", 50, 250, 30, Color.BLUE));
            bm.getElements().add(new Ball("ball2", 250, 50, 30, Color.PLUM));
            bm.getElements().add(new Ball("ball3", 250, 250, 30, Color.AQUAMARINE));

            bm.getPane().setStyle("-fx-background-color: orange");

            root.setCenter(bm.getPane());

            //failed attempt to change the color of ball1, which i believe is at index 0.
            bm.getElements().get(0).setColor(Color.WHITE);

            // this statement now prints 'ball1' as intended.
            System.out.println(bm.getElements().get(0).getName());


        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

欢迎提出任何建议,如果我需要提供更多信息,请在下面的评论中告诉我。感谢。

0 个答案:

没有答案