三人行者,必有我?guī)熝伞?/a>

          為了理想,我愿付出全部的力量

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          用隨機(jī)數(shù)寫(xiě)的猜數(shù)字游戲(鍵盤(pán)可以控制)


            1 
            2 import java.awt.*;
            3 import javax.swing.*;
            4 import java.awt.event.*;
            5 import java.applet.Applet;
            6 import java.awt.Container;
            7 import java.util.*;
            8 
            9 //int a =(int)(Math.random()*(15-5))+5; 
           10 
           11 public class homework  extends Canvas 
           12 {
           13 
           14     private static JFrame window = new JFrame("homework");
           15     private static TextField tf = new TextField(20);
           16     static int a = (int)(Math.random()*(100-0))+0;
           17     private static JLabel jlabel = new JLabel("猜的消息");
           18     public static void main(String[] args) 
           19     {
           20     try
           21     {
           22         Button bn1 = new Button("猜一猜");
           23         Button bn2 = new Button("重新來(lái)一局");
           24         Container container = window.getContentPane();
           25         Toolkit thekit = window.getToolkit();
           26         bn2.addActionListener(new ActionListener()
           27         {
           28             public void actionPerformed(ActionEvent e)
           29             {
           30                 reseta();
           31                 jlabel.setText("猜的消息");
           32             }
           33         }
           34         );
           35         bn1.addActionListener(new ActionListener()
           36         {
           37             public void actionPerformed(ActionEvent e)
           38             {
           39             
           40                 int b = Integer.parseInt(tf.getText());
           41                 if(a > b)
           42                 {
           43                     jlabel.setText("你猜的數(shù)小了");
           44                 }
           45                 else if(a < b )
           46                 {
           47                     jlabel.setText("你猜的數(shù)大了");
           48                 }
           49                 else
           50                     {
           51                     jlabel.setText("恭喜!!你猜對(duì)了,請(qǐng)重新來(lái)");        
           52                     reseta();
           53                     }
           54             }
           55         
           56         }
           57         );        
           58         tf.addKeyListener(new KeyAdapter()
           59         {
           60              public void keyReleased(KeyEvent e)    //按鍵被按下的時(shí)候
           61             {
           62                  if(e.getKeyCode()==KeyEvent.VK_ENTER)
           63                 {
           64                     int b = Integer.parseInt(tf.getText());
           65                     if(a > b)
           66                     {
           67                         jlabel.setText("你猜的數(shù)小了");
           68                     }
           69                     else if(a < b )
           70                     {
           71                         jlabel.setText("你猜的數(shù)大了");
           72                     }
           73                     else
           74                         {
           75                         jlabel.setText("恭喜!!你猜對(duì)了,請(qǐng)重新來(lái)");        
           76                         reseta();
           77                         }
           78                 }                
           79             }
           80             
           81         }
           82         );            
           83         Dimension wndsize = thekit.getScreenSize();
           84         window.setBounds(wndsize.width/4,wndsize.height/4,wndsize.width/2,wndsize.height/2);
           85         container.setLayout(new FlowLayout());
           86         container.add(tf);
           87         container.add(bn1);
           88         container.add(bn2);
           89         container.add(jlabel);
           90         window.setVisible(true);        
           91     }
           92     catch(Exception e)
           93     {        
           94     }
           95 }
           96     public static void reseta()
           97     {
           98         a = (int)(Math.random()*(100-0))+0;
           99     }
          100 

          posted on 2008-10-26 03:05 wsy6634 閱讀(1004) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 普安县| 屯昌县| 织金县| 贺兰县| 会理县| 连南| 康保县| 南阳市| 麦盖提县| 时尚| 涞源县| 冀州市| 波密县| 龙州县| 吉首市| 饶河县| 南汇区| 平利县| 合江县| 宝山区| 阜新| 剑河县| 乐安县| 甘肃省| 邹城市| 莎车县| 卢氏县| 南木林县| 平湖市| 浪卡子县| 修水县| 彭州市| 米林县| 晋州市| 宁德市| 盐源县| 武隆县| 宜宾市| 江油市| 丹巴县| 登封市|