一基础题
14.22
如何将一个节点加人到一个Pane、StackPane、FlowPane、GrldPane、BorderPane、HBox、VBox
中?如何从这些面板中移除一个节点?
Pane
pane=new Pane/StackPane/FlowPane/Grldpane/borderpane/hbox/vbox()
Pane.getchildren().add()
Pane.getchildren().remove()
14.26 FlowPane 和HBox 或者VBox
之间的区别是什么?
Flowpane是从左到右按顺序排列,地方不够就换行
Hbox是分布在单个水平行中
VBox 是分布在单个垂直列中
二编程题
14.9 (创建四个风扇)请写一个程序,将四个风扇按照两行两列置于一个GridPane 中,如图14-45b
所示。
import javafx.application.Application;
import javafx.stage.*;
import javafx.scene.*;
import javafx.geometry.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
public class Creatfourpan extends Application{
@Override
public void start(Stage primaryStage)
{
GridPane pane =new GridPane();
pane.setPadding(new Insets(10,10,10,10));
for(int column=0;column<2;column++)
{
for(int row=0;row<2;row++)
{
pane.add(getCircle(),column,row);
pane.add(getArc(), column, row);
}
}
Scene scene =new Scene(pane);
primaryStage.setTitle("CREATFOURPAN");
primaryStage.setScene(scene);
primaryStage.show();
}
public Pane getCircle()
{
Pane pane =new Pane();
pane.setPadding(new Insets(10,10,10,10));
Circle c=new Circle(100,100,70);
c.setFill(Color.WHITE);
c.setStroke(Color.BLACK);
c.setStyle("-fx-fill;white;-fx-stroke;black");
pane.getChildren().add(c);
return pane;
}
public Pane getArc()
{
Pane pane=new Pane();
pane.setPadding(new Insets(10,10,10,10));
for(int i=0;i<4;i++)
{
Arc a=new Arc(100,100,60,60,(i*90+45),30);
a.setType(ArcType.ROUND);
a.setFill(Color.BLACK);
a.setStroke(Color.BLACK);
pane.getChildren().add(a);
}
return pane;
}
public static void main(String[]args){
launch(args);
}
}
14.12 (显示一个柱形图)请写一个程序,使用柱形图来显示一个总成绩的各个组成部分的百分比,包括项目、测试、期中考试和期末考试,如图14*46b 所示。假设项目占20%并显示为红色,测试占10%并显示为蓝色,期中考试占30%并显示为绿色,期末考试占40% 并显示为橙色。使用Rectangle
类来显示柱形。有兴趣的读者可以探索使用JavaFX
的BarChart 类来进一步学习。
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.text.Text;
import javafx.geometry.*;
public class Histogram extends Application{
@Override
public void start (Stage primaryStage)
{
HBox pane =new HBox(10);
pane.setPadding(new Insets(10,10,10,10));
double[] re={20,10,30,40};
String[]name={"项目-20%","测试-10%","期中考试-30%","期末考试-40%"};
int length =re.length;
double max=re[0];
for(int i=1;i<length;i++)
{
if(re[i]>max)
{
max=re[i];
}
}
for(int i=0;i<length;i++)
{
pane.getChildren().add(getRectangle(max,re[i],name[i]));
}
Scene scene=new Scene(pane);
primaryStage.setTitle("Histogram");
primaryStage.setScene(scene);
primaryStage.show();
}
public Pane getRectangle(double max,double length,String name)
{
final double LENGTH =3;
final double WIDTH =100;
Pane pane=new Pane();
Text per =new Text(0,(max-length+8)*LENGTH,name);
per.setStroke(Color.BLACK);
Rectangle re=new Rectangle(0,(max-length+10)*LENGTH,WIDTH,length*LENGTH);
switch((int)length/10)
{
case 1:
re.setFill(Color.BLUE);
break;
case 2:
re.setFill(Color.RED);
break;
case 3:
re.setFill(Color.GREEN);
break;
case 4:
re.setFill(Color.ORANGE);
break;
}
re.setStroke(Color.BLACK);
pane.getChildren().addAll(re,per);
return pane;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
}
14.16 (显示一个3 x 3 的网格)请写一个绘制3 x 3 网格的程序,如图14>47c 所示。使用红色绘制垂直线,蓝色绘制水平线。当窗体改变大小的时候,这些线条自动改变大小。
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
public class grid extends Application{
@Override
public void start(Stage primaryStage)
{
Pane pane = new Pane();
pane.setPadding(new Insets(200,200,200,200));
DoubleProperty x1=new SimpleDoubleProperty();
x1.bind(pane.widthProperty().divide(3));
DoubleProperty x2=new SimpleDoubleProperty();
x2.bind(pane.widthProperty().multiply(2).divide(3));
DoubleProperty y1=new SimpleDoubleProperty();
y1.bind(pane.heightProperty().divide(3));
DoubleProperty y2=new SimpleDoubleProperty();
y2.bind(pane.heightProperty().multiply(2).divide(3));
Line lineX1=new Line();
lineX1.setStroke(Color.RED);
lineX1.startXProperty().bind(x1);
lineX1.setStartY(0.0);
lineX1.endXProperty().bind(x1);
lineX1.endYProperty().bind(pane.heightProperty());
Line lineX2=new Line();
lineX2.setStroke(Color.RED);
lineX2.startXProperty().bind(x2);
lineX2.setStartY(0.0);
lineX2.endXProperty().bind(x2);
lineX2.endYProperty().bind(pane.heightProperty());
Line lineY1=new Line();
lineY1.setStroke(Color.BLUE);
lineY1.setStartX(0.0);
lineY1.startYProperty().bind(y1);
lineY1.endXProperty().bind(pane.widthProperty());
lineY1.endYProperty().bind(y1);
Line lineY2=new Line();
lineY2.setStroke(Color.BLUE);
lineY2.setStartX(0.0);
lineY2.startYProperty().bind(y2);
lineY2.endXProperty().bind(pane.widthProperty());
lineY2.endYProperty().bind(y2);
pane.getChildren().addAll(lineX1,lineX2,lineY1,lineY2);
Scene scene=new Scene(pane);
primaryStage.setTitle("grid");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
}