标题:一个猜数字的小游戏,要求添加计时器,下面是我的代码求大佬分析
只看楼主
苏黎世942
Rank: 1
来 自:山东
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-5-23
结帖率:0
已结贴  问题点数:20 回复次数:3 
一个猜数字的小游戏,要求添加计时器,下面是我的代码求大佬分析
package bianxie;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;


public class GuessNumber extends JFrame {                                   
    private JButton btn1;
    private JButton btn2;
    private JTextField jttime;         
    private JLabel jlfanwei;
    private JLabel jltime;
    private JLabel jlnum;
   
    private JTextField jtffanwei;   
    private JTextField jtfnum;
    private Timer tmr;
    int guessNumber=0;                           
    int max=100;                                 
    int min=1;
    int haomiao=1000;
   
GuessNumber() {
            super();
            jttime = new JTextField("0",14);
            jltime =new JLabel("时    间:");
            jlnum = new JLabel("猜一猜:");
            jtfnum = new JTextField(14);  
            jlfanwei=new JLabel("范     围:");
            jtffanwei = new JTextField("请猜一个范围1-100的数字",14);      
            btn1 = new JButton("开始");
            btn2 = new JButton("确定");
            setLayout(new FlowLayout());
            add(jltime);
            add(jttime);
            add(jlfanwei);
            add(jtffanwei);
            add(jlnum);
            add(jtfnum);
            add(btn1);
            add(btn2);
            setSize(230,170);        
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            setResizable(false);
            
            
            btn1.addActionListener(new ActionListener() {
               
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    if (e.getSource() == btn1) {
                        jttime.setText("0");
                         tmr.start();           
                     } else {
                         int t;
                         t = Integer.parseInt(jltime.getText());
                         t++;
                         jltime.setText("" + t);
                     }
                }
            });
            btn2.addActionListener(new ActionListener() {
               
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    int num = (int) (Math.random() * 100) + 1;
                    if (guessNumber > num) {
                             max = guessNumber;
                             jtffanwei.setText("你猜大了!");
                     } else if (guessNumber < num) {
                             min = guessNumber;                             
                             jtffanwei.setText("你猜小了!");
                     }
         }
               
            });
            
            
        }

        
    public static void main(String[] args) {
        GuessNumber c = new GuessNumber();   

    }

}
搜索更多相关主题的帖子: 计时器 import public 小游戏 package 
2017-06-21 18:23
苏黎世942
Rank: 1
来 自:山东
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-5-23
得分:0 
不知道错哪里T_T
2017-06-21 18:24
主骑士
Rank: 2
等 级:论坛游民
帖 子:2
专家分:20
注 册:2017-6-22
得分:20 
写注释好吗?不写注释谁看?
2017-06-22 17:02
我的梅梅
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-7-24
得分:0 
回复 楼主 苏黎世942
你的计时器只是定义了一下Timer tmr,就直接用了,明显错了,还有你那个生成随机数,放在按钮时间里面意味着你没按一次随机数生成一次,好扯蛋,应该把随机数放在全局变量
2017-07-24 17:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-478822-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.090443 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved