更好的划拳游戏
import javax.swing.*;import javax.swing.JTextArea;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class SmallGame extends JFrame implements ActionListener
{ Container container1;
JPanel panel1,panel2, panel3;
JButton ok,clear;
JLabel label1;
JTextArea textarea;
JComboBox combobox;
JScrollPane scroll;
String []box={"剪刀","石头","布"};
int win=0;
int loss=0;
int equal=0;
Random r;
public SmallGame()
{ this.setLocation(200,200);
this.setSize(300,200);
this.setTitle("划拳游戏");
this.setResizable(true);
this.setDefaultCloseOperation(3);
JComboBox();
this.panel1=new JPanel();
this.panel2=new JPanel();
this.panel3=new JPanel();
for(int i=0;i<box.length;i++)
(this.box[i]);
this.panel1.add();
this.ok=new JButton("出招");
this.ok.addActionListener(this);
this.panel1.add(this.ok);
this.clear=new JButton("清除");
this.clear.addActionListener(this);
this.panel1.add(this.clear);
this.textarea=new JTextArea(2,15);
this.textarea.setFont(new Font("楷体",Font.BOLD,15));
this.textarea.setEditable(false);
this.scroll=new JScrollPane(this.textarea);
this.panel2.add(this.scroll);
this.label1=new JLabel();
this.label1.setFont(new Font("楷体",Font.BOLD,20));
this.panel3.add(this.label1);
this.container1=getContentPane();
this.container1.setLayout(new BorderLayout());
this.container1.add(this.panel1,BorderLayout.NORTH);
this.container1.add(this.panel2,BorderLayout.CENTER);
this.container1.add(this.panel3,BorderLayout.SOUTH);
r=new Random();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==ok)
{this.textarea.setText(this.getResult());
this.label1.setText(this.getTotal());
}
if(e.getSource()==clear)
{ this.textarea.setText("");
this.win=0;
this.loss=0;
this.equal=0;
this.label1.setText(this.getTotal());
}
}
public String getResult()
{
String str="";
int people=();
int computer=this.getComputer();
str+="人:\t"+box[people];
str+="\n电脑:\t"+box[computer];
str+="\n结果: \t"+check(people,computer);
return str;
}
public int getComputer()
{return r.nextInt(3);
}
public String check(int people,int computer)
{
String result="";
if(people==(computer+1)%3)
{result="恭喜,你赢了";
win++;}
else if (people==computer)
{result="平";
equal++;
}
else
{result="你输了";
loss++;
}
return result;
}
public String getTotal()
{return " 赢:"+win+" 平:"+equal+" 输:"+loss+" 得分:"+getPoint();
}
public int getPoint()
{return (win-loss)*10;
}
public static void main(String []args)
{ SmallGame huaqua=new SmallGame();
}
}