Java实现石头剪刀布小游戏

2022-07-25,,,

本文实例为大家分享了java实现石头剪刀布的具体代码,供大家参考,具体内容如下

代码:

package com.neusoft.test;

import java.awt.borderlayout;
import java.awt.choice;
import java.awt.color;
import java.awt.gridlayout;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.itemevent;
import java.awt.event.itemlistener;
import javax.swing.imageicon;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jpanel;

public class test05 extends jframe implements actionlistener, itemlistener{
  int m = 1;
  int n = 2;
  int peace = 0;
  int win = 0;
  int lose = 0;
  int all = 0;
  jbutton jb1,jb2;
  jlabel jl1,jl2,jl2i,jl3,jl3i,jl4,jl5;
  imageicon img1,img2;
  choice ch;
  jpanel jp1,jp2,jp3,jp4,jp5,jp6;

  public test05(){
    jb1 = new jbutton("确定出拳");
    jb2 = new jbutton("退出");
    
    //设置监听 
    jb1.addactionlistener(this); 
    jb2.addactionlistener(this); 
    
    jl1 = new jlabel("猜拳小游戏");
    jl2 = new jlabel("我方出拳");
    jl3 = new jlabel("敌方出拳");
    jl4 = new jlabel("请出拳...");
    jl5 = new jlabel("您玩了"+all+"局,平了"+peace+"局,赢了"+win+"局,输了"+lose+"局");

    system.out.println(m);
    img1 = new imageicon("d:/mkn/test/img/"+m+".png");
    img2 = new imageicon("d:/mkn/test/img/"+n+".png");
    
    jl2i = new jlabel(img1);
    jl3i = new jlabel(img2);
    
    ch = new choice();
    ch.add("石头");
    ch.add("剪刀");
    ch.add("布");
    ch.additemlistener(this);

    jp1 = new jpanel(new borderlayout());
    jp2 = new jpanel();
    jp3 = new jpanel(new borderlayout());
    jp4 = new jpanel(new borderlayout());
    jp5 = new jpanel(new gridlayout(5,1));
    jp6 = new jpanel(new borderlayout());
    
    jp2.setsize(600, 25);
    jp2.setbackground(color.red);
    jp3.setsize(280, 300);
    jp4.setsize(280, 300);
    jp5.setsize(40, 300);
    jp6.setsize(600, 25);
//    jp6.setbackground(color.gray);
    jp2.add(jl1); //标题
    jp3.add(jl2,borderlayout.north); //
    jp3.add(jl2i,borderlayout.center);
    jp4.add(jl3,borderlayout.north);
    jp4.add(jl3i, borderlayout.center);//敌方出拳
    jp5.add(ch);
    jp5.add(jb1);
    jp5.add(jb2);
    jp6.add(jl4,borderlayout.west);
    jp6.add(jl5,borderlayout.east);//结果
    
    jp1.add(jp2,borderlayout.north);
    jp1.add(jp3,borderlayout.west);
    jp1.add(jp4,borderlayout.center);
    jp1.add(jp5,borderlayout.east);
    jp1.add(jp6,borderlayout.south);
    
    this.add(jp1);
    
    this.settitle("猜拳小游戏");     
    this.setsize(540,300); 
//   this.setlayout(new borderlayout());
    this.setlocationrelativeto(null);  //居中    
    this.setdefaultcloseoperation(jframe.exit_on_close);
    this.setvisible(true);
    this.setresizable(false); //窗口大小可变
    
  }
  
  @override
  public void actionperformed(actionevent e) {
    // todo auto-generated method stub
    if(e.getactioncommand()=="确定出拳"){
      n = (int)(1+math.random()*(3));
      jl3i.seticon(new imageicon("d:/mkn/test/img/"+n+".png"));
      if (m - n == 0) {
        jl4.settext("您和计算机平局");
        peace++;
      }
      if (m - n == -1||m - n == 2) {
        jl4.settext("您赢了!!!");
        win++;
      }
      if (m - n == -2||m - n == 1) {
        jl4.settext("您输了!!!");
        lose++;
      }
      all++;
      jl5.settext("您玩了"+all+"局,平了"+peace+"局,赢了"+win+"局,输了"+lose+"局");
//      system.out.println(n);
    }else{
      this.dispose();
    }
  }
  public static void main(string[] args) {
    test05 test05 = new test05();
  }

  @override
  public void itemstatechanged(itemevent e) {
    if(e.getsource()==ch){
      choice temp=(choice)e.getsource();
      if (temp.getselecteditem() == "石头") {
        m = 1;
      }
      if (temp.getselecteditem() == "剪刀") {
        m = 2;
      }
      if (temp.getselecteditem() == "布") {
        m = 3;
      }
      jl2i.seticon(new imageicon("d:/mkn/test/img/"+m+".png")); 
//      system.out.println(m);
      }
    
  }

  
}

运行效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《Java实现石头剪刀布小游戏.doc》

下载本文的Word格式文档,以方便收藏与打印。