【问题标题】:When changing the value of a variable the variable reverts back after the method called one is run, how do I make the change permanent [duplicate]更改变量的值时,变量在运行调用一个的方法后恢复,如何使更改永久化[重复]
【发布时间】:2016-09-14 00:36:49
【问题描述】:

我有一个类应该将谢尔宾斯基三角形绘制到给定的度数,由增加/减少按钮来改变该度数。 但是,在我处理按钮按下后,度数(变量 x)总是恢复为 0

我的问题在哪里?

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Recursion4 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane pane = new BorderPane();
        Button plus = new Button("+");
        Button minus = new Button("-");
        HBox h = new HBox(5);
        Pane p = new Pane();
        h.getChildren().addAll(plus, minus);
        h.setSpacing(10);
        pane.setBottom(h);
        pane.setCenter(p);
        h.setAlignment(Pos.CENTER);
        Scene scene = new Scene(pane, 900, 900);
        primaryStage.setTitle("Triangles");
        primaryStage.setScene(scene);
        primaryStage.show();
        int x = 0;
        draw(450, 300, 300, 600, 600, 600, x, p);
        plus.setOnAction(e -> {
             plus(x, p);

        });
        minus.setOnAction(e -> {
            minus(x, p);
        });

    }

    public static void main(String[] args) {
        launch(args);
        int x = 0;
    }

    public static void plus(int x, Pane p) {
        p.getChildren().clear();
        ++x;
        draw(450, 300, 300, 600, 600, 600, x, p);

    }

    public static void minus(int x, Pane p) {
        p.getChildren().clear();
        if (x == 0) {
            draw(450, 300, 300, 600, 600, 600, x, p);
        } else {
            x--;
            draw(450, 300, 300, 600, 600, 600, x, p);

        }
    }

    public static void draw(int x1, int y1, int x2, int y2, int x3, int y3, Pane p) {
        Line l1 = new Line(x1, y1, x2, y2);
        Line l2 = new Line(x2, y2, x3, y3);
        Line l3 = new Line(x3, y3, x1, y1);
        p.getChildren().add(l1);
        p.getChildren().add(l2);
        p.getChildren().add(l3);
    }

    public static void draw(int x1, int y1, int x2, int y2, int x3, int y3, int z, Pane p) {
        if (z == 0) {
            draw(x1, y1, x2, y2, x3, y3, p);
        } else {
            draw(x1, y1, x2, y2, x3, y3, p);
            int xa, ya, xb, yb, xc, yc; 
            xa = (x1 + x2) / 2; 
            ya = (y1 + y2) / 2;
            xb = (x1 + x3) / 2;
            yb = (y1 + y3) / 2;
            xc = (x2 + x3) / 2;
            yc = (y2 + y3) / 2;

            draw(x1, y1, xa, ya, xb, yb, z - 1,p); 
            draw(xa, ya, x2, y2, xc, yc, z - 1,p);
            draw(xb, yb, xc, yc, x3, y3, z - 1,p);

        }
    }
}

【问题讨论】:

    标签: java button recursion methods javafx


    【解决方案1】:

    ma​​inplusminus都有一个局部变量x(注意局部声明) .你没有提到对象属性x,这似乎是你想要改变的。使用 this.x 而不是声明一个本地的。

    【讨论】:

    • 其实 OP 好像根本没有x 字段。
    • 我将 Start 中的声明作为创建字段的意图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 2010-10-04
    • 2023-02-07
    • 1970-01-01
    • 2014-01-05
    • 2017-03-20
    相关资源
    最近更新 更多