《Java语言程序设计基础篇》第10版梁勇著第十五章练习题答案.docx
《《Java语言程序设计基础篇》第10版梁勇著第十五章练习题答案.docx》由会员分享,可在线阅读,更多相关《《Java语言程序设计基础篇》第10版梁勇著第十五章练习题答案.docx(135页珍藏版)》请在冰点文库上搜索。
《Java语言程序设计基础篇》第10版梁勇著第十五章练习题答案
《Java语言程序设计(基础篇)》(第10版梁勇著)
第十五章练习题答案
15.1
importjavafx.application.Application;
importjavafx.geometry.Pos;
importjavafx.scene.Scene;
importjavafx.scene.control.Button;
importjavafx.scene.image.ImageView;
importjavafx.scene.layout.BorderPane;
importjavafx.scene.layout.HBox;
importjavafx.stage.Stage;
importjava.util.ArrayList;
publicclassExercise15_01extendsApplication{
@Override
//OverridethestartmethodintheApplicationclass
publicvoidstart(StageprimaryStage){
//Therearetwowaysforshuffling.Oneistousethehintinthebook.
//Theotherwayistousethestaticshufflemethodinthejava.util.Collectionsclass.
ArrayListlist=newArrayList<>();
for(inti=1;i<=52;i++){
list.add(i);
}
java.util.Collections.shuffle(list);
HBpxhBox=newHBox(5);
hBox.setAlignment(Pos.CENTER);
hBox.getChildren().add(
new
ImageView(
"image/card/"
+list.get(0)+
".png"));
hBox.getChildren().add(
new
ImageView(
"image/card/"
+list.get
(1)+
".png"));
hBox.getChildren().add(
new
ImageView(
"image/card/"
+list.get
(2)+
".png"));
hBox.getChildren().add(
new
ImageView(
"image/card/"
+list.get(3)+
".png"));
//Createabutton
ButtonbtRefresh=newButton("Refresh");
btRefresh.setOnAction(e->{
java.util.Collections.shuffle(list);
hBox.getChildren().clear();
hBox.getChildren().add(
newImageView(
"image/card/"
+list.get(0)+
".png"));
hBox.getChildren().add(
newImageView(
"image/card/"
+list.get
(1)+
".png"));
hBox.getChildren().add(
newImageView(
"image/card/"
+list.get
(2)+
".png"));
hBox.getChildren().add(
newImageView(
"image/card/"
+list.get(3)+
.png"));
});
newBorderPane();
BorderPanepane=pane.setCenter(hBox);
pane.setBottom(btRefresh);
BorderPane.setAlignment(btRefresh,Pos.TOP_CENTER);
//Createasceneandplaceitinthestage
Scenescene=newScene(pane,250,150);
primaryStage.setTitle("Exercise15_01");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){launch(args);
}
}
15.1附加
importimportimportimportimportimportimportimportimport
javafx.application.Application;
javafx.geometry.Pos;
javafx.scene.Scene;
javafx.scene.control.Button;
javafx.scene.layout.BorderPane;javafx.scene.layout.GridPane;javafx.scene.text.Font;
javafx.scene.text.Text;
javafx.stage.Stage;
publicclassExercise15_01ExtraextendsApplication{
privateText[][]texts=newText[10][10];
@Override//OverridethestartmethodintheApplicationclasspublicvoidstart(StageprimaryStage){
//CreateUI
GridPanepane=newGridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(10);
pane.setVgap(10);
for(inti=0;i<10;i++){
for(intj=0;j<10;j++){
pane.add(texts[i][j]=newText((int)(Math.random()*2)+""),j,i);
}
}
ButtonbtRefresh=newButton("Refresh");
BorderPaneborderPane=newBorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(btRefresh);
BorderPane.setAlignment(btRefresh,Pos.CENTER);
btRefresh.setOnAction(e->{
for(inti=0;i<10;i++){
for(intj=0;j<10;j++){
texts[i][j].setText((int)(Math.random()*2)+"");
}
}
});
//Createasceneandplaceitinthestage
Scenescene=newScene(borderPane,200,350);
primaryStage.setTitle("Exercise15_01");//Settitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
classCellextendsText{
Cell(){
setFont(Font.font("TimesRoman",35));super.setText("H");
this.setOnMouseClicked(e->{
if(getText().equals("H")){setText("T");
}else{setText("H");
}});
}
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){launch(args);
}
}
15.2
importjavafx.application.Application;
importjavafx.geometry.Pos;
importjavafx.scene.Scene;
importjavafx.scene.control.Button;
importjavafx.scene.layout.BorderPane;
importjavafx.scene.layout.StackPane;
importjavafx.scene.paint.Color;
importjavafx.scene.shape.Rectangle;
importjavafx.stage.Stage;
publicclassExercise15_02extendsApplication{privatedoubleangle=0;
@Override
//OverridethestartmethodintheApplicationclasspublicvoidstart(StageprimaryStage){StackPanestackPane=newStackPane();
Rectanglerectangle=newRectangle(30,30,30,60);
rectangle.setFill(Color.WHITE);
rectangle.setStroke(Color.BLACK);
stackPane.getChildren().add(rectangle);
//Createabutton
ButtonbtRotate=newButton("Rotate");
btRotate.setOnAction(e->{
angle+=15;
rectangle.setRotate(angle);
});
BorderPanepane=newBorderPane();
pane.setCenter(stackPane);
pane.setBottom(btRotate);
BorderPane.setAlignment(btRotate,Pos.TOP_CENTER);
//Createasceneandplaceitinthestage
Scenescene=newScene(pane,200,150);
primaryStage.setTitle("Exercise15_02");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){launch(args);
}
}
15.2附加
import
javafx.application.Application;
import
javafx.geometry.Pos;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.BorderPane;
import
javafx.scene.layout.Pane;
import
javafx.scene.shape.Line;
import
javafx.stage.Stage;
public
classExercise15_02Extra
extendsApplication{
private
static
Panepane=newPane();
private
static
doublepaneWidth=400;
private
static
doublepaneHeight=250;
@Override//OverridethestartmethodintheApplicationclasspublicvoidstart(StageprimaryStage){
drawARandomArrowLine();
ButtonbtDrawArrowLine=newButton("Drawarandomarrowline");
BorderPaneborderPane=newBorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(btDrawArrowLine);
BorderPane.setAlignment(btDrawArrowLine,Pos.CENTER);
btDrawArrowLine.setOnAction(e->drawARandomArrowLine());
//Createasceneandplaceitinthestage
Scenescene=newScene(borderPane,400,250);
primaryStage.setTitle("Exercise15_02");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
privatestaticvoiddrawARandomArrowLine(){pane.getChildren().clear();
doublex1=Math.random()*(paneWidth-12);
doubley1=Math.random()*(paneHeight-12);
doublex2=Math.random()*(paneWidth-12);
doubley2=Math.random()*(paneHeight-12);
drawArrowLine(x1,y1,x2,y2,pane);
}
publicstaticvoiddrawArrowLine(doublex1,doubley1,doublex2,doubley2,Panepane){
pane.getChildren().add(newLine(x1,y1,x2,y2));
//findslopeofthislinedoubleslope=((((double)y1)-(double)y2))
/(((double)x1)-(((double)x2)));
doublearctan=Math.atan(slope);
//Thiswillflipthearrow45offofa
//perpendicularlineatptx2
doubleset45=1.57/2;
//arrowsshouldalwayspointtowardsi,noti+1
if(x1//add90degreestoarrowlinesset45=-1.57*1.5;
}
//setlengthofarrowsintarrlen=15;
//drawarrowsonline
pane.getChildren().add(newLine(x2,y2,(x2+(Math.cos(arctan+set45)*
arrlen)),
((y2))+(Math.sin(arctan+set45)*arrlen)));
pane.getChildren().add(newLine(x2,y2,(x2+(Math.cos(arctan-set45)*
arrlen)),
((y2))+(Math.sin(arctan-set45)*arrlen)));
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){launch(args);
}
}
15.3
importjavafx.application.Application;
importjavafx.geometry.Pos;
importjavafx.scene.Scene;
importjavafx.scene.control.Button;
importjavafx.scene.layout.BorderPane;
importjavafx.scene.layout.HBox;
importjavafx.scene.layout.Pane;
importjavafx.scene.paint.Color;
importjavafx.scene.shape.Circle;
importjavafx.stage.Stage;
publicclassExercise15_03extendsApplication{
@Override
//OverridethestartmethodintheApplicationclasspublicvoidstart(StageprimaryStage){
Panepane=newPane();
Circlecircle=newCircle(40,50,20);
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
pane.getChildren().add(circle);
//Createfourbuttons
HBoxhBox=newHBox(5);
ButtonbtLeft=
ButtonbtRight=
ButtonbtUp=
ButtonbtDown=
newButton("Left");
newButton("Right");
newButton("Up");
newButton("Down");
hBox.setAlignment(Pos.CENTER);
hBox.getChildren().addAll(btLeft,btRight,btUp,btDown);
BorderPaneborderPane=newBorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox,Pos.TOP_CENTER);
//Createasceneandplaceitinthestage
Scenescene=newScene(borderPane,200,150);
primaryStage.setTitle("Exercise15_03");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
btLeft.setOnAction(e->{circle.setCenterX(circle.getCenterX()>0?
circle.getCenterX()-2:
0);
});
btRight.setOnAction(e->{
circle.setCenterX(circle.getCenterX()circle.getCenterX()+2:
0);});
btUp.setOnAction(e->{circle.setCenterY(circle.getCenterY()>0?
circle.getCenterY()-2:
0);
});
btDown.setOnAction(e->{
circle.setCenterY(circle.getCe