关于使用JavaFX中的按钮调整矩形大小的家庭作业帮助



所以我的作业是把这个controlCircle程序转换成一个画矩形而不是圆形的程序。程序需要:

  • 使用四个按钮来控制矩形的大小,这四个按钮对用户做出反应,使矩形变宽、变窄、变高或变短

任何关于按钮以及它们如何寻找矩形的长度、宽度、高度和短度的帮助都将不胜感激,因为我认为这将是我最大的问题。以下是controlCircle.java的代码:

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 controlCircle extends Application {
  private CirclePane circlePane = new CirclePane();
  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) {
    // Hold two buttons in an HBox
    HBox hBox = new HBox();
    hBox.setSpacing(10);
    hBox.setAlignment(Pos.CENTER);
    Button btEnlarge = new Button("Enlarge");
    Button btShrink = new Button("Shrink");
    hBox.getChildren().add(btEnlarge);
    hBox.getChildren().add(btShrink);
    // Create and register the handler
    btEnlarge.setOnAction(e -> {
        circlePane.enlarge();
    });
    btShrink.setOnAction(new ShrinkHandler());
    BorderPane borderPane = new BorderPane();
    borderPane.setCenter(circlePane);
    borderPane.setBottom(hBox);
    BorderPane.setAlignment(hBox, Pos.CENTER);
    // Create a scene and place it in the stage
    Scene scene = new Scene(borderPane, 200, 150);
    primaryStage.setTitle("ControlCircle"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
  }
  class ShrinkHandler implements EventHandler<ActionEvent> {
        @Override // Override the handle method
        public void handle(ActionEvent e) {
          circlePane.shrink();
        }
      }
  public static void main(String[] args) {
    launch(args);
  }
}
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 enlarge() {
    circle.setRadius(circle.getRadius() + 2);
  }
  public void shrink() {
    circle.setRadius(circle.getRadius() > 2 ?
      circle.getRadius() - 2 : circle.getRadius());
  }
}

它很简单。使用以下代码并理解。

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.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ControlRectangle extends Application {
  private RectanglePane rectanglePane = new RectanglePane();
  @Override
  public void start(Stage primaryStage) {
    HBox hBox = new HBox();
    hBox.setSpacing(5);
    hBox.setAlignment(Pos.CENTER);
    Button btTaller = new Button("Taller");
    Button btSmaller = new Button("Smaller");
    Button btWider = new Button("Wider");
    Button btNarrower = new Button("Narrower");
    hBox.getChildren().add(btTaller);
    hBox.getChildren().add(btSmaller);
    hBox.getChildren().add(btWider);
    hBox.getChildren().add(btNarrower);
    // Create and register the handler
    btTaller.setOnAction(e -> rectanglePane.taller());
    btSmaller.setOnAction(e -> rectanglePane.smaller());
    btWider.setOnAction(e -> rectanglePane.wider());
    btNarrower.setOnAction(e -> rectanglePane.narrower());
    BorderPane borderPane = new BorderPane();
    borderPane.setCenter(rectanglePane);
    borderPane.setBottom(hBox);
    BorderPane.setAlignment(hBox, Pos.CENTER);
    // Create a scene and place it in the stage
    Scene scene = new Scene(borderPane, 250, 200);
    primaryStage.setTitle("ControlRectangle"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
  }

  public static void main(String[] args) {
    launch(args);
  }
}
class RectanglePane extends StackPane {
  private Rectangle rectangle = new Rectangle(150,75);
  public RectanglePane() {
    getChildren().add(rectangle);
    rectangle.setStroke(Color.BLACK);
    rectangle.setFill(Color.WHITE);
  }
  public void taller() {
      rectangle.setHeight(rectangle.getHeight() + 2);
  }
  public void smaller() {
      rectangle.setHeight(rectangle.getHeight() - 2);
  }
  public void wider() {
      rectangle.setWidth(rectangle.getWidth() + 2);
  }
  public void narrower() {
      rectangle.setWidth(rectangle.getWidth() - 2);
  }
}

我希望它能帮助你。。

最新更新