1、Java语言程序设计基础篇第10版梁勇著第十五章练习题答案Java语言程序设计(基础篇)(第10版梁勇著)第十五章练习题答案15.1import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.image.ImageView;import javafx.scene.layout.BorderPane;import javafx.scene.layout.HBox;im
2、port javafx.stage.Stage;import java.util.ArrayList;public class Exercise15_01 extends Application Override/ Override the start method in the Application classpublic void start(Stage primaryStage) / There are two ways for shuffling. One is to use the hint in the book./ The other way is to use the sta
3、tic shuffle method in the java.util.Collections class.ArrayList list = new ArrayList();for ( int i = 1; i java.util.Collections.shuffle(list);hBox.getChildren().clear();hBox.getChildren().add(new ImageView(image/card/+ list.get(0) +.png );hBox.getChildren().add(new ImageView(image/card/+ list.get(1)
4、 +.png );hBox.getChildren().add(new ImageView(image/card/+ list.get(2) +.png );hBox.getChildren().add(new ImageView(image/card/+ list.get(3) +.png ););new BorderPane();BorderPane pane = pane.setCenter(hBox);pane.setBottom(btRefresh);BorderPane.setAlignment(btRefresh, Pos.TOP_CENTER);/ Create a scene
5、 and place it in the stageScene scene = new Scene(pane, 250, 150);primaryStage.setTitle( Exercise15_01 ); / Set the stage titleprimaryStage.setScene(scene); / Place the scene in the stageprimaryStage.show(); / Display the stage/*The main method is only needed for the IDE with limited*JavaFX support.
6、 Not needed for running from the command line.*/public static void main(String args) launch(args);15.1 附加import import import import import import import import importjavafx.application.Application;javafx.geometry.Pos;javafx.scene.Scene;javafx.scene.control.Button;javafx.scene.layout.BorderPane; jav
7、afx.scene.layout.GridPane; javafx.scene.text.Font;javafx.scene.text.Text;javafx.stage.Stage;public class Exercise15_01Extra extends Application private Text texts = new Text1010;Override / Override the start method in the Application class public void start(Stage primaryStage) / Create UIGridPane pa
8、ne = new GridPane();pane.setAlignment(Pos.CENTER);pane.setHgap(10);pane.setVgap(10);for ( int i = 0; i 10; i+) for ( int j = 0; j for ( int i = 0; i 10; i+) for ( int j = 0; j if (getText().equals( H ) setText( T ); else setText( H ); );/*The main method is only needed for the IDE with limited*JavaF
9、X support. Not needed for running from the command line.*/public static void main(String args) launch(args);15.2import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.BorderPane;import javafx.scene.layo
10、ut.StackPane;import javafx.scene.paint.Color;import javafx.scene.shape.Rectangle;import javafx.stage.Stage;public class Exercise15_02 extends Application private double angle = 0;Override/ Override the start method in the Application class public void start(Stage primaryStage) StackPane stackPane =
11、new StackPane();Rectangle rectangle = new Rectangle(30, 30, 30, 60);rectangle.setFill(Color.WHITE);rectangle.setStroke(Color.BLACK);stackPane.getChildren().add(rectangle);/ Create a buttonButton btRotate = new Button( Rotate );btRotate.setOnAction(e - angle += 15;rectangle.setRotate(angle););BorderP
12、ane pane = new BorderPane();pane.setCenter(stackPane);pane.setBottom(btRotate);BorderPane.setAlignment(btRotate, Pos.TOP_CENTER);/ Create a scene and place it in the stageScene scene = new Scene(pane, 200, 150);primaryStage.setTitle( Exercise15_02 ); / Set the stage titleprimaryStage.setScene(scene)
13、; / Place the scene in the stageprimaryStage.show(); / Display the stage/*The main method is only needed for the IDE with limited*JavaFX support. Not needed for running from the command line.*/public static void main(String args) launch(args);15.2附加importjavafx.application.Application;importjavafx.g
14、eometry.Pos;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.BorderPane;importjavafx.scene.layout.Pane;importjavafx.scene.shape.Line;importjavafx.stage.Stage;publicclass Exercise15_02Extraextends Application privatestaticPane pane = new Pane();privatestaticdouble
15、paneWidth = 400;privatestaticdouble paneHeight = 250;Override / Override the start method in the Application class public void start(Stage primaryStage) drawARandomArrowLine();Button btDrawArrowLine = new Button( Draw a random arrow line );BorderPane borderPane = new BorderPane();borderPane.setCente
16、r(pane);borderPane.setBottom(btDrawArrowLine);BorderPane.setAlignment(btDrawArrowLine, Pos.CENTER);btDrawArrowLine.setOnAction(e - drawARandomArrowLine();/ Create a scene and place it in the stageScene scene = new Scene(borderPane, 400, 250);primaryStage.setTitle( Exercise15_02 ); / Set the stage ti
17、tleprimaryStage.setScene(scene); / Place the scene in the stageprimaryStage.show(); / Display the stageprivate static void drawARandomArrowLine() pane.getChildren().clear();double x1 = Math.random() * (paneWidth - 12);double y1 = Math.random() * (paneHeight - 12);double x2 = Math.random() * (paneWid
18、th - 12);double y2 = Math.random() * (paneHeight - 12);drawArrowLine(x1, y1, x2, y2, pane);public static void drawArrowLine( double x1, double y1, double x2, double y2, Pane pane) pane.getChildren().add( new Line(x1, y1, x2, y2);/ find slope of this line double slope = ( double ) y1) - ( double ) y2
19、)/ ( double ) x1) - ( double ) x2);double arctan = Math.atan(slope);/ This will flip the arrow 45 off of a/ perpendicular line at pt x2double set45 = 1.57 / 2;/ arrows should always point towards i, not i+1if (x1 circle.setCenterX(circle.getCenterX() 0 ? circle.getCenterX() - 2 : 0););btRight.setOnAction(e - circle.setCenterX(circle.getCenterX() circle.setCenterY(circle.getCenterY() 0 ? circle.getCenterY() - 2 : 0););btDown.setOnAction(e - circle.setCenterY(circle.getCe
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2