1,系统简要说明:系统利用Java Swing创建窗口以及按钮,内置多线程实现玩家进行猜测之后,甲乙随机图片的转动,按停止按钮后停止图片的转动,并用Text文本框输出结果。
利用多线程以及生成随机数进行图片的轮转,停止后比较随机数的值进行大小的判断
import java.awt.Color;
import java.awt.Font;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.lang.Thread;
import javax.swing.JTextField;
import java.util.Random;
import java.lang.Runnable;
public class caiquancankao extends JFrame
{
private JPanel panel=new JPanel();
private JPanel panell=new JPanel();
private JPanel panelll=new JPanel();
private JTextField t=new JTextField(30);
private JButton btnn=new JButton();
private JButton btn=new JButton();
public int flag;
public boolean flagg=false;
public int flagjia;
public int flagyi;
Thread thr=new Thread(new mythread());
public caiquancankao()
{
Container con=getContentPane();
JButton jiawin=new JButton("甲方赢");
JButton yiwin=new JButton("乙方赢");
JButton begin=new JButton("开始");
JButton stop=new JButton("停止");
JButton rebegin=new JButton("重新开始");
JLabel jia=new JLabel("甲出:");
JLabel yi=new JLabel("乙出:");
JLabel j=new JLabel("请玩家选择按钮:");
setTitle("猜拳游戏");
setLayout(null);
setSize(1200,1200);
setVisible(true);
panel.setSize(600,600);
panell.setBounds(0,600,800,800);
panelll.setBounds(0,300,800,800);
con.add(panel);
con.add(panell);
con.add(panelll);
j.setBounds(100,500,50,50);
j.setFont(new Font("宋体",Font.BOLD,20));
jia.setFont(new Font("宋体",Font.BOLD,20));
yi.setFont(new Font("宋体",Font.BOLD,20));
panel.add(j);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jiawin.setBounds(100,500,100,100);
yiwin.setBounds(200,500,100,100);
begin.setBounds(300,500,100,100);
stop.setBounds(400,500,100,100);
rebegin.setBounds(500,500,100,100);
jia.setBounds(100,700,100,100);
yi.setBounds(300,700,100,100);
panel.add(jiawin);
panel.add(yiwin);
panel.add(begin);
panel.add(stop);
panel.add(rebegin);
JLabel j1=new JLabel("结果:");
j1.setFont(new Font("宋体",Font.BOLD,20));
panell.add(j1);
JTextField t=new JTextField(30);
t.setSize(100,100);
t.setEditable(false);
panell.add(t);
panelll.add(jia);
panelll.add(btn);
//btn.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\bu.jpg"));
panelll.add(yi);
//btnn.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\bu.jpg"));
panelll.add(btnn);
jiawin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag=1;
}
});
yiwin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag=2;
}
});
begin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flagg=true;
thr.start();
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flagg=false;
thr.stop();
//0布 1剪刀 2石头 1甲赢 2乙赢
if((flagjia==0&&flagyi==2&&flag==1)||(flagjia==1&&flagyi==0&&flag==1)||(flagjia==2&&flagyi==1&&flag==1))
{
t.setText("你赢了");
}//甲赢
else if((flagjia==0&&flagyi==1&&flag==2)||(flagjia==1&&flagyi==2&&flag==2)||(flagjia==2&&flagyi==0&&flag==2))
{
t.setText("你赢了");
}//乙赢
else if((flagjia==flagyi&&flagjia==1)||(flagjia==flagyi&&flag==2))
{
t.setText("平局");
}
else if((flagjia==0&&flagyi==2&&flag==2)||(flagjia==1&&flagyi==0&&flag==2)||(flagjia==2&&flagyi==1&&flag==2))
{
t.setText("你输了");
}//乙输
else if((flagjia==0&&flagyi==1&&flag==1)||(flagjia==1&&flagyi==2&&flag==1)||(flagjia==2&&flagyi==0&&flag==1))
{
t.setText("你输了");
}//甲输
}
});
rebegin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
caiquancankao cc=new caiquancankao();
}
});
}
class mythread implements Runnable
{
public void run()
{
if(flagg=true)
{
for(int i=0;;i++)
{
flagjia=(int)(Math.random()*3);
if(flagjia==0)
{ btn.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\bu.jpg")); }
else if (flagjia==1)
{ btn.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\jiandao.jpg")); }
else if(flagjia==2){btn.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\shitou.jpg")); }
flagyi=(int)(Math.random()*3);
if(flagyi==0)
{ btnn.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\bu.jpg")); }
else if (flagyi==1)
{ btnn.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\jiandao.jpg")); }
else if(flagyi==2){btnn.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\shitou.jpg")); }
try
{
Thread.sleep(100);
}
catch(Exception e)
{e.printStackTrace();}
}
}
}
}
public static void main(String[] args)
{
caiquancankao cc=new caiquancankao();
}
}
游戏界面: