java代码猜拳 java猜拳代码简单

求JAVA人机猜拳的代码 , 类似一下界面 。自己纯手打,老半天才弄出来啊
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Demo2 extends JFrame {
private JLabel lb1, lb2, lb3, lb4; // 提示标签
private JTextField ta1, ta2;// 两个文本框
private JButton b1, b2, b3; // 三个按钮
private JPanel p1, p2; // 两个JPanel面板
public Demo2() {
// 初始化所有组件
lb1 = new JLabel("欢迎使用人机猜拳程序");
lb2 = new JLabel("你出拳:");
lb3 = new JLabel("电脑出拳:");
lb4 = new JLabel("结果");
ta1 = new JTextField();
ta1.setPreferredSize(new Dimension(60, 60)); // 设置大小
ta1.setEditable(false);//设置不可编辑
ta2 = new JTextField();
ta2.setPreferredSize(new Dimension(60, 60));
ta2.setEditable(false);//设置不可编辑
b1 = new JButton("剪刀");
b2 = new JButton("石头");
b3 = new JButton("布");
p1 = new JPanel();
p2 = new JPanel();
// 设置第一个面板内容
Box box = Box.createVerticalBox();
Box box1 = Box.createHorizontalBox();
box1.add(lb2);
box1.add(ta1);
box1.add(lb3);
box1.add(ta2);
box.add(lb1);
box.add(Box.createVerticalStrut(40));
box.add(box1);
box.add(Box.createVerticalStrut(10));
box.add(lb4);
box.add(new JLabel());
p1.add(box);
// 设置第二个面板
p2.setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器
p2.setPreferredSize(new Dimension(0, 60));
GridBagConstraints g2 = new GridBagConstraints();
g2.fill = GridBagConstraints.BOTH;
g2.weightx = 1.0;
g2.weighty = 1.0;
g2.gridx = 0;
g2.gridy = 0;
p2.add(b1, g2);
g2.gridx = 1;
p2.add(b2, g2);
g2.gridx = 2;
p2.add(b3, g2);
//为3个按钮添加事件
b1.addActionListener(new buttonAction());
b2.addActionListener(new buttonAction());
b3.addActionListener(new buttonAction());
this.getContentPane().add(p1);
this.getContentPane().add(p2, BorderLayout.SOUTH);
this.setTitle("机器人猜拳游戏");
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//事件类
class buttonAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
ta1.setText("剪刀");
init(ta1.getText());
}else if(e.getSource()==b2){
ta1.setText("石头");
init(ta1.getText());
}else if(e.getSource()==b3){
ta1.setText("布");
init(ta1.getText());
}
}
// 模拟电脑出拳,产生三个随机数 。0代表剪刀 , 1代表石头,2代表布
public String getQuan(){
String str="";
int num=new Random().nextInt(3) ;
if(num==0){
str="剪刀";
}else if(num==1){
str="石头";
}else if(num==2){
str="布";
}
return str;
}
// 判断输赢方法
public String isying(String s1,String s2){
String s="";
if(s1.equals(s2)){
s="平局";
}else if(s1.equals("剪刀")s2.equals("布")){
s="你赢";
}else if(s1.equals("石头")s2.equals("剪刀")){
s="你赢";
}else if(s1.equals("布")s2.equals("石头")){
s="你赢";
}else{
s="电脑赢";
}
return s;
}
public void init(String wo){

推荐阅读