gpt4 book ai didi

JavaFx - 方法未定义

转载 作者:行者123 更新时间:2023-12-02 04:51:43 24 4
gpt4 key购买 nike

我必须使用 JavaFX 创建一个程序,该程序创建一个带有球的 Pane 。有四个按钮可以使球向左、向右、向上或向下移动。我不明白为什么当我去定义每个方法(如所示的 .left() )时,上面的方法调用仍然是一个错误,告诉我它是未定义的。

主类:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class MoveTheBall extends Application {
@Override
public void start(Stage primaryStage) {
StackPane pane = new StackPane();
Circle circle = new Circle(20);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
pane.getChildren().add(circle);

HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btLeft = new Button("Left");
Button btRight = new Button("Right");
Button btUp = new Button("Up");
Button btDown = new Button("Down");
hBox.getChildren().add(btLeft);
hBox.getChildren().add(btRight);
hBox.getChildren().add(btUp);
hBox.getChildren().add(btDown);

BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);

Scene scene = new Scene(borderPane, 300, 200);
primaryStage.setTitle("Move The Ball");
primaryStage.setScene(scene);
primaryStage.show();

}
}

按钮类:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class ControlBall extends Application {
private MoveTheBall moveTheBall = new MoveTheBall();

@Override
public void start(Stage primaryStage) {

HBox hBox = new HBox();
hBox.setSpacing(5);
hBox.setAlignment(Pos.CENTER);
Button btLeft = new Button("Left");
Button btRight = new Button("Right");
Button btUp = new Button("Up");
Button btDown = new Button("Down");
hBox.getChildren().add(btLeft);
hBox.getChildren().add(btRight);
hBox.getChildren().add(btUp);
hBox.getChildren().add(btDown);

btLeft.setOnAction(new LeftHandler());
btRight.setOnAction(new RightHandler());
btUp.setOnAction(new UpHandler());
btDown.setOnAction(new DownHandler());

BorderPane borderPane = new BorderPane();
borderPane.setCenter(moveTheBall);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);

Scene scene = new Scene(borderPane, 200, 150);
primaryStage.setTitle("Move The Ball");
primaryStage.setScene(scene);
primaryStage.show();
}

class LeftHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
moveTheBall.left(); // error because no class yet
}
}

class RightHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
moveTheBall.right(); // error because no class yet
}
}

class UpHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
moveTheBall.up(); // error because no class yet
}
}

class DownHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
moveTheBall.down(); // error because no class yet
}
}
}

class moveTheBall extends StackPane {
private Circle ball = new Circle(20);

public moveTheBall() {
getChildren().add(ball);
ball.setStroke(Color.BLACK);
ball.setFill(Color.WHITE);
}

public void left() {
ball.setCenterX(-2.0);
ball.setCenterY(-2.0);
}
}

线 borderPane.setCenter(moveTheBall);也给我带来了问题。 “BorderPane 类型中的 setCenter(Node) 方法不适用于参数 (MoveTheBall)”

最佳答案

我对 JavaFX 还很陌生,但我会在这里尽力而为。

我建议不要使用完全独立的类来控制球、移动球和处理每个按钮,而是考虑使用 lambda 表达式来简化代码并减少出错的空间。例如,您可以通过编写如下行来设置 btRight 按钮的功能:

btRight.setOnAction(e -> {
//if the ball is inside the right limit
//Move the ball to the right
});

您可以将这些表达式直接写入 start() 方法中,并避免定义内部类以及控件的单独类。

至于为什么会出现错误,我最好的猜测(正如我所说,我对此很陌生)是 moveTheBall() 是 MoveTheBall 类的构造函数,但它被作为方法调用。

希望这有帮助。

关于JavaFx - 方法未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29158964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com