当前位置: 代码迷 >> Java相关 >> 写了个程序,可是不是我想要的。。
  详细解决方案

写了个程序,可是不是我想要的。。

热度:272   发布时间:2012-12-15 15:16:08.0
写了个程序,可是不是我想要的。。
package day1215;


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class csz{
        public static void main(String args[]){
            TimeWin Win=new TimeWin();
        }
    }
    class TimeWin extends JFrame implements ActionListener{
        JButton bStart,bStop,bContinue;
        JLabel showNumber;
        Timer time;
        int number=0,start=1;
        char [] Number;
        TimeWin(){
            time=new Timer(1000,this);
            showNumber=new JLabel();
            showNumber.setHorizontalAlignment(JLabel.CENTER);
            showNumber.setFont(new Font("数字",Font.BOLD,58));
            bStart=new JButton("开始");
    bStop=new JButton("暂停");
    bContinue=new JButton("继续");
            bStart.addActionListener(this);
            bStop.addActionListener(this);
            bContinue.addActionListener(this);
            JPanel pNorth=new JPanel();
            pNorth.add(bStart);
    pNorth.add(bStop);
    pNorth.add(bContinue);
            add(pNorth,BorderLayout.NORTH);
            add(showNumber,BorderLayout.CENTER);
            setSize(300,180);
    setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Number=new char[100];
            for(int k=0,i='0';k<Number.length;i++,k++)
                Number[k]=(char)i;
        }
        public void actionPerformed(ActionEvent e){
          if(e.getSource()==time){
              showNumber.setText(""+Number[number]);
            number++;
            if(number==Number.length-1)
              number=0;
          }
          else if(e.getSource()==bStart)
            time.start();   
          else if(e.getSource()==bStop)
            time.stop();   
          else if(e.getSource()==bContinue)
            time.restart();   
        }
    }



猜数字游戏
要求:用图形用户界面实现。
功能:计算机产生随机数,猜中即胜,猜不中,提示是大了还是小了,继续猜,直至猜
到,给出所用时间和评语。



这个写出来为什么不是随机数啊。。。
搜索更多相关的解决方案: number  public  package  void  

----------------解决方案--------------------------------------------------------
程序代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class GuessNumber extends JFrame {
    private JTextField tfNumber;
    private JButton btnGenerate;
    private JButton btnGuess;
    private JLabel lbTip;
    private int number;
    private Timer timer;
    private int usedTime;
   
    private Action generate = new AbstractAction("生成一个随机数") {
        public void actionPerformed(ActionEvent e) {
            number = new Random().nextInt(1000); // 0 ~ 999之间的随机数
            lbTip.setText("请猜一猜这个数是多少?");
            generate.setEnabled(false);
            guess.setEnabled(true);
            timer.start();
        }
    };
   
    private Action guess = new AbstractAction("猜一猜") {
        public void actionPerformed(ActionEvent e) {
            String textNumber = tfNumber.getText().trim();
            if (textNumber.isEmpty())
                lbTip.setText("您还没有输入数字!");
            else {
                try {
                    int guessNumber = Integer.parseInt(textNumber);
                    if (guessNumber == number) {
                        lbTip.setText("恭喜您猜中啦!用时:" + (usedTime / 100.0) + "秒!");
                        timer.stop();
                        usedTime = 0;
                        generate.setEnabled(true);
                        guess.setEnabled(false);
                    } else if (guessNumber > number) {
                        lbTip.setText("您猜的数字有点大!");
                    } else {
                        lbTip.setText("您猜的数字有点小!");
                    }
                } catch (NumberFormatException ex) {
                    lbTip.setText("请输入数字!");
                }
            }
        }
    };
   
    private ActionListener timerListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            usedTime += 1; // "增加0.01秒"
        }
    };
   
    public GuessNumber() {
        tfNumber = new JTextField(20);
        btnGenerate = new JButton(generate);
        btnGuess = new JButton(guess);
        lbTip = new JLabel("");
        guess.setEnabled(false);
      
        timer = new Timer(10, timerListener);
      
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        topPanel.add(btnGenerate);
        topPanel.add(lbTip);
        bottomPanel.add(tfNumber);
        bottomPanel.add(btnGuess);
        add(topPanel, BorderLayout.NORTH);
        add(bottomPanel, BorderLayout.CENTER);
      
        setTitle("猜数字游戏");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 200);
        setLocation(300, 300);
    }
   
    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GuessNumber().setVisible(true);
            }
        });
    }
}

按照楼主要求写了一个,可以参考下。
----------------解决方案--------------------------------------------------------
谢谢谢谢啦,怎么给你分啊
----------------解决方案--------------------------------------------------------
回复 楼主 heawse
我想问下主函数那一块儿是什么意思,throws Exception,,,是啥啊.。。。还有啊,,为什么没有找到程序的名字,,,没名字怎么运行啊,,,,看到一定要跟我说说,,谢谢了,,,帮人帮到底啊
----------------解决方案--------------------------------------------------------
程序名倒是找到了。。。可是throws Exception还是不懂哎
----------------解决方案--------------------------------------------------------
throws Exception 是抛出的异常

----------------解决方案--------------------------------------------------------
程序的名字可买 自己改吧,这个都 不是很重要吧,我觉得。
----------------解决方案--------------------------------------------------------
  相关解决方案