java语言程序设计基础篇第十版练习答案.docx
《java语言程序设计基础篇第十版练习答案.docx》由会员分享,可在线阅读,更多相关《java语言程序设计基础篇第十版练习答案.docx(59页珍藏版)》请在冰豆网上搜索。
java语言程序设计基础篇第十版练习答案
01
importjavafx.application.Application;
importjavafx.geometry.Pos;
importjavafx.scene.Scene;
importjavafx.scene.layout.GridPane;
importjavafx.stage.Stage;
importjavafx.scene.image.ImageView;
publicclassExercise14_01extendsApplication{
@Override//OverridethestartmethodintheApplicationclass
publicvoidstart(StageprimaryStage){
GridPanepane=newGridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(5);
pane.setVgap(5);
ImageViewimageView1=newImageView("image/uk.gif");
ImageViewimageView2=newImageView("image/ca.gif");
ImageViewimageView3=newImageView("image/china.gif");
ImageViewimageView4=newImageView("image/us.gif");
pane.add(imageView1,0,0);
pane.add(imageView2,1,0);
pane.add(imageView3,0,1);
pane.add(imageView4,1,1);
//Createasceneandplaceitinthestage
Scenescene=newScene(pane);
primaryStage.setTitle("Exercise14_01");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){
launch(args);
}
}
02
importjavafx.application.Application;
importjavafx.geometry.Pos;
importjavafx.scene.Scene;
importjavafx.scene.layout.GridPane;
importjavafx.stage.Stage;
importjavafx.scene.image.ImageView;
importjavafx.scene.image.Image;
publicclassExercise14_02extendsApplication{
@Override//OverridethestartmethodintheApplicationclass
publicvoidstart(StageprimaryStage){
ImageimageX=newImage("image/x.gif");
ImageimageO=newImage("image/o.gif");
GridPanepane=newGridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(5);
pane.setVgap(5);
for(inti=0;i<3;i++){
for(intj=0;j<3;j++){
intstatus=(int)(Math.random()*3);
if(status==0){
pane.add(newImageView(imageX),j,i);
}
elseif(status==1){
pane.add(newImageView(imageO),j,i);
}
}
}
//Createasceneandplaceitinthestage
Scenescene=newScene(pane);
primaryStage.setTitle("Exercise14_02");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){
launch(args);
}
}
03
importjava.util.ArrayList;
importjavafx.application.Application;
importjavafx.geometry.Pos;
importjavafx.scene.Scene;
importjavafx.scene.layout.HBox;
importjavafx.stage.Stage;
importjavafx.scene.image.ImageView;
publicclassExercise14_03extendsApplication{
@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);
HBoxpane=newHBox(5);
pane.setAlignment(Pos.CENTER);
pane.getChildren().add(newImageView("image/card/"+list.get(0)+".png"));
pane.getChildren().add(newImageView("image/card/"+list.get
(1)+".png"));
pane.getChildren().add(newImageView("image/card/"+list.get
(2)+".png"));
//Createasceneandplaceitinthestage
Scenescene=newScene(pane);
primaryStage.setTitle("Exercise14_03");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){
launch(args);
}
}
04
importjavafx.application.Application;
importjavafx.geometry.Pos;
importjavafx.scene.Scene;
importjavafx.scene.layout.HBox;
importjavafx.scene.paint.Color;
importjavafx.scene.text.Font;
importjavafx.scene.text.FontPosture;
importjavafx.scene.text.FontWeight;
importjavafx.scene.text.Text;
importjavafx.stage.Stage;
publicclassExercise14_04extendsApplication{
@Override//OverridethestartmethodintheApplicationclass
publicvoidstart(StageprimaryStage){
HBoxpane=newHBox();
pane.setAlignment(Pos.CENTER);
Fontfont=Font.font("TimesNewRoman",FontWeight.BOLD,
FontPosture.ITALIC,22);
for(inti=0;i<5;i++){
Texttxt=newText("Java");
txt.setRotate(90);
txt.setFont(font);
txt.setFill(newColor(Math.random(),Math.random(),Math.random(),Math.random()));
pane.getChildren().add(txt);
}
//Createasceneandplaceitinthestage
Scenescene=newScene(pane,200,100);
primaryStage.setTitle("Exercise14_04");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){
launch(args);
}
}
05
importjavafx.application.Application;
importjavafx.scene.Scene;
importjavafx.scene.layout.Pane;
importjavafx.scene.text.Font;
importjavafx.scene.text.FontPosture;
importjavafx.scene.text.FontWeight;
importjavafx.scene.text.Text;
importjavafx.stage.Stage;
publicclassExercise14_05extendsApplication{
@Override//OverridethestartmethodintheApplicationclass
publicvoidstart(StageprimaryStage){
Panepane=newPane();
Fontfont=Font.font("TimesNewRoman",FontWeight.BOLD,
FontPosture.REGULAR,35);
Strings="WELCOMETOJAVA";
doubleradius=80;
for(inti=0;idoublealpha=2*Math.PI*(s.length()-i)/s.length();
Texttxt=newText(radius*Math.cos(alpha)+120,
120-radius*Math.sin(alpha),s.charAt(i)+"");
txt.setFont(font);
txt.setRotate(360*i/s.length()+90);
pane.getChildren().add(txt);
}
//Createasceneandplaceitinthestage
Scenescene=newScene(pane,240,240);
primaryStage.setTitle("Exercise14_05");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){
launch(args);
}}
05
importjavafx.application.Application;
importjavafx.scene.Scene;
importjavafx.scene.layout.Pane;
importjavafx.scene.text.Font;
importjavafx.scene.text.FontPosture;
importjavafx.scene.text.FontWeight;
importjavafx.scene.text.Text;
importjavafx.stage.Stage;
publicclassExercise14_05extendsApplication{
@Override//OverridethestartmethodintheApplicationclass
publicvoidstart(StageprimaryStage){
Panepane=newPane();
Fontfont=Font.font("TimesNewRoman",FontWeight.BOLD,
FontPosture.REGULAR,35);
Strings="WELCOMETOJAVA";
doubleradius=80;
for(inti=0;idoublealpha=2*Math.PI*(s.length()-i)/s.length();
Texttxt=newText(radius*Math.cos(alpha)+120,
120-radius*Math.sin(alpha),s.charAt(i)+"");
txt.setFont(font);
txt.setRotate(360*i/s.length()+90);
pane.getChildren().add(txt);
}
//Createasceneandplaceitinthestage
Scenescene=newScene(pane,240,240);
primaryStage.setTitle("Exercise14_05");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){
launch(args);
}
}
06
importjavafx.application.Application;
importjavafx.scene.Scene;
importjavafx.scene.layout.Pane;
importjavafx.scene.paint.Color;
importjavafx.stage.Stage;
importjavafx.scene.shape.Rectangle;
publicclassExercise14_06extendsApplication{
@Override//OverridethestartmethodintheApplicationclass
publicvoidstart(StageprimaryStage){
doubleWIDTH=200;
doubleHEIGHT=200;
Panepane=newPane();
for(inti=0;i<8;i++){
booleanisWhite=i%2==0;
for(intj=0;j<8;j++){
Rectanglerectangle=newRectangle(i*WIDTH/8,
j*HEIGHT/8,WIDTH/8,HEIGHT/8);
rectangle.setStroke(Color.BLACK);
if(isWhite){
rectangle.setFill(Color.WHITE);
}
else{
rectangle.setFill(Color.BLACK);
}
isWhite=!
isWhite;
pane.getChildren().add(rectangle);
}
}
//Createasceneandplaceitinthestage
Scenescene=newScene(pane,WIDTH,HEIGHT);
primaryStage.setTitle("Exercise14_06");//Setthestagetitle
primaryStage.setScene(scene);//Placethesceneinthestage
primaryStage.show();//Displaythestage
}
/**
*ThemainmethodisonlyneededfortheIDEwithlimited
*JavaFXsupport.Notneededforrunningfromthecommandline.
*/
publicstaticvoidmain(String[]args){
launch(args);
}
}
07
importjavafx.application.Application;
importjavafx.geometry.Pos;
importjavafx.scene.Scene;
importjavafx.scene.control.TextField;
importjavafx.scene.layout.GridPane;
importjavafx.stage.Stage;
publicclassExercise14_07extendsApplication{
@Override//OverridethestartmethodintheApplicationclass
publicvoidstart(StageprimaryStage){
doubleWIDTH=200;
doubleHEIGHT=200;
GridPanepane=newGridPane();
for(inti=0;i