JavaFX、选择几何图形

package fx;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Exercise16_02 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        StackPane stackPane = new StackPane();
        RadioButton circle = new RadioButton("Circle");
        RadioButton rectangle = new RadioButton("Rectangle");
        RadioButton ellipse = new RadioButton("Ellipse");
        CheckBox fill = new CheckBox("Fill");

        ToggleGroup toggleGroup = new ToggleGroup();
        circle.setToggleGroup(toggleGroup);
        rectangle.setToggleGroup(toggleGroup);
        ellipse.setToggleGroup(toggleGroup);

        HBox bottomHBox = new HBox(10, circle, rectangle, ellipse, fill);
        bottomHBox.setAlignment(Pos.CENTER);

        circle.setOnAction(event -> {
            if (circle.isSelected()) {
                Circle c = new Circle(50);
                if (fill.isSelected())
                    c.setStyle("-fx-fill: black; -fx-stroke: black;");
                else
                    c.setStyle("-fx-fill: null; -fx-stroke: black;");
                stackPane.getChildren().clear();
                stackPane.getChildren().add(c);
            }
        });
        rectangle.setOnAction(event -> {
            if (rectangle.isSelected()) {
                Rectangle r = new Rectangle(100, 50);
                if (fill.isSelected())
                    r.setStyle("-fx-fill: black; -fx-stroke: black;");
                else
                    r.setStyle("-fx-fill: null; -fx-stroke: black;");
                stackPane.getChildren().clear();
                stackPane.getChildren().add(r);
            }
        });
        ellipse.setOnAction(event -> {
            if (ellipse.isSelected()) {
                Ellipse e = new Ellipse(50, 25);
                if (fill.isSelected())
                    e.setStyle("-fx-fill: black; -fx-stroke: black;");
                else
                    e.setStyle("-fx-fill: null; -fx-stroke: black;");
                stackPane.getChildren().clear();
                stackPane.getChildren().add(e);
            }
        });
        fill.setOnAction(event -> {
            if (fill.isSelected())
                stackPane.getChildren().get(0).setStyle("-fx-fill: black; -fx-stroke: black;");
            else
                stackPane.getChildren().get(0).setStyle("-fx-fill: null; -fx-stroke: black;");
        });

        BorderPane pane = new BorderPane(stackPane);
        pane.setBottom(bottomHBox);
        pane.getCenter().setStyle("-fx-border-color: black;");

        Scene scene = new Scene(pane, 350, 200);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Exercise16_02");
        primaryStage.show();
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值