Java语言程序设计第十版第十六章16.2课后操作题答案

16.2

package Exercise; 
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.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.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

 
	public class Exercise16_2 extends Application{  
		protected BorderPane getPane(){
			 BorderPane pane = new BorderPane();
			StackPane stackpane = new StackPane(); 
			HBox paneForRadioButtons = new HBox(20); 
		    RadioButton cir = new RadioButton("circle");
			RadioButton rec = new RadioButton("rectangle");
		    RadioButton ell = new RadioButton("ellipse");
		    CheckBox Fill = new CheckBox("Fill");
			paneForRadioButtons.getChildren().addAll(cir, rec, ell, Fill);  
			paneForRadioButtons.setAlignment(Pos.CENTER);
			
			Circle circle = new Circle(100,100,60);
			circle.setStroke(Color.BLACK);
			circle.setFill(Color.TRANSPARENT);
			
		    Rectangle rectangle = new Rectangle(100,100,200,120);  
		    rectangle.setStroke(Color.BLACK);
			rectangle.setFill(Color.TRANSPARENT);
			
			Ellipse ellipse = new Ellipse(150,100,100,50);
			ellipse.setStroke(Color.BLACK);
			ellipse.setFill(Color.TRANSPARENT);
			
			paneForRadioButtons.setPadding(new Insets(5,5,5,5));
			
			ToggleGroup group = new ToggleGroup();
			cir.setToggleGroup(group);
			rec.setToggleGroup(group);
			ell.setToggleGroup(group);  
			
			Fill.setOnAction(new EventHandler() { 
				public void handle(ActionEvent arg0) {
					if(Fill.isSelected()){ 
							circle.setFill(Color.RED);  
							rectangle.setFill(Color.PINK);  
							ellipse.setFill(Color.GRAY);
					}
					else
					{
						circle.setFill(Color.TRANSPARENT);
						rectangle.setFill(Color.TRANSPARENT);
						ellipse.setFill(Color.TRANSPARENT);
					}
				}
			});
			
			cir.setOnAction(new EventHandler() { 
				public void handle(ActionEvent arg0) {   
					if(cir.isSelected()){   
						stackpane.getChildren().clear();  
						stackpane.getChildren().add(circle);   
					}  
				}
			}); 
			
			rec.setOnAction(new EventHandler() {  
				public void handle(ActionEvent arg0) {    
						if(rec.isSelected()){
						stackpane.getChildren().clear();  
						stackpane.getChildren().add(rectangle);
					}  
					} 
			}); 
			
			ell.setOnAction(new EventHandler() { 
				public void handle(ActionEvent arg0) {  
					if(ell.isSelected()){  
						stackpane.getChildren().clear();  
						stackpane.getChildren().add(ellipse);
					} 
				}
				});    
			
			pane.setBottom(paneForRadioButtons);  
			pane.setCenter(stackpane);
			return pane;
		}
		
		public void start(Stage args){
			Scene scene = new Scene(getPane(), 450, 200);
			args.setTitle("Exercise9_A");
			args.setScene(scene);
			args.show();
		}
		public static void main(String[] args){
			Application.launch(args);
		}
	}
 

你可能感兴趣的:(Java语言程序设计第十版第十六章16.2课后操作题答案)