使用两个按钮控制一个圆的大小,用户可以单击Enlarge和Shrink按钮来放大和缩小圆的尺寸。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
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.Circle;
import javafx.stage.Stage;
public class Hello extends Application{
private CirclePane circlePane = new CirclePane();
@Override
public void start(Stage primaryStage){
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setPadding(new Insets(10));
hBox.setAlignment(Pos.CENTER);
Button btEnlarge = new Button("Enlarge");
Button btShrink = new Button("Shrink");
hBox.getChildren().add(btEnlarge);
hBox.getChildren().add(btShrink);
btEnlarge.setOnAction(new EnlargeHandler());
btShrink.setOnAction(new ShrinkHandler());
BorderPane borderPane = new BorderPane();
borderPane.setCenter(circlePane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
Scene scene = new Scene(borderPane,400,400);
primaryStage.setTitle("ControlCircle");
primaryStage.setScene(scene);
primaryStage.show();
}
class EnlargeHandler implements EventHandler<ActionEvent>{
@Override
public void handle(ActionEvent e){
circlePane.enlarge();
}
}
class ShrinkHandler implements EventHandler<ActionEvent>{
@Override
public void handle(ActionEvent e){
circlePane.shrink();
}
}
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());
}
}
public static void main(String[] agrs){
Application.launch(agrs);
}
}