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()方法获取的(注意里面的/)。