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

qq8731728a qq8731728a 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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);
}
}
...全文
评论
收藏 已收藏
分享
返回顶部
qq8731728a
Java 51,407 成员 86,128 内容