猜拳游戏:有甲乙双方猜拳,投注其中一方,猜赢家,猜中即赢,猜错即输,四个按钮,一个是“甲方赢”,一个是“乙方赢”,一个“开始”,一个“停止”;两个图片,分别是甲乙方的石头剪刀布其中一个的图片。先点击“甲方赢”或“乙方赢”,然后点击“开始”,图片开始随机变换,最后点击“停止”,图片停止变换,输出一个输赢结果。
import java.awt.Color;
import java.awt.Font;
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 caiiquan extends JFrame
{
public int flag1;
public static void main(String[] args)
{
thread t1=new thread(0);
thread1 t2=new thread1(0);
Thread tt1=new Thread(t1);
Thread tt2=new Thread(t2);
caiquan c2=new caiquan();
JFrame frame=new JFrame();
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JButton jiawin=new JButton("甲方赢");
JButton yiwin=new JButton("乙方赢");
JButton begin=new JButton("开始");
JButton stop=new JButton("停止");
JLabel scissors,fist,cloth;
JLabel j=new JLabel("请玩家选择按钮:");
JLabel jia=new JLabel("甲出:");
JLabel yi=new JLabel("乙出:");
frame.setTitle("猜拳游戏");
frame.setLayout(null);
frame.setSize(1200,1200);
frame.setVisible(true);
panel1.setSize(600,600);
panel2.setBounds(0,600,800,800);
panel3.setBounds(0,300,800,800);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
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);
frame.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);
jia.setBounds(100,700,100,100);
yi.setBounds(300,700,100,100);
panel1.add(yiwin);
panel1.add(jiawin);
panel1.add(begin);
panel1.add(stop);
JLabel j1=new JLabel("结果:");
j1.setFont(new Font("宋体",Font.BOLD,20));
panel2.add(j1);
JTextField t=new JTextField(30);
t.setSize(100,100);
t.setEditable(false);
panell.add(t);
JButton btn1=new JButton();
JButton btn=new JButton();
panel3.add(jia);
panel3.add(btn);
panel3.add(yi);
panel3.add(btn1);
jiawin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c2.flag1=1;
}
});
yiwin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c2.flag1=2;
}
});
begin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tt1.start();
tt2.start();
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tt1.stop();
tt2.stop();
if((t1.flag==0&&t2.flag==2&&c2.flag1==1)||(t1.flag==1&&t2.flag==0&&c2.flag1==1)||(t1.flag==2&&t2.flag==1&&c2.flag1==1))
{
t.setText("你赢了");
}
else if((t1.flag==0&&t2.flag==1&&c2.flag1==2)||(t1.flag==1&&t2.flag==2&&c2.flag1==2)||(t1.flag==2&&t2.flag==0&&c2.flag1==2))
{
t.setText("你赢了");
}
else if((t1.flag==t2.flag&&c2.flag1==1)||(t1.flag==t2.flag&&c2.flag1==2))
{
t.setText("平局");
}
else if((t1.flag==0&&t2.flag==2&&c2.flag1==2)||(t1.flag==1&&t2.flag==0&&c2.flag1==2)||(t1.flag==2&&t2.flag==1&&c2.flag1==2))
{
t.setText("你输了");
}
else if((t1.flag==0&&t2.flag==1&&c2.flag1==1)||(t1.flag==1&&t2.flag==2&&c2.flag1==1)||(t1.flag==2&&t2.flag==0&&c2.flag1==1))
{
t.setText("你输了");
}
}
});
class thread implements Runnable
{
public int flag;
public void run()
{
try
{
for(int i=0;;i++)
{
flag=(int)(Math.random()*3);
if(flag==0)
{btn.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\bu.jpg")); }//甲
else if (flag==1)
{btn.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\jiandao.jpg")); }
else if(flag==2){btn.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\shitou.jpg")); }
}}
catch(Exception e)
{e.printStackTrace();}
}
}
class thread1 implements Runnable
{
public int flag;
public void run()
{
try
{
for(int i=0;;i++)
{
flag=(int)(Math.random()*3);
if(flag==0)
{btn1.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\bu.jpg")); }
else if (flag==1)
{btn1.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\jiandao.jpg")); }
else if(flag==2){btn1.setIcon(new ImageIcon("A:\\zhaiwa\\eclipse\\shitou.jpg")); }
}}
catch(Exception e)
{e.printStackTrace();}
}
}
}}
我点击开始按钮的时候为什么会弹出两个新的窗口变换图片?停止的时候结果也不正确,有什么办法能让两张图片转换显示在panel3中,代码该怎么改?