注释:太多人催我更新,本来这第十四章至第十六章java GUI 对于一部分人(不是科班生没必要学的),理由主要是你实际用不太到,而就是太过于简单了,没必要写答案。可是呢,最简单的东西大家问我要代码往往最多,无奈,有任何问题可以加QQ/微信:289582304,有时间我有及时回复。
编程题14_01:
第一道里不要太简单,如果不会,女的,找个人嫁了吧。男的说重一点,可以化学阉割了。im kidding!~~~~
再说一下这里的main方法是可以省略,但是idea太强大,它自己蹦出来
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
public class Exercise14_01 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
/**我这里是自己在百度图片找的图,然后放到创建得一个文件images
你们自己看着弄吧,想写出来和题目效果差不多的人,注意尺寸要差不
多,我是个完美主义的人,我自己裁剪400像素-250像素的。*/
ImageView image = new ImageView("images/UK.png");
ImageView image1 = new ImageView(("images/Canada.png"));
ImageView image2 = new ImageView(("images/China.png"));
ImageView image3 = new ImageView(("images/US.png"));
// 这里我觉得有点必要写,对齐和间隙,美观点
pane.setAlignment(Pos.CENTER);
pane.setHgap(5);
pane.setVgap(5);
// 利用GridPane知识点中add方法,
pane.add(image, 0, 0);
pane.add(image1, 0, 1);
pane.add(image2, 1, 0);
pane.add(image3, 1, 1);
// 这里就不说了,看不懂直接物理,留着没用
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowExercise");
primaryStage.setScene(scene);
primaryStage.show();
}
}
编程题14_02
说明,参上
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
public class Exercise14_02 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
// 我自己用画板画的原始图
Image image = new Image("images/O.png");
Image image1 = new Image("images/X.png");
// 参上
pane.setAlignment(Pos.CENTER);
pane.setVgap(5);
pane.setHgap(5);
// 看不懂去复习一下循环,意思说看不懂从第一章再学一遍
for (int i = 0; i < 3; i++) {
for (int n = 0; n < 3; n++) {
int status = (int)(Math.random() * 3);
if (status == 0)
pane.add(new ImageView(image), i, n);
if (status == 1)
pane.add(new ImageView(image1), i, n);
}
}
// 物理
Scene scene = new Scene(pane, 200, 200);
primaryStage.setTitle("ShowExercise");
primaryStage.setScene(scene);
primaryStage.show();
}
}
14_03 略
说明: 可以做,但是要实现效果有点累,略了
14_04
说明:没啥要说的,这种简单可以做一下,不是浪费时间,巩固一下基础知识,下面两道题我就不做了,直接从第七题开始
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
import javafx.scene.layout.VBox;
public class Exercise14_02 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
VBox pane = new VBox(15);
// 这里记得循环,一个个实现效率有点低
// 循环条件5可以改成静态常量,方便大家知道这个5怎么凭空来的
for (int i = 0; i < 5; i++) {
Text text = new Text("Java");
pane.setAlignment(Pos.CENTER);
pane.setRotate(90); // 旋转
text.setFont(Font.font("TimesRomes", FontWeight.BOLD,
FontPosture.ITALIC, 22));
text.setFill(Color.color((Math.random() * 1), (Math.random() * 1),
(Math.random() * 1), (Math.random() * 1)));
pane.getChildren().add(text);
}
Scene scene = new Scene(pane, 200, 200);
primaryStage.setTitle("ShowExercise");
primaryStage.setScene(scene);
primaryStage.show();
}
}
编程14_07
说明一下,这道题其实和第六题同出一辙,当然你也可以参照这道题写第六题
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.scene.control.TextField;
public class Exercise14_02 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
FlowPane pane = new FlowPane();
double width = 300, height = 300;
// 这里特别说明一下, 这个10应该设置为静态常量,100是10*10过来的
// 但是因为赶时间就懒了,你们自己看着办,其他没什么好讲的
// 下面也就TextField比较常用的方法
for (int i = 0; i < 100 ; i++) {
TextField textField = new TextField();
textField.setAlignment(Pos.CENTER);
textField.setPrefSize(width / 10, height / 10);
textField.setText((int)(Math.random() * 2) + "");
pane.getChildren().add(textField);
}
Scene scene = new Scene(pane, width, height);
primaryStage.setTitle("ShowExercise");
primaryStage.setScene(scene);
primaryStage.show();
}
}
编程14_08 略
说明:既然第三题我没做,这一道题同样看不到效果我也就略了
编程14_09
import javafx.application.Application;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
public class Exercise14_02 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
pane.add(getPane(), 0, 0);
pane.add(getPane(), 0, 1);
pane.add(getPane(), 1, 0);
pane.add(getPane(), 1, 1);
Scene scene = new Scene(pane, 350, 350);
primaryStage.setTitle("ShowExercise");
primaryStage.setScene(scene);
primaryStage.show();
}
public Pane getPane() {
Pane pane = new Pane();
double radius = 70;
Circle circle = new Circle(100,100, radius);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
pane.getChildren().add(circle);
// 这里在设置Arc的初始的Angle要注意,一定要利用循环进行旋转,
// 不是利用Rotate方法旋转,不然四个Arc不会围绕中心点
// 因为我就犯了这样的错误,其他也没什么好注释的
for (int i = 0; i < 4; i++) {
Arc arc = new Arc(circle.getCenterX(), circle.getCenterY(), radius - 10,
radius - 10, 30 + i * 90, 35);
arc.setType(ArcType.ROUND);
arc.setStroke(Color.BLACK);
arc.setFill(Color.BLACK);
pane.getChildren().add(arc);
}
return pane;
}
}
编程14_10
这道题我竟然构思半个钟头,裂开,我把它想复杂了,原谅我好久没用FX
写这种题的时候突然音乐放了学友《她来听我的演唱会》没文化,只能用说一句,cao,真他娘的好听
import javafx.application.Application;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
public class Exercise14_02 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Ellipse ellipse = new Ellipse(100, 40, 50, 20);
ellipse.setFill(Color.WHITE);
ellipse.setStroke(Color.BLACK);
Arc arc1 = new Arc(100, 140, 50, 20, 0, 180);
arc1.setFill(Color.WHITE);
arc1.setStroke(Color.BLACK);
arc1.getStrokeDashArray().addAll(6.0, 21.0);
Arc arc2 = new Arc(100, 140, 50, 20, 180, 180);
arc2.setFill(Color.WHITE);
arc2.setStroke(Color.BLACK);
pane.getChildren().addAll(ellipse, arc1, arc2,
new Line(50, 40, 50, 140), new Line(150, 40, 150, 140));
Scene scene = new Scene(pane, 200, 200);
primaryStage.setTitle("ShowExercise");
primaryStage.setScene(scene);
primaryStage.show();
}
}
剩下的题目我只写两颗星以上的,其他一星你们有问题,直接联系我吧.
还有我不只是学友的粉丝,我是Eason十年以上的老粉
编程14_14
说明一下,因为时间问题我就做了两个图,反正就这样的意思,这里面的系数一些常事都是根据自己的判断,不然没真任何办法,我看标准答题也是这样,当然你们有更好的数据或者更好的方法,有朋友说,能否用循环,我觉得不行,必须用一个个创建五个Rectangle和Text对象,还有就是剩下三个你只需把系数0.1的基础上根据百分比例来排出来。
import javafx.application.Application;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
public class Exercise14_02 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
// 注意间距,每创建一个对象,间距也随之扩大一倍
// 间距为什么还要加5呢,为什么放上面的Text对象的字符串在上面不太好看
double gap = 10, paneHeight = 150, height = 300;
Rectangle rectangle = new Rectangle(gap, paneHeight - height * 0.2, 100, height * 0.2);
Text text = new Text(gap+5,paneHeight - height * 0.2 - gap,"Project-20%");
pane.getChildren().add(rectangle);
rectangle.setFill(Color.RED);
pane.getChildren().add(text);
Rectangle rectangle1 = new Rectangle(gap * 2 + 100, paneHeight - height * 0.1, 100,height * 0.1);
Text text1 = new Text((gap+5) * 2 + 100,paneHeight - height * 0.1 - gap, "Test-10%");
pane.getChildren().add(rectangle1);
rectangle1.setFill(Color.BLUE);
pane.getChildren().add(text1);
Scene scene = new Scene(pane, 300, paneHeight);
primaryStage.setTitle("ShowExercise");
primaryStage.setScene(scene);
primaryStage.show();
}