ZBLOG

package test; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextFiel...

package test; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.ButtonGroup; import javax.swing.JOptionPane; import javax.swing.JRadioButton;

public class Test1 extends JFrame implements ActionListener {

  1. private static final long serialVersionUID = 920921981358099067L;
  2. public int sumMatch;
  3. private JRadioButton jrJiandan,jrKunnan;
  4. private JLabel lbPer, lbCom, lbLast,btnNandu;
  5. private JButton btnStart, btnOne, btnTwo, btnThree;
  6. private JTextField tfPcshow, tfLastshow;
  7. private boolean win = true;
  8. public Test1() {
  9. super("21计科2班第六组拿火柴小游戏");
  10. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11. setSize(600, 300);
  12. setResizable(false);
  13. initUI();
  14. setLocationRelativeTo(null);
  15. }
  16. private void initUI() {
  17. Container container = getContentPane();
  18. ButtonGroup group = new ButtonGroup();
  19. btnNandu = new JLabel ("请选择难度:");
  20. btnNandu.setBounds(40, 10, 150, 30);
  21. btnNandu.setForeground(new Color(255, 140, 0));
  22. jrJiandan = new JRadioButton ("简单");
  23. jrJiandan.setBounds(200, 10, 150, 30);
  24. jrKunnan = new JRadioButton ("困难");
  25. jrKunnan.setBounds(300, 10, 150, 30);
  26. btnStart = new JButton("定义初始火柴数");
  27. btnStart.setBounds(410, 100, 150, 30);
  28. btnStart.setForeground(new Color(255, 140, 0));
  29. lbPer = new JLabel("人拿火柴数");
  30. lbPer.setBounds(40, 50, 120, 30);
  31. lbPer.setForeground(new Color(255, 140, 0));
  32. lbCom = new JLabel("计算机拿火柴数");
  33. lbCom.setBounds(40, 100, 120, 30);
  34. lbCom.setForeground(new Color(255, 140, 0));
  35. lbLast = new JLabel("剩余火柴数");
  36. lbLast.setBounds(40, 150, 120, 30);
  37. lbLast.setForeground(new Color(255, 140, 0));
  38. btnOne = new JButton("1");
  39. btnOne.setBounds(210, 50, 80, 40);
  40. btnOne.setForeground(new Color(255, 140, 0));
  41. btnTwo = new JButton("2");
  42. btnTwo.setBounds(310, 50, 80, 40);
  43. btnTwo.setForeground(new Color(255, 140, 0));
  44. btnThree = new JButton("3");
  45. btnThree.setBounds(410,50 ,80 ,40 );
  46. btnThree.setForeground(new Color (255 ,140 ,0));
  47. tfPcshow=new JTextField();
  48. tfPcshow.setBounds (210 ,100 ,150 ,30 );
  49. tfPcshow.setForeground (new Color (255 ,140 ,0));
  50. tfLastshow=new JTextField();
  51. tfLastshow.setBounds (210 ,150 ,150 ,30 );
  52. tfLastshow.setForeground (new Color (255 ,140 ,0));
  53. btnStart.addActionListener(this);
  54. btnOne.addActionListener(this);
  55. btnTwo.addActionListener(this);
  56. btnThree.addActionListener(this);
  57. group.add(btnOne);
  58. group.add(btnTwo);
  59. group.add(btnThree);
  60. container.setLayout(null);
  61. container.add(btnNandu);
  62. container.add(jrJiandan);
  63. container.add(jrKunnan);
  64. container.add(lbPer);
  65. container.add(lbCom);
  66. container.add(lbLast);
  67. container.add(btnStart);
  68. container.add(btnOne);
  69. container.add(btnTwo);
  70. container.add(btnThree);
  71. container.add(tfPcshow);
  72. container.add(tfLastshow);
  73. }
  74. public void actionPerformed(ActionEvent e) {
  75. if(e.getSource()==btnStart){
  76. sumMatch=(int)(Math.random()*(50-20))+20;
  77. tfPcshow.setText("初始火柴数为"+sumMatch+"根");
  78. tfLastshow.setText("");
  79. }
  80. else if(e.getSource() instanceof JButton){
  81. int k=Integer.parseInt(((JButton)e.getSource()).getText());
  82. sumMatch-=k;
  83. if (sumMatch == 0) {
  84. JOptionPane.showMessageDialog(this, "恭喜你赢了!", "", JOptionPane.INFORMATION_MESSAGE);
  85. } else if (sumMatch < 0) {
  86. JOptionPane.showMessageDialog(this, "请点击定义初始火柴数按钮", "错误", JOptionPane.INFORMATION_MESSAGE);
  87. }
  88. if(jrJiandan.isSelected()){
  89. tfPcshow.setText("计算机拿了"+computer1()+"根");
  90. }else{
  91. tfPcshow.setText("计算机拿了"+computer2()+"根");
  92. }
  93. tfLastshow.setText("火柴剩余"+sumMatch+"根");
  94. if (!win) {
  95. JOptionPane.showMessageDialog(this, "你输了,再试一次吧", "", JOptionPane.INFORMATION_MESSAGE);
  96. win = true;
  97. }
  98. }
  99. }
  100. private int computer1() {
  101. int ret = 0;
  102. if (sumMatch <= 3 && sumMatch > 0) {
  103. ret = sumMatch;
  104. sumMatch = 0;
  105. win = false;
  106. } else if (sumMatch != 0) {
  107. ret = (int) (Math.random() * (3 - 1)) + 1;
  108. sumMatch -= ret;
  109. }
  110. return ret;
  111. }
  112. private int computer2() {
  113. int ret = 0;
  114. if(sumMatch%4!=0){
  115. ret=sumMatch%4;
  116. sumMatch-=ret;
  117. }else{
  118. ret=(int)(Math.random()*3)+1;
  119. sumMatch-=ret;
  120. }
  121. if(sumMatch==0){
  122. win=false;
  123. }
  124. return ret;
  125. }
  126. public static void main(String[] args) {
  127. Test1 frame=new Test1();
  128. frame.setVisible(true);
  129. }

}

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=4674

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?