JAVA代码题
我已经写出了代码,但是点击向上向下圆不移动,求大神指导!!importjavafx.application.Application;importjavafx.event....
我已经写出了代码,但是点击向上向下圆不移动,求大神指导!!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 HandleEvent extends Application{ private CirclePane circlePane = new CirclePane(); @Override public void start(Stage primaryStage){ HBox hBox = new HBox(); hBox.setSpacing(10); hBox.setAlignment(Pos.CENTER); Button btleft = new Button("向左"); Button btright = new Button("向右"); Button btup = new Button("向上"); Button btdown = new Button("向下"); 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(circlePane); borderPane.setBottom(hBox); BorderPane.setAlignment(hBox,Pos.CENTER); Scene scene = new Scene(borderPane,200,200); primaryStage.setTitle(""); primaryStage.setScene(scene); primaryStage.show(); } class LeftHandler implements EventHandler<ActionEvent>{ @Override public void handle(ActionEvent e){ circlePane.left(); } } class RightHandler implements EventHandler<ActionEvent>{ @Override public void handle(ActionEvent e){ circlePane.right(); } } class UpHandler implements EventHandler<ActionEvent>{ @Override public void handle(ActionEvent e){ circlePane.up(); } } class DownHandler implements EventHandler<ActionEvent>{ @Override public void handle(ActionEvent e){ circlePane.down(); } }}class CirclePane extends StackPane{ private Circle circle = new Circle(50); public CirclePane(){ getChildren().add(circle); circle.setStroke(Color.BLACK); circle.setFill(Color.WHITE); } public void left(){ circle.setCenterX(circle.getCenterX() - 2); } public void right(){ circle.setCenterX(circle.getCenterX() + 2); } public void up(){ circle.setCenterY(circle.getCenterY() + 2); } public void down(){ circle.setCenterY(circle.getCenterY() - 2); }}
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询