JavaFX实现石头剪刀布小游戏
用JavaFX写一个石头剪刀布的小游戏,供大家参考,具体内容如下
课程上布置的作业,其实java很少用来写这种程序的
-GUI界面部分
-石头剪刀布的逻辑部分
书上教的是JavaFX,其实还有很多方法,书上说AWT和Swing基本上淘汰了,但事实上…
代码
MyJavaFX.java
packagecn.homework; importjavafx.application.Application; importjavafx.event.ActionEvent; importjavafx.event.EventHandler; importjavafx.geometry.Pos; importjavafx.scene.Scene; importjavafx.scene.control.Button; importjavafx.scene.control.Label; importjavafx.scene.layout.BorderPane; importjavafx.scene.layout.GridPane; importjavafx.scene.image.Image; importjavafx.scene.image.ImageView; importjavafx.scene.layout.StackPane; importjavafx.scene.text.*; importjavafx.scene.text.Font; importjavafx.stage.Stage; importjava.awt.*; publicclassMyJavaFXextendsApplication{ Lablepanelablepane=newLablepane(); //Stringstr1; // //publicvoidsetStr1(Stringstr1){ //this.str1=str1; //} @Override publicvoidstart(StageprimaryStage)throwsException{ GridPanepane=newGridPane(); pane.setAlignment(Pos.CENTER); pane.setHgap(5); Labellabel=newLabel("开始游戏:"); label.setFont(Font.font("TimesNewRoman",FontWeight.BOLD,FontPosture.ITALIC,30)); pane.add(label,0,0); //布 Imageimage1=newImage("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1521291025265&di=a0bc9a448dfcaac150e064577e6dabfd&imgtype=0&src=http%3A%2F%2Fimg1.ali213.net%2Fshouyou%2Fcover%2Fc%2F48302.jpg"); ImageViewimageView1=newImageView(image1); imageView1.setFitHeight(150); imageView1.setFitWidth(150); pane.add(imageView1,0,1); Image1HanderClassimage1HanderClass=newImage1HanderClass(); ButtonbtOK1=newButton("布"); pane.add(btOK1,0,2); btOK1.setOnAction(image1HanderClass); //石头 Imageimage2=newImage("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1419901674,657140521&fm=27&gp=0.jpg"); ImageViewimageView2=newImageView(image2); imageView2.setFitHeight(150); imageView2.setFitWidth(150); pane.add(imageView2,1,1); Image2HanderClassimage2HanderClass=newImage2HanderClass(); ButtonbtOK2=newButton("石头"); pane.add(btOK2,1,2); btOK2.setOnAction(image2HanderClass); //剪刀 Imageimage3=newImage("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2460250897,3788214935&fm=27&gp=0.jpg"); ImageViewimageView3=newImageView(image3); imageView3.setFitHeight(150); imageView3.setFitWidth(150); pane.add(imageView3,2,1); Image3HanderClassimage3HanderClass=newImage3HanderClass(); ButtonbtOK3=newButton("剪刀"); pane.add(btOK3,2,2); btOK3.setOnAction(image3HanderClass); BorderPaneborderPane=newBorderPane(); borderPane.setCenter(pane); borderPane.setBottom(lablepane); BorderPane.setAlignment(pane,Pos.CENTER); //游戏结束 //Labellabel1=newLabel("111"); //label1.setFont(Font.font("TimesNewRoman",FontWeight.BOLD,FontPosture.ITALIC,30)); //pane.add(label1,0,3); pane.setStyle("-fx-background-color:#0effb8"); Scenescene=newScene(borderPane,800,500); primaryStage.setTitle("石头剪刀布"); primaryStage.setScene(scene); primaryStage.show(); } classImage1HanderClassimplementsEventHandler{ //布2 @Override publicvoidhandle(ActionEventevent){ Gamegame=newGame(); Stringstr=game.playgame(2); //setStr1(str); lablepane.show(str); System.out.println(str); } } classImage2HanderClassimplementsEventHandler { //石头0 @Override publicvoidhandle(ActionEventevent){ Gamegame=newGame(); Stringstr=game.playgame(0); //setStr1(str); lablepane.show(str); System.out.println(str); } } classImage3HanderClassimplementsEventHandler { //剪刀1 @Override publicvoidhandle(ActionEventevent){ Gamegame=newGame(); Stringstr=game.playgame(1); //setStr1(str); lablepane.show(str); System.out.println(str); } } classLablepaneextendsStackPane{ privateLabellabel=newLabel("结果"); publicLablepane(){ getChildren().add(label); label.setFont(Font.font("TimesNewRoman",FontWeight.BOLD,FontPosture.ITALIC,30)); } publicvoidshow(Stringstr){ label.setText(str); } } }
代码
Game.java
packagecn.homework; importjava.util.Random; importjava.util.Scanner; publicclassGame{ publicstaticvoidmain(String[]args){ Stringresult=playgame(0); System.out.println(result); } publicstaticStringplaygame(intuser){ //用户输入 //System.out.println("请输入您要出的:如果出石头请输入0如果出剪刀请输入1如果出布请输入2"); //系统生成 Randomxx=newRandom();//声明随机数 intnumber=xx.nextInt(3);//赋值随机数给number //比较 if(user==0&&number==0){ return"你出的是石头,系统出的是石头,双方平局。"; } elseif(user==1&&number==0){ return"你出的是剪刀,系统出的是石头,你输了。"; } elseif(user==2&&number==0){ return"你出的是布,系统出的是石头,你赢了。"; } elseif(user==0&&number==1){ return"你出的是石头,系统出的是剪刀,你赢了。"; } elseif(user==1&&number==1){ return"你出的是石头,系统出的是石头,双方平局。"; } elseif(user==2&&number==1){ return"你出的是布,系统出的是剪刀,你输了。"; } elseif(user==0&&number==2){ return"你出的是石头,系统出的是布,你输了。"; } elseif(user==1&&number==2){ return"你出的是剪刀,系统出的是布,你赢了。"; } else{ return"你出的是布,系统出的是布,双方平局。"; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。