Java Swing编写的一个猜拳小游戏

18年在学校时候写的,整理目录发现的,这个游戏当时是模仿一个微信小程序
效果截图:
在这里插入图片描述
里面所用到的四张图片放在百度云:
在这里插入图片描述

链接:https://pan.baidu.com/s/1pnbcOCDpHzA-h2s_56wDWg
提取码:hjvv
代码在此:

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class Caiquan {

    public static void main(String[] args) {
        new Thread(new Quansun()).start();
    }

}

class Quansun extends JFrame implements Runnable {
    int x; // 图片循环
    int n = 0; // 获胜次数
    boolean yes;
    JLabel biaoti = new JLabel("你已经获胜0次", JLabel.CENTER);
    JLabel bian = new JLabel();
    JLabel my = new JLabel();
    JLabel xinxi = new JLabel("", JLabel.CENTER); // 结果提示
    JButton bu = new JButton("开始!");

    public Quansun() {
        setTitle("猜拳游戏");
        setSize(399, 650);
        setLocationRelativeTo(null);
        Container con = getContentPane();
        con.setBackground(new Color(250, 236, 56));
        con.setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JLabel name = new JLabel("付付付   2018");       //个人信息
        name.setFont(new Font("微软雅黑", Font.LAYOUT_LEFT_TO_RIGHT, 20));
        name.setBounds(95, 565, 199, 30);
        con.add(name);
        biaoti.setFont(new Font("宋体", Font.BOLD, 16));     //成绩
        biaoti.setBounds(75, 15, 232, 22);
        bian.setBounds(35, 55, 90, 90);
        bian.setIcon(new ImageIcon(Caiquan.class.getResource("/1.png")));
        my.setBounds(255, 55, 90, 90);
        my.setIcon(new ImageIcon(Caiquan.class.getResource("/one.png")));
        xinxi.setForeground(Color.RED);
        xinxi.setBounds(135, 55, 120, 90);
        xinxi.setFont(new Font("微软雅黑", Font.LAYOUT_LEFT_TO_RIGHT, 16));
        con.add(bian);
        con.add(biaoti);
        con.add(my);
        con.add(xinxi);
        JPanel pan = new JPanel(null);
        pan.setBackground(new Color(255, 255, 255));
        pan.setBounds(15, 200, 350, 350);
        con.add(pan);
        JLabel boy = new JLabel("出拳吧,骚年", JLabel.CENTER);
        boy.setFont(new Font("宋体", Font.BOLD, 16));
        boy.setBounds(105, 15, 145, 25);
        pan.add(boy);
        JPanel pan2 = new JPanel(new GridLayout(1, 3, 20, 0));
        pan2.setBounds(25, 55, 300, 90);
        pan2.setBackground(new Color(255, 255, 255));
        bu.setFont(new Font("微软雅黑", 666, 22));
        bu.setBackground(new Color(250, 236, 56));
        bu.setBounds(39, 275, 275, 45);
        pan.add(bu);
        pan.add(pan2);
        JButton jd = new JButton();
        JButton st = new JButton();
        JButton b = new JButton();
        jd.setIcon(new ImageIcon(Caiquan.class.getResource("/1.png")));
        st.setIcon(new ImageIcon(Caiquan.class.getResource("/2.png")));
        b.setIcon(new ImageIcon(Caiquan.class.getResource("/3.png")));
        pan2.add(jd);
        pan2.add(st);
        pan2.add(b);

        bu.addActionListener(new ActionListener() { // 开始按钮

            @Override
            public void actionPerformed(ActionEvent e) {
                yes = true;
                my.setIcon(new ImageIcon(Caiquan.class.getResource("/one.png")));
                xinxi.setText("");
            }
        });
        jd.addActionListener(new ActionListener() { // 剪刀

            @Override
            public void actionPerformed(ActionEvent e) {
                if (yes == true) {
                    yes = false;
                    my.setIcon(new ImageIcon(Caiquan.class.getResource("/1.png")));
                    bu.setText("再来!");
                    if (x == 1) {
                        xinxi.setText("打平手了!");
                    } else if (x == 2) {
                        xinxi.setText("你输了!");
                    } else if (x == 3) {
                        xinxi.setText("你赢了!");
                        n++;
                        biaoti.setText("你已经获胜" + n + "次");
                    }
                }
            }
        });
        st.addActionListener(new ActionListener() { // 石头

            @Override
            public void actionPerformed(ActionEvent e) {
                if (yes == true) {
                    yes = false;
                    my.setIcon(new ImageIcon(Caiquan.class.getResource("/2.png")));
                    bu.setText("再来!");
                    if (x == 2) {
                        xinxi.setText("打平手了!");
                    } else if (x == 3) {
                        xinxi.setText("你输了!");
                    } else if (x == 1) {
                        xinxi.setText("你赢了!");
                        n++;
                        biaoti.setText("你已经获胜" + n + "次");
                    }
                }
            }
        });
        b.addActionListener(new ActionListener() { // 布

            @Override
            public void actionPerformed(ActionEvent e) {
                if (yes == true) {
                    yes = false;
                    my.setIcon(new ImageIcon(Caiquan.class.getResource("/3.png")));
                    bu.setText("再来!");
                    if (x == 3) {
                        xinxi.setText("打平手了!");
                    } else if (x == 1) {
                        xinxi.setText("你输了!");
                    } else if (x == 2) {
                        xinxi.setText("你赢了!");
                        n++;
                        biaoti.setText("你已经获胜" + n + "次");
                    }
                }
            }
        });
        setVisible(true);
    }

    public void run() {
        for (; ; ) {
            while (yes) {
                x = new Random().nextInt(3) + 1;
                bian.setIcon(new ImageIcon(Caiquan.class.getResource("/" + x + ".png")));
            }
            try {
                Thread.sleep(125);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

注意,图片文件放在resources下面,因为代码是通过getResource()方法获取的(注意里面的/)。
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值