JAVA课程设计

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013097262/article/details/42191613

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JOptionPane;

public class Mm extends Frame implements ActionListener,KeyListener,WindowListener{
 
 private static final long serialVersionUID = 1L;
 Button bt1=new Button("确定");
 Button bt2=new Button("退出");
 Label l1=new Label("大了!");
 Label l2=new Label("小了!");
 Label l3=new Label("正确!");
 TextField tx=new TextField(10);
 int a=(int)(Math.random()*100);
 int b;
 public Mm(){
  super("猜数字");
  this.setSize(200, 150);
  this.setLocation(300, 400);
  this.setLayout(new FlowLayout());
  this.setBackground(Color.orange);
  this.add(new Label("数字 :"));
  this.add(tx);
  this.add(bt1);
  this.add(bt2);
  bt1.addActionListener(this);
  bt2.addActionListener(this);
  tx.addKeyListener(this);
  this.addWindowListener(this);
  this.setVisible(true);
 }
 
 public static void main(String[] args) {
  new Mm();
 }

 public void actionPerformed(ActionEvent arg0) {
  if(arg0.getSource()==bt1){
   String aa=new String();
   aa=tx.getText();
   while(aa.length()==0){
    aa=tx.getText();
   }
   b=Integer.parseInt(aa);
   if(a==b){
    try{
     JOptionPane.showMessageDialog(null, l3);
    }catch (Exception e) {
     ;
    }
    a=(int)(Math.random()*100);
   }
   else if(a>b){
     try{
      JOptionPane.showMessageDialog(null, l2);
     }catch (Exception e) {
      ;
     }
     
    }
    else {

     try{
      JOptionPane.showMessageDialog(null, l1);
     }catch (Exception e) {
      ;
     }
    }
    tx.setText(null);
  }else if(arg0.getSource()==bt2){
   System.exit(0);
  }
  
 }

 public void keyPressed(KeyEvent e) {
  if(e.getKeyCode()==10){

   String aa=new String();
   aa=tx.getText();
   while(aa.length()==0){
    aa=tx.getText();
   }
   b=Integer.parseInt(aa);
   if(a==b){
    try{
     JOptionPane.showMessageDialog(null, l3);
    }catch (Exception aaa) {
     ;
    }
    a=(int)(Math.random()*100);
   }
   else if(a>b){
     try{
      JOptionPane.showMessageDialog(null, l2);
     }catch (Exception aaa) {
      ;
     }
     
    }
    else {

     try{
      JOptionPane.showMessageDialog(null, l1);
     }catch (Exception aaa) {
      ;
     }
    }
    tx.setText(null);
  }
  }

 public void keyReleased(KeyEvent e) {
  // TODO Auto-generated method stub
  
 }

 public void keyTyped(KeyEvent e) {
  // TODO Auto-generated method stub
  
 }

 public void windowActivated(WindowEvent e) {
  // TODO Auto-generated method stub
  
 }

 public void windowClosed(WindowEvent e) {
  
 }

 public void windowClosing(WindowEvent e) {
  System.exit(0);
  
 }

 public void windowDeactivated(WindowEvent e) {
  // TODO Auto-generated method stub
  
 }

 public void windowDeiconified(WindowEvent e) {
  // TODO Auto-generated method stub
  
 }

 public void windowIconified(WindowEvent e) {
  // TODO Auto-generated method stub
  
 }

 public void windowOpened(WindowEvent e) {
  // TODO Auto-generated method stub
  
 }
}

展开阅读全文

没有更多推荐了,返回首页