第五次作业

代码:

import java.awt.*;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.util.Random;  
import javax.swing.*;  
class MyPanel extends Panel implements ActionListener{      
    JButton button;  
    JTextField text1, text2, text3;  
    JLabel label1, label2;  
    public int resultNum;  
      
    MyPanel()  
    {  
        button = new JButton("获取题目");  
        button.addActionListener(this);  
        text1 = new JTextField(5);  
        text1.setEditable(false);  
        text2 = new JTextField(5);  
        text2.setEditable(false);  
        text3 = new JTextField(5);  
        label1 = new JLabel("+");  
        label2 = new JLabel("=");  
      
        this.add(button);  
        this.add(text1);  
        this.add(label1);  
        this.add(text2);  
        this.add(label2);  
        this.add(text3);  
    }  
 
    public void actionPerformed(ActionEvent e) {  
        Random random = new Random();  
        int num1 = random.nextInt(100);  
        int num2 = random.nextInt(100);  
        resultNum = 0;  
        int sizeof = random.nextInt(4);  
        String str = "";  
         
        switch(sizeof)  
        {  
        case 0:  
            str = "+";  
            resultNum  = num1 + num2;  
            break;  
       case 1:  
            str = "-";  
            resultNum = num1 - num2;  
            break;  
        case 2:  
            str = "*";  
            resultNum = num1 * num2;  
            break;  
        case 3:  
            str = "/";  
            resultNum = num1 / num2;  
            break;  
        }  
        label1.setText(str);  
        text1.setText(Integer.toString(num1));  
        text2.setText(Integer.toString(num2));  
    }  
}  
 public class ComputerFrame extends JFrame implements ActionListener{  
     MyPanel panel;  
     JButton button1;  
     JLabel label3;  
      
     ComputerFrame()  
     {  
         this.setTitle("随机计算器");  
         setLayout(new FlowLayout());  
         panel = new MyPanel();  
         button1 = new JButton("确认答案");  
         button1.addActionListener(this);  
         label3 = new JLabel();  
         this.add(panel);  
         this.add(button1);  
         this.add(label3);     
         setVisible(true);  
         setExtendedState(Frame.MAXIMIZED_VERT);  
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
         setBounds(450, 300, 380, 125);  
         validate();  
    }  
   
     public void actionPerformed(ActionEvent e) {  
  
         if (panel.text3.getText().length() == 0) {  
             label3.setText("你还没有回答呢");  
         }  
     else   
         {  
             int result = Integer.parseInt(panel.text3.getText());  
               
             if (result != panel.resultNum)   
             {  
                label3.setText("你回答错误");  
             }   
             else  
             {  
                 label3.setText("你回答正确");  
             }  
         }  
     }  
 }

 }

封装的心得:

可以提高代码重用,使代码简洁!应该还可以提高安全性。

 

你可能感兴趣的:(作业)