上机实践六 组件及事件处理1

作者在 2011-12-21 16:51:56 发布以下内容
/*
*Teacher.java
*
*/
public class Teacher{
    int numberOne,numberTwo;
    String operator="";
    boolean right;
    public int giveNumberOne(int n){
        numberOne = (int)(Math.random()*n)+1;
        return numberOne;
    }    
    public int giveNumberTwo(int n){
        numberTwo = (int)(Math.random()*n)+1;
        return numberTwo;
    }
    public String giveOperator(){
        double d = Math.random();
        if(d>=0.5){
            operator="+";
        }else{
            operator="-";
        }
        return operator;
    }
    
    public boolean getRight(int answer){
        if(operator.equals("+")){
            if(answer==numberOne+numberTwo)
                right=true;
            else
                right=false;
        }else if(operator.equals("-")){
            if(answer==numberOne-numberTwo)
                right=true;
            else
                right=false;
        }
        return right;
    }
}
 /*
*ComputerFrame.java
**/
import java.awt.*;
import java.awt.event.*;
public class ComputerFrame extends Frame implements ActionListener{
    TextField textOne,textTwo,textResult;
    Button getProblem,giveAnwser;
    Label operatorLabel,message;
    
    Teacher teacher;
    ComputerFrame(String s){
        super(s);
        teacher = new Teacher();
        setLayout(new FlowLayout());
        textOne=new TextField("",10);
        textTwo=new TextField("",10);
        textResult=new TextField("",10);
        operatorLabel=new Label("+");
        message=new Label("你还没有回答问题呢");
        getProblem=new Button("获取题目");
        giveAnwser=new Button("确认答案");
        
        add(getProblem);
        add(textOne);
        add(operatorLabel);
        add(textTwo);
        add(new Label("="));
        add(textResult);
        add(giveAnwser);
        add(message);
        
        textResult.requestFocus();
        textOne.setEditable(false);
        textTwo.setEditable(false);
        
        //注册事件监听器
        getProblem.addActionListener(this);
        giveAnwser.addActionListener(this);
        textResult.addActionListener(this);
        
        setBounds(100,100,450,100);
        setVisible(true);
        validate();
        this.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==getProblem){//判断事件源是否是getProblem
            int number1=teacher.giveNumberOne(100);
            int number2=teacher.giveNumberTwo(100);
            String operator=teacher.giveOperator();
            textOne.setText(""+number1);
            textTwo.setText(""+number2);
            operatorLabel.setText(operator);
            message.setText("请回答");
            textResult.setText(null);
        }
        if(e.getSource()==giveAnwser){//判断事件源是否是giveAnwser
            String answer=textResult.getText();
            try{
                int result=Integer.parseInt(answer);
                if(teacher.getRight(result)==true){
                    message.setText("回答正确");
                }else{
                    message.setText("回答错误");
                }
            }catch(NumberFormatException ex){
                message.setText("请输入数字字符串");
            }    
        }
        textResult.requestFocus();
        validate();
    }
}
 /*
*MainClass.java
**/
public class MainClass{
    
    public static void main(String[] agrs){
        ComputerFrame frame;
        frame=new ComputerFrame ("算数测试");
    }
}
 
java codes | 阅读 1120 次
文章评论,共0条
游客请输入验证码
浏览18061次
最新评论