Java实现石头剪刀布小游戏
作者:MKN 时间:2023-02-25 22:41:29
本文实例为大家分享了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);
}
}
}
运行效果:
来源:https://www.cnblogs.com/makangning/p/9387644.html
标签:java,石头剪刀布
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#静态static的用法实例分析
2022-06-30 09:37:27
C#9新特性之增强的模式匹配
2023-04-16 07:35:34
Android实现自定义轮播图片控件示例
2021-09-19 01:27:05
![](https://img.aspxhome.com/file/2023/8/96778_0s.gif)
C#利用VS中插件打包并发布winfrom程序
2022-01-01 21:18:22
![](https://img.aspxhome.com/file/2023/6/91336_0s.jpg)
Java毕业设计实战之教室预订管理系统的实现
2023-03-03 20:38:11
![](https://img.aspxhome.com/file/2023/3/74873_0s.jpg)
C#实现将汉字转化为2位大写的16进制Unicode的方法
2022-03-11 21:45:07
JAVA判断空值方法原理解析
2021-09-28 14:37:34
一文详解Java线程的6种状态与生命周期
2022-02-08 08:44:30
![](https://img.aspxhome.com/file/2023/5/122955_0s.jpg)
C++异常处理 try,catch,throw,finally的用法
2021-08-21 21:29:55
java基础之字符串编码知识点总结
2023-06-20 19:05:09
![](https://img.aspxhome.com/file/2023/3/128843_0s.png)
深入讲解基于JDK的动态代理机制
2022-07-19 07:13:32
![](https://img.aspxhome.com/file/2023/8/78328_0s.png)
C# 崩溃异常中研究页堆布局的详细过程
2022-11-06 17:04:08
![](https://img.aspxhome.com/file/2023/9/129649_0s.png)
c# 通过代码开启或关闭防火墙
2021-12-11 19:53:20
Swagger2配置方式(解决404报错)
2022-08-30 17:21:13
![](https://img.aspxhome.com/file/2023/0/63660_0s.png)
UE4 Unlua 调用异步蓝图节点AIMoveTo函数示例详解
2022-04-12 05:35:41
C#实现把txt文本数据快速读取到excel中
2022-10-15 02:59:10
Java使用Thread和Runnable的线程实现方法比较
2021-11-17 07:52:54
Android使用Gallery实现照片拖动的特效
2022-12-31 23:04:22
java对double数组排序示例分享
2022-07-13 03:22:14
C# DataTable 转换为 实体类对象实例
2021-12-25 02:14:51