java语言程序设计 第十五章 (15.1、15.2、15.3)

15.1(1) 只随机刷新一次扑克牌
在这里插入图片描述

package hahaha;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;

public class demo14_3 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{

        Pane pane = new HBox(10);
//        pane.setPadding(new Insets(5,5,5,5));
        int[]a=new int [60];
        for(int i=0;i<3;i++){
            int x;
                do {
                    x=(int)(Math.random()*54+1);
                }
            while(a[x]!=0);
            a[x]++;
            Image image = new Image("image/card/"+x+".png");
            pane.getChildren().add(new ImageView(image));
        }

        Scene scene=new Scene(pane);
        primaryStage.setTitle("card");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

15.1 (2)添加了下方中间的按钮()
在这里插入图片描述

package sample;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
//import javafx.scene.layout.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{

        Pane pane = new HBox(10);
        pane.setPadding(new Insets(5,5,5,5));
        int[]a=new int [55];
        for(int i=1;i<=3;i++) {
            int x;
            do {
                x = (int) (Math.random() * 54 + 1);
            }
            while (a[x] != 0);
            a[x]++;
            Image image = new Image("image/card/" + x + ".png");
            pane.getChildren().add(new ImageView(image));
        }

        Pane pane2 = new VBox(10);
        pane2.getChildren().add(pane);
        pane2.getChildren().add(new StackPane(new Button("Refresh")));
        Scene scene=new Scene(pane2);
        primaryStage.setTitle("card");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

15.1(3)可以随机生成,点按钮后并随机刷新
把抽三张牌写进了函数里

package sample;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
//import javafx.scene.layout.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{

        Pane pane = new HBox(10);
        pane.setPadding(new Insets(5,5,5,5));
        addcard(pane);

        Pane pane2 = new VBox(10);
        pane2.getChildren().add(pane);
        Button btRefresh = new Button("Refresh");
        pane2.getChildren().add(new StackPane(btRefresh));
        btRefresh.setOnAction(e -> {
            pane.getChildren().clear();
            addcard(pane);
        });
        Scene scene=new Scene(pane2);
        primaryStage.setTitle("card");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

    public void addcard(Pane pane){
        int[]a=new int [55];
        for(int i=1;i<=3;i++) {
            int x;
            do {
                x = (int) (Math.random() * 54 + 1);
            }
            while (a[x] != 0);
            a[x]++;
            Image image = new Image("image/card/" + x + ".png");
            pane.getChildren().add(new ImageView(image));
        }
    }
}

15.2
在这里插入图片描述

package hahaha;

import javafx.application.Application;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.control.Button;
import javafx.geometry.Insets;
import javafx.geometry.Pos;

public class demo2 extends Application
{
    private double angle = 0;
    @Override
    public void start(Stage primaryStage)
    {
        Pane pane = new VBox(15);
        pane.setPadding(new Insets(30, 80, 30, 80));
        Rectangle re = new Rectangle(25, 50, 25, 50);
        re.setFill(Color.WHITE);
        re.setStroke(Color.BLACK);
        Button btRotate = new Button("Rotate");
        Pane t1 = new StackPane();
        Pane t2 = new StackPane();
        t1.getChildren().add(re);
        t2.getChildren().add(btRotate);
        pane.getChildren().add(t1);
        pane.getChildren().add(t2);

//        pane.setAlignment(Pos.CENTER);
        btRotate.setOnAction(e -> {
            jiaodu(re);
        });

        primaryStage.setTitle("RotateARectangle");
        primaryStage.setScene(new Scene(pane));
        primaryStage.show();
    }

    public void jiaodu(Rectangle r)
    {
        angle += 15;
        r.setRotate(angle);
    }
}

15.3
在这里插入图片描述

package hahaha;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.geometry.Pos;

public class MoveBall extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        Pane pane = new Pane();
        Circle circle = new Circle(100, 100, 30);
        circle.setFill(Color.WHITE);
        circle.setStroke(Color.BLACK);

        Button up = new Button("Up");
        Button right = new Button("Right");
        Button down = new Button("Down");
        Button left = new Button("Left");
        HBox hbox = new HBox(5);
        hbox.setAlignment(Pos.CENTER);
        hbox.getChildren().add(left);
        hbox.getChildren().add(right);
        hbox.getChildren().add(up);
        hbox.getChildren().add(down);

        pane.getChildren().addAll(hbox,circle);

        up.setOnAction(e -> {
            if (circle.getCenterY() - 10 - 5 < 0)
            {
                circle.setCenterY(circle.getCenterY());
            }
            else
                circle.setCenterY(circle.getCenterY() - 5);
            circle.setCenterX(circle.getCenterX());
        });

        right.setOnAction(e -> {
            if (circle.getCenterX() + circle.getRadius() + 5 > pane.getWidth())
            {
                circle.setCenterX(circle.getCenterX());
            }
            else
                circle.setCenterX(circle.getCenterX() + 5);
            circle.setCenterY(circle.getCenterY());
        });

        down.setOnAction(e -> {
            if (circle.getCenterY() + circle.getRadius() + 5 > pane.getHeight())
            {
                circle.setCenterY(circle.getCenterY());
            }
            else
                circle.setCenterY(circle.getCenterY() + 5);
            circle.setCenterX(circle.getCenterX());
        });

        left.setOnAction(e -> {
            if (circle.getCenterX() - circle.getRadius() - 5 < 0)
            {
                circle.setCenterX(circle.getCenterX());
            }
            else
                circle.setCenterX(circle.getCenterX() - 5);
            circle.setCenterY(circle.getCenterY());
        });


        Scene scene = new Scene(pane, 500, 300);
        primaryStage.setTitle("Move");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值