目录
题目:*16.2 (选择几何图形)
习题思路
代码示例
结果展示
题目:*16.2 (选择几何图形)
编写一个绘制各种几何图形的程序,如图16-36b所示。用户从单选按钮中选择一个几何图形 ,并且使用复选框指定是否被填充
-
习题思路
- 新建一个Pane,分别创建Circle、Rectangle、Ellipse,设置好参数但不添加到Pane中
- 新建一个HBox,创建三个RadioButton,和一个CheckBox,把RadioButton绑定在一个组中,当单选按钮被选择时清除Pane上的节点,把对应的图形添加在Pane上,如果复选框被勾选,则填充节点的颜色(填充Pane中的第一个节点即可)
- 新建一个BorderPane,将Pane设置在在中心,将HBox设置在底部,把Scene的参数设为BorderPane
代码示例:
编程练习题16_2SelectGeometry.java
package chapter_16;
import javafx.application.Application;
import javafx.geometry.Insets;
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.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class 编程练习题16_2SelectGeometry extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
pane.setStyle("-fx-border-color: Black");
double x = 200;
double y = 100;
Circle circle = new Circle(x, y,20);
Rectangle rectangle = new Rectangle(x-50,y-20,100, 40);
Ellipse ellipse = new Ellipse(x, y, 60, 30);
circle.setFill(Color.TRANSPARENT);
circle.setStroke(Color.BLACK);
rectangle.setFill(Color.TRANSPARENT);
rectangle.setStroke(Color.BLACK);
ellipse.setFill(Color.TRANSPARENT);
ellipse.setStroke(Color.BLACK);
HBox hBox = new HBox(20);
hBox.setPadding(new Insets(5, 5, 5, 5));
hBox.setAlignment(Pos.CENTER);
RadioButton rbCircle = new RadioButton("Circle");
RadioButton rbRectangle = new RadioButton("Rectangle");
RadioButton rbEllipse = new RadioButton("Ellipse");
ToggleGroup group = new ToggleGroup();
rbCircle.setToggleGroup(group);
rbRectangle.setToggleGroup(group);
rbEllipse.setToggleGroup(group);
CheckBox chkFill = new CheckBox("Fill");
hBox.getChildren().addAll(rbCircle, rbRectangle, rbEllipse, chkFill);
chkFill.setOnAction(e ->{
if(chkFill.isSelected()&& (rbCircle.isSelected()||rbRectangle.isSelected()||rbEllipse.isSelected())) {
pane.getChildren().get(0).setStyle("-fx-fill: black");
}
if(!chkFill.isSelected()&& (rbCircle.isSelected()||rbRectangle.isSelected()||rbEllipse.isSelected())) {
pane.getChildren().get(0).setStyle(null);
}
});
rbCircle.setOnAction(e ->{
pane.getChildren().clear();
chkFill.setSelected(false);
circle.setStyle(null);
pane.getChildren().add(circle);
});
rbRectangle.setOnAction(e ->{
pane.getChildren().clear();
chkFill.setSelected(false);
rectangle.setStyle(null);
pane.getChildren().add(rectangle);
});
rbEllipse.setOnAction(e ->{
pane.getChildren().clear();
chkFill.setSelected(false);
ellipse.setStyle(null);
pane.getChildren().add(ellipse);
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(hBox);
Scene scene = new Scene(borderPane, 400, 226);
primaryStage.setTitle("编程练习题16_2SelectGeometry");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
-
结果展示