使用JavaFX怎么实现石头剪刀布小游戏

发布时间:2021-05-27 17:57:35 作者:Leah
来源:亿速云 阅读:169

使用JavaFX怎么实现石头剪刀布小游戏?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

MyJavaFX.java

package cn.homework;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.text.*;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.awt.*;


public class MyJavaFX extends Application {

 Lablepane lablepane=new Lablepane();
// String str1;
//
// public void setStr1(String str1) {
//  this.str1 = str1;
// }

 @Override
 public void start(Stage primaryStage) throws Exception {
  GridPane pane = new GridPane();
  pane.setAlignment(Pos.CENTER);
  pane.setHgap(5);

  Label label = new Label("开始游戏:");
  label.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 30));
  pane.add(label, 0, 0);
  //布
  Image image1 = new Image("https://cache.yisu.com/upload/information/20200623/121/96291.html");
  ImageView imageView1 = new ImageView(image1);
  imageView1.setFitHeight(150);
  imageView1.setFitWidth(150);
  pane.add(imageView1, 0, 1);
  Image1HanderClass image1HanderClass = new Image1HanderClass();
  Button btOK1 = new Button("布");
  pane.add(btOK1, 0, 2);
  btOK1.setOnAction(image1HanderClass);
  //石头
  Image image2 = new Image("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1419901674,657140521&fm=27&gp=0.jpg");
  ImageView imageView2 = new ImageView(image2);
  imageView2.setFitHeight(150);
  imageView2.setFitWidth(150);
  pane.add(imageView2, 1, 1);
  Image2HanderClass image2HanderClass = new Image2HanderClass();
  Button btOK2 = new Button("石头");
  pane.add(btOK2, 1, 2);
  btOK2.setOnAction(image2HanderClass);
  //剪刀
  Image image3 = new Image("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2460250897,3788214935&fm=27&gp=0.jpg");
  ImageView imageView3 = new ImageView(image3);
  imageView3.setFitHeight(150);
  imageView3.setFitWidth(150);
  pane.add(imageView3, 2, 1);
  Image3HanderClass image3HanderClass = new Image3HanderClass();
  Button btOK3 = new Button("剪刀");
  pane.add(btOK3, 2, 2);
  btOK3.setOnAction(image3HanderClass);


  BorderPane borderPane=new BorderPane();
  borderPane.setCenter(pane);
  borderPane.setBottom(lablepane);
  BorderPane.setAlignment(pane,Pos.CENTER);


//  游戏结束
//  Label label1 = new Label("111");
//  label1.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 30));
//  pane.add(label1, 0, 3);

  pane.setStyle("-fx-background-color: #0effb8");

  Scene scene = new Scene(borderPane, 800, 500);
  primaryStage.setTitle("石头剪刀布");
  primaryStage.setScene(scene);
  primaryStage.show();
 }


 class Image1HanderClass implements EventHandler<ActionEvent> {

  //布2
  @Override
  public void handle(ActionEvent event) {
   Game game = new Game();
   String str = game.playgame(2);
   //setStr1(str);
   lablepane.show(str);
   System.out.println(str);

  }
 }

 class Image2HanderClass implements EventHandler<ActionEvent> {

  //石头0
  @Override
  public void handle(ActionEvent event) {
   Game game = new Game();
   String str = game.playgame(0);
   //setStr1(str);
   lablepane.show(str);
   System.out.println(str);
  }
 }

 class Image3HanderClass implements EventHandler<ActionEvent> {

  //剪刀1
  @Override
  public void handle(ActionEvent event) {
   Game game = new Game();
   String str = game.playgame(1);
   //setStr1(str);
   lablepane.show(str);
   System.out.println(str);
  }
 }
 class Lablepane extends StackPane{
  private Label label=new Label("结果");
  public Lablepane(){
   getChildren().add(label);
   label.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 30));
  }
  public void show(String str){
   label.setText(str);
  }
 }

}

代码

Game.java

package cn.homework;
import java.util.Random;
import java.util.Scanner;
public class Game {
 public static void main(String []args){
  String result=playgame(0);
  System.out.println(result);
 }
 public static String playgame(int user){
  //用户输入
  //System.out.println("请输入您要出的:如果出石头请输入0 如果出剪刀请输入1 如果出布请输入2");
  //系统生成
  Random xx = new Random(); //声明随机数
  int number = xx.nextInt(3); //赋值随机数给number
  //比较
  if(user==0 && number==0){
   return "你出的是石头,系统出的是石头,双方平局。";
  }
  else if(user==1 && number==0){
   return "你出的是剪刀,系统出的是石头,你输了。";
  }
  else if(user==2 && number==0){
   return "你出的是布,系统出的是石头,你赢了。";
  }
  else if(user==0 && number==1){
   return "你出的是石头,系统出的是剪刀,你赢了。";
  }
  else if(user==1 && number==1){
   return "你出的是石头,系统出的是石头,双方平局。";
  }
  else if(user==2 && number==1){
   return "你出的是布,系统出的是剪刀,你输了。";
  }
  else if(user==0 && number==2){
   return "你出的是石头,系统出的是布,你输了。";
  }
  else if(user==1 && number==2){
   return "你出的是剪刀,系统出的是布,你赢了。";
  }
  else{
   return "你出的是布,系统出的是布,双方平局。";
  }
 }

}

看完上述内容,你们掌握使用JavaFX怎么实现石头剪刀布小游戏的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Python实现剪刀石头布小游戏
  2. Python之石头剪刀布

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javafx

上一篇:使用SpringBoot怎么实现文件上传与下载

下一篇:怎么在Spring Mvc中以文件流方式下载文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》