课堂/听众之间的沟通

时间:2016-06-27 16:21:14

标签: java javafx onclicklistener

我想在顶部创建一个带有按钮和颜色选择器的应用程序,并在BorderPane的中心创建一个画布。我创建了一个主类TestSceneBuilder和2个侦听器:一个用于按钮,另一个用于ColorPicker。问题是:当我检测到颜色变化时,如何将其传递给我的CerchioListener?

主要类别:

public class TestSceneBuilder extends Application {
    final int H = 300, W = 300; //height and width
    BorderPane root;
    @Override
    public void start(Stage primaryStage) {
        root = this.setScene();
        Scene scene = new Scene(root, H, W);

        primaryStage.setTitle("Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    /**
     * This method is supposed to build the scene with all components:
     * a button "Draw" that draws the rectangle
     * a canvas
     * a colorPicker
     * @return 
     */
    BorderPane setScene(){
        BorderPane border = new BorderPane();
        final ColorPicker cp = new ColorPicker(Color.AQUA);
        Canvas canvas = new Canvas(H, W);
        Button btn = new Button("Draw");
        /*CerchioListner should get the mouse clicked event and draw the circle*/
        final CerchioListner l = new CerchioListner(canvas, cp.getValue());
        btn.addEventHandler(MouseEvent.MOUSE_CLICKED, l);

        /*ColorListener intercept the color change in ColorPicker cp and change the color of the
        shape drawn*/
        ColorListener cl = new ColorListener(cp);
        cp.setOnAction(cl);

        HBox hb = new HBox();
        hb.getChildren().addAll(btn, cp);
        border.setTop(hb);
        BorderPane.setAlignment(hb, Pos.CENTER);

        border.setCenter(canvas);
        return border;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

按钮监听器:CerchioListener

public class CerchioListner implements javafx.event.EventHandler{
Canvas canvas = null;
Color colore;
public CerchioListner(Canvas c, Color colore) {
    this.canvas = c;
    this.colore = colore;
}

public void changeColor(Color c) {
    this.colore = c;
}
@Override
public void handle(Event t) {
    disegna();
}
public void disegna(){
    canvas.getGraphicsContext2D().setFill(colore);
    canvas.getGraphicsContext2D().fillOval(20, 20, 20, 20);
    }  
}

颜色选择器侦听器:ColorListener

public class ColorListener implements javafx.event.EventHandler{
ColorPicker cp = null;
public ColorListener(ColorPicker cp) {
    this.cp = cp;
}

@Override
public void handle(Event t) {
    Color c = cp.getValue();
    System.out.println("handle CP "+cp.getValue());

    //restituisciColoreSelezionato(c);
}

/*public Color restituisciColoreSelezionato(Color c){
    return c;
}*/
}

1 个答案:

答案 0 :(得分:1)

有几件事情不是你能做到的最好的事情:

  • 您有一个Canvas,它不是Main的成员,只是setScene()中的一个局部变量,因此只能在该方法中访问它。由于Canvas是您班级中最重要的部分,因此您应该将其作为班级成员,因为您希望从班级的任何地方访问它。

  • Button的侦听器不应存储对所选颜色的任何引用,而应存储Canvas,它由Main存储,侦听器应使用该成员。

  • ColorPicker的监听器不应存储对ColorPicker本身的任何引用。 ColorPicker应该是会员,以便能够在Main中的任意位置访问当前选定的颜色。

我已更新您的代码以包含这些修改:

public class TestSceneBuilder extends Application {
    final int H = 300, W = 300;
    BorderPane root;
    Canvas canvas;
    ColorPicker cp;
    Button btn;

    @Override
    public void start(Stage primaryStage) {
        root = this.setScene();
        Scene scene = new Scene(root, H, W);

        primaryStage.setTitle("Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    BorderPane setScene(){
        BorderPane border = new BorderPane();

        cp = new ColorPicker(Color.AQUA);
        canvas = new Canvas(H, W);
        btn = new Button("Draw");

        btn.setOnAction((event) -> {
            canvas.getGraphicsContext2D().setFill(cp.getValue());
            canvas.getGraphicsContext2D().fillOval(20, 20, 20, 20);
        });


        HBox hb = new HBox();
        hb.getChildren().addAll(btn, cp);
        border.setTop(hb);
        BorderPane.setAlignment(hb, Pos.CENTER);

        border.setCenter(canvas);
        return border;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

如果您想与外部听众保持联系:

兑换这个:

btn.setOnAction((event) -> {
    canvas.getGraphicsContext2D().setFill(cp.getValue());
    canvas.getGraphicsContext2D().fillOval(20, 20, 20, 20);
});

CerchioListener cerchioListener = new CerchioListener(canvas);
btn.setOnAction(cerchioListener);
cerchioListener.colorProperty.bind(cp.valueProperty());

并添加监听器:

CerchioListener.java

public class CerchioListener implements EventHandler<ActionEvent> {

    private Canvas canvas = null;
    public ObjectProperty<Color> colorProperty = new SimpleObjectProperty<Color>(Color.WHITE);

    public CerchioListener(Canvas c) {
        this.canvas = c;
    }

    public Canvas getCanvas() {
        return canvas;
    }

    public void setCanvas(Canvas canvas) {
        this.canvas = canvas;
    }

    @Override
    public void handle(ActionEvent t) {
        canvas.getGraphicsContext2D().setFill(colorProperty.get());
        canvas.getGraphicsContext2D().fillOval(20, 20, 20, 20);
    }

}