求大神帮我写下java火柴人小游戏代码注释

qq8731728a 2016-01-10 05:09:44
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class GetMatch extends JFrame implements ActionListener
{
private static final long serialVersionUID = 920921981358099067L;

public int sumMatch;

private JLabel lbPer, lbCom, lbLast;

private JButton btnStart;

private JRadioButton rdbOne, rdbTwo, rdbThree;

private JTextField tfPcshow, tfLastshow;

private boolean win = true;

public GetMatch()
{
super("火柴游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setLocationRelativeTo(null);
setResizable(false);
initUI();
}

private void initUI()
{
Container contrainer = getContentPane();
ButtonGroup group = new ButtonGroup();
contrainer.setLayout(null);

lbPer = new JLabel("Person取数");
lbPer.setBounds(20, 25, 100, 25);
lbCom = new JLabel("Computer取数");
lbCom.setBounds(20, 70, 100, 25);
lbLast = new JLabel("剩余火柴数");
lbLast.setBounds(20, 115, 100, 25);
btnStart = new JButton("PC启动");
btnStart.setBounds(310, 70, 100, 25);
rdbOne = new JRadioButton("1");
rdbOne.setBounds(135, 25, 35, 35);
rdbTwo = new JRadioButton("2");
rdbTwo.setBounds(170, 25, 35, 35);
rdbThree = new JRadioButton("3");
rdbThree.setBounds(205, 25, 35, 35);
tfPcshow = new JTextField();
tfPcshow.setBounds(135, 70, 150, 25);
tfLastshow = new JTextField();
tfLastshow.setBounds(135, 115, 150, 25);

btnStart.addActionListener(this);
rdbOne.addActionListener(this);
rdbTwo.addActionListener(this);
rdbThree.addActionListener(this);

group.add(rdbOne);
group.add(rdbTwo);
group.add(rdbThree);

contrainer.add(btnStart);
contrainer.add(rdbOne);
contrainer.add(rdbTwo);
contrainer.add(rdbThree);
contrainer.add(rdbOne);
contrainer.add(rdbTwo);
contrainer.add(rdbThree);
contrainer.add(lbPer);
contrainer.add(lbCom);
contrainer.add(tfPcshow);
contrainer.add(btnStart);
contrainer.add(lbLast);
contrainer.add(tfLastshow);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btnStart)
{
sumMatch = (int)(Math.random() * (50 - 20)) + 20;
tfPcshow.setText("计算机随机取了" + sumMatch + "根火柴");
tfLastshow.setText("");
}
else if(e.getSource() instanceof JRadioButton)
{
int k = Integer.parseInt(((JRadioButton)e.getSource()).getText());
;
sumMatch -= k;

if(sumMatch == 0)
{
JOptionPane.showMessageDialog(this, "你赢啦", "TEST", JOptionPane.INFORMATION_MESSAGE);
}
else if(sumMatch < 0)
{
JOptionPane.showMessageDialog(this, "出错啦!重新来过", "TEST", JOptionPane.INFORMATION_MESSAGE);
}

tfPcshow.setText("计算机取了" + computer() + "根");
tfLastshow.setText("剩余火柴" + sumMatch + "根");

if(!win)
{
JOptionPane.showMessageDialog(this, "你输啦!", "TEST", JOptionPane.INFORMATION_MESSAGE);
win = true;
}
}
}

private int computer()
{
int ret = 0;

if(sumMatch <= 3 && sumMatch > 0)
{
ret = sumMatch;
sumMatch = 0;
win = false;
}
else if(sumMatch != 0)
{
ret = (int)(Math.random() * (3 - 1)) + 1;
sumMatch -= ret;
}

return ret;
}

public static void main(String[] args)
{
GetMatch frame = new GetMatch();
frame.setVisible(true);
}
}
...全文
656 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧