首页 > 代码库 > 冯斌:JavaFx实例(十一)“ControlCircle”
冯斌:JavaFx实例(十一)“ControlCircle”
本实例是在实例(十)的基础上给按钮加上了句柄,可以用按钮对实例(十)中的圆进行缩放。
本实例的代码如下:
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) { 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); 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, 200, 150); primaryStage.setTitle("ControlCircle"); primaryStage.setScene(scene); primaryStage.show(); } class EnlargeHandler implements EventHandler<ActionEvent> { public void handle(ActionEvent e) { circlePane.enlarge(); } } class ShrinkHandler implements EventHandler<ActionEvent> { 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()); } }
本文出自 “冯斌的技术博客” 博客,请务必保留此出处http://fengbin8606.blog.51cto.com/8840305/1575044
冯斌:JavaFx实例(十一)“ControlCircle”
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。