• 5.1-测试与封装


    1.开发环境---Eclipse,Eclipse-Junit4

    2.人员---吕日荣(201306114315)、张梓锋(201306114318)

    第一阶段---把计算核心算法封装成类

    package sizeyunsuan;
    
    public class SiZeYunSuan {
        int ranswer=0;
    public int test1(char op,int a,int b){                                                                                                   
            
            switch(op) 
            {  
            case '+':   ranswer=a+b;break;
            case '-':   ranswer=a-b;break;
            case '*':   ranswer=a*b;break; 
            case '/':   ranswer=a/b;break;  
            } 
            return ranswer;
    }
      
    public int test2(int c){     
            int jc=1;
            for(int i=c;i>0;i--)
            {
                jc=jc*i;
            }
            ranswer=jc;
            return ranswer;
    }
    }

    第二阶段---对这个测试类进行单元测试

    package sizeyunsuan;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    public class SiZeYunSuanTest {
    
        @Test
        public void testTest1() {
            SiZeYunSuan core=new SiZeYunSuan();
            int a=core.test1('-',2,8);
            assertEquals(-6,a);
        }
        @Test
        public void testTest2() {
            SiZeYunSuan core=new SiZeYunSuan();
            int a=core.test1('+',3,6);
            assertEquals(9,a);
        }
        @Test
        public void testTest3() {
            SiZeYunSuan core=new SiZeYunSuan();
            int a=core.test1('*',2,9);
            assertEquals(18,a);
        }
        @Test
        public void testTest4() {
            SiZeYunSuan core=new SiZeYunSuan();
            int a=core.test1('/',6,3);
            assertEquals(2,a);
        }
    
        @Test
        public void testTest5() {
            SiZeYunSuan core=new SiZeYunSuan();
            int b=core.test2(3);
            assertEquals(6,b);
        }
        @Test
        public void testTest6() {
            SiZeYunSuan core=new SiZeYunSuan();
            int b=core.test2(4);
            assertEquals(24,b);
        }  
        }

     

                                                                                                  第三阶段---白盒测试

    编号 输入数据 预期输出数据(a或b的值) 实际输出数据(a或b的值) 正确与否
    1 (‘-’,2,8)

    -6

    -6 正确
    2 (‘+’,3,6) 9 9 正确
    3 (‘*’,2,9) 18 18 正确
    4 (‘/’,6,3) 2 2 正确
    5 (3) 6 6 正确
    6 (4) 24 24 正确
    7 (5) 120 121 错误

    第四阶段---黑盒测试

    原代码:

    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.LayoutStyle.ComponentPlacement;
    import java.awt.Font;
    import javax.swing.JTextPane;
    import javax.swing.JButton;
    import java.awt.Color;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import java.awt.event.*;
    import java.util.Random;
    
    public class SiZeYunSuan {
    
        private JFrame frame;
        private JTextField textField;
        private JTextField textField_1;
        private JTextField textField_2;
        private JTextField textField_3;
        int num=5,T=0,F=0,count=1;
        int ranswer=0;                 
        int uanswer=0;                 
        private JTextField textField_4;
        private JTextField textField_5;
        private JTextField textField_6;
        private JTextField textField_7;
        private boolean stop = false;
        String str="";
        String z="";
        JTextPane textPane = new JTextPane();
        JButton button = new JButton("u5F00u59CBu7B54u9898");
        long startTime;
        long endTime;
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        SiZeYunSuan window = new SiZeYunSuan();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         */
        public SiZeYunSuan() {
            initialize();    
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            frame = new JFrame();
            frame.setResizable(false);
            
            JPanel panel = new JPanel();
            GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
            groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                    .addGroup(groupLayout.createSequentialGroup()
                        .addComponent(panel, GroupLayout.PREFERRED_SIZE, 448, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, 487, Short.MAX_VALUE)
            );
            
            
            textPane.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 22));
            textPane.setEditable(false);
            textPane.setToolTipText("");
            textPane.setBackground(Color.WHITE);
            
            JPanel panel_1 = new JPanel();
            panel_1.setBackground(new Color(204, 204, 204));
            
            
            button.setBackground(Color.GREEN);
            
            JButton button_1 = new JButton("u9000u51FA");
            button_1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            button_1.setBackground(Color.RED);
            
            JLabel lblNewLabel = new JLabel(" u8F93u5165u9898u6570uFF1A");
            lblNewLabel.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
            lblNewLabel.setForeground(new Color(0, 0, 0));
            
            JLabel label = new JLabel(" u7B54u5BF9u9898u6570uFF1A");
            label.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
            
            JLabel label_1 = new JLabel(" u7B54u9519u9898u6570uFF1A");
            label_1.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
            
            JLabel label_2 = new JLabel(" u4FE1u606Fu53CDu9988uFF1A");
            label_2.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
            
            textField = new JTextField();
            textField.setColumns(10);
            
            textField_1 = new JTextField();
            textField_1.setForeground(Color.GREEN);
            textField_1.setEditable(false);
            textField_1.setColumns(10);
            
            textField_2 = new JTextField();
            textField_2.setForeground(Color.RED);
            textField_2.setEditable(false);
            textField_2.setColumns(10);
            
            textField_3 = new JTextField();
            textField_3.setFont(new Font("宋体", Font.PLAIN, 12));
            textField_3.setForeground(Color.RED);
            textField_3.setEditable(false);
            textField_3.setColumns(10);
            
            JLabel label_3 = new JLabel(" u6B63u786Eu7B54u6848uFF1A");
            label_3.setFont(new Font("Dialog", Font.PLAIN, 15));
            
            JLabel label_4 = new JLabel(" u8F93u5165u7B54u6848uFF1A");
            label_4.setFont(new Font("Dialog", Font.PLAIN, 15));
            
            JLabel label_5 = new JLabel(" u5F53u524Du9898u6570uFF1A");
            label_5.setFont(new Font("Dialog", Font.PLAIN, 15));
            
            textField_4 = new JTextField();
            textField_4.setEditable(false);
            textField_4.setColumns(10);
            
            textField_5 = new JTextField();
            textField_5.setColumns(10);
            
            textField_6 = new JTextField();
            textField_6.setFont(new Font("Yu Gothic", Font.BOLD, 14));
            textField_6.setForeground(Color.CYAN);
            textField_6.setEditable(false);
            textField_6.setColumns(10);
            
            JLabel label_6 = new JLabel(" u6240u7528u65F6u95F4uFF1A");
            label_6.setFont(new Font("Dialog", Font.PLAIN, 15));
            
            textField_7 = new JTextField();
            textField_7.setFont(new Font("宋体", Font.PLAIN, 14));
            textField_7.setEditable(false);
            textField_7.setColumns(10);
            GroupLayout gl_panel_1 = new GroupLayout(panel_1);
            gl_panel_1.setHorizontalGroup(
                gl_panel_1.createParallelGroup(Alignment.TRAILING)
                    .addGroup(gl_panel_1.createSequentialGroup()
                        .addGap(11)
                        .addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                        .addGap(110))
                    .addGroup(gl_panel_1.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                            .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                    .addGroup(gl_panel_1.createSequentialGroup()
                                        .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                            .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(label, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                                        .addPreferredGap(ComponentPlacement.RELATED))
                                    .addGroup(gl_panel_1.createSequentialGroup()
                                        .addComponent(label_3, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                                        .addPreferredGap(ComponentPlacement.RELATED)))
                                .addGroup(gl_panel_1.createSequentialGroup()
                                    .addComponent(label_1)
                                    .addPreferredGap(ComponentPlacement.RELATED)))
                            .addGroup(gl_panel_1.createSequentialGroup()
                                .addComponent(label_4)
                                .addPreferredGap(ComponentPlacement.RELATED)))
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                            .addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                            .addComponent(textField_1, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                            .addComponent(textField_4, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                            .addGroup(gl_panel_1.createSequentialGroup()
                                .addComponent(textField, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED, 13, Short.MAX_VALUE))
                            .addComponent(textField_5, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                            .addComponent(textField_6, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE))
                        .addGap(66))
                    .addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING, false)
                            .addComponent(label_6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(label_2, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                            .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
                            .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
                            .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(10, Short.MAX_VALUE))
                    .addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
                        .addGap(52)
                        .addComponent(button)
                        .addContainerGap(91, Short.MAX_VALUE))
            );
            gl_panel_1.setVerticalGroup(
                gl_panel_1.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel_1.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                            .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                            .addComponent(textField, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                        .addGap(18)
                        .addComponent(button, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
                        .addGap(18)
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                            .addGroup(gl_panel_1.createSequentialGroup()
                                .addGap(18)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                    .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(textField_4, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                    .addGroup(gl_panel_1.createSequentialGroup()
                                        .addGap(40)
                                        .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                            .addComponent(textField_6, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(textField_5, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_4, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                    .addComponent(label, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                    .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label_1)))
                            .addGroup(gl_panel_1.createSequentialGroup()
                                .addGap(206)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                    .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                            .addComponent(label_6, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                            .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                        .addGap(33)
                        .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                        .addGap(40))
            );
            panel_1.setLayout(gl_panel_1);
            
            button.addActionListener(new ActionListener(){//开始答题
                public void actionPerformed(ActionEvent e)
                {
                    count=1;
                    T=0;F=0;
                    str="";
                    z="";
                    stop=false;
                    textField_6.setText("");
                    textField_4.setText(""+count);
                    try{
                    num=Integer.parseInt(textField.getText());
                    if(num>5)
                    {
                        
                        textPane.setText("输入题数错误,请重新输入!(1~5)");
                        textField.setText("");
                    }
                    else
                    {
                        startTime=System.currentTimeMillis();
                        textField_1.setText(""+T);
                        textField_2.setText(""+F);
                        button.setVisible(false);
                        textField.setEditable(false);
                        showtext();
                    }
                    }
                    catch(NumberFormatException e1)
                    {
                        textPane.setText("输入题数错误,请重新输入!(1~5)");
                        textField.setText("");
                    }
                    
                    
                }            
            });
            textField_5.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {try{
                    if(!stop)
                    {
                    
                    
                    uanswer=Integer.parseInt(textField_5.getText());
                    if(num==count)
                    {
                        if(uanswer==ranswer) 
                        {  
                            textField_6.setText(""+ranswer);
                            textField_3.setText("答对!"); 
                            T++; 
                            textField_1.setText(""+T);    
                            count++;    
                        }  
                        else 
                        {  
                            textField_6.setText(""+ranswer);
                            textField_3.setText("答错!");
                            F++;
                            textField_2.setText(""+F);
                            count++;
                        }
                        stop=true;
                        textField_3.setText(" 答題結束!");
                        endTime=System.currentTimeMillis();
                        textField.setEditable(true);
                        button.setVisible(true);
                        textField_7.setText((endTime-startTime)/1000+"s");
                        
                    }
                    else if(uanswer==ranswer) 
                    {   
                        textField_6.setText(""+ranswer);
                        textField_3.setText("答对!"); 
                        T++; 
                        textField_1.setText(""+T);    
                        showtext();
                        count++;
                        
                    }  
                    else
                    {   
                        textField_6.setText(""+ranswer);
                        textField_3.setText("答错!");
                        F++;
                        textField_2.setText(""+F);
                        showtext();
                        count++;
                    }
                    }
                }
                    
                    catch(NumberFormatException e2)
                    {
                        textPane.setText(str+"答案格式错误,请重新输入答案!");
                        textField_5.setText("");
                    }
                
                }
            });
            GroupLayout gl_panel = new GroupLayout(panel);
            gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel.createSequentialGroup()
                        .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 243, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
                        .addGap(28))
            );
            gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel.createSequentialGroup()
                        .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                            .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 484, GroupLayout.PREFERRED_SIZE)
                            .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            panel.setLayout(gl_panel);
            frame.getContentPane().setLayout(groupLayout);
            frame.setBounds(100, 100, 454, 518);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }    
        void showtext()
        {
            char op='+';
            int a=0,b=0,c=0,e=0,f=0,opnum=0,chioce=0;
            int temp=0;
            Random ra =new Random();
            e=ra.nextInt(10)+1;
            f=ra.nextInt(10)+1;
            chioce=ra.nextInt(2)+1;
            if(e%2==0)
            {
            a=ra.nextInt(10)+2;
            }
            else
            {
                a=-ra.nextInt(10)+2;
            }
            if(f%2==0)
            {
            b=ra.nextInt(10)+2;
            }
            else
            {
                b=-ra.nextInt(10)+2;
            }
            c=ra.nextInt(5);
            opnum=Math.abs(ra.nextInt())%4+1;    
            textField_5.setText("");
            switch(chioce)
            {
            case 1:
            {
            switch(opnum)  
                {  
                case 1:    
                    op='+';  
                    break;
                case 2:  
                    op='-'; 
                    break;
                case 3:  
                    op='*';    
                    break;
                case 4:  
                    op='/';  
                    break; 
                }                                               
                                                                
                if((op=='-') && (a<b)) 
                {   
                    temp=a; 
                    a=b;
                    b=temp;  
                }
                                                              
                if(op=='/')  
                {   
                    if(b==0)
                    {    
                        b=1;  
                    }   
                    a=a*b; 
                }  
                if(a>0&&b>0)
                {
                    z=String.valueOf(a)+String.valueOf(op)+String.valueOf(b)+"="+"
    ";
                }
                else if(a<0&&b>0)
                {
                    z="("+String.valueOf(a)+")"+String.valueOf(op)+String.valueOf(b)+"="+"
    ";
                }
                else if(a>0&&b<0)
                {
                    z=String.valueOf(a)+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"
    ";
                }
                else
                {
                    z="("+String.valueOf(a)+")"+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"
    ";
                }
                str=str+z;                                                  
                textPane.setText(str); 
            
                switch(op) 
                {  
                case '+':   ranswer=a+b;   break;
                case '-':   ranswer=a-b;   break;
                case '*':   ranswer=a*b;   break;
                case '/':   ranswer=a/b;   break;  
                } 
                break;
            }
            case 2:
            {
                int jc=1;
                str=str+String.valueOf(c)+"!="+"
    ";                                                
                textPane.setText(str);
                for(int i=c;i>0;i--)
                {
                    jc=jc*i;
                }
                ranswer=jc;
                break;
            }
            }
        }
    }
    原代码

    通过封装后的源代码

    package Russ;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.LayoutStyle.ComponentPlacement;
    import java.awt.Font;
    import javax.swing.JTextPane;
    import javax.swing.JButton;
    import java.awt.Color;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    
    import sizeyunsuan.SiZeYunSuan;
    
    import java.awt.event.*;
    import java.util.Random;
    
    public class SiZeYunSuan1{
    
        private JFrame frame;
        private JTextField textField;
        private JTextField textField_1;
        private JTextField textField_2;
        private JTextField textField_3;
        int num=5,T=0,F=0,count=1;
        int ranswer=0;                 
        int uanswer=0;                 
        private JTextField textField_4;
        private JTextField textField_5;
        private JTextField textField_6;
        private JTextField textField_7;
        private boolean stop = false;
        String str="";
        String z="";
        JTextPane textPane = new JTextPane();
        JButton button = new JButton("u5F00u59CBu7B54u9898");
        long startTime;
        long endTime;
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        SiZeYunSuan1 window = new SiZeYunSuan1();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         */
        public SiZeYunSuan1() {
            initialize();    
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            frame = new JFrame();
            frame.setResizable(false);
            
            JPanel panel = new JPanel();
            GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
            groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                    .addGroup(groupLayout.createSequentialGroup()
                        .addComponent(panel, GroupLayout.PREFERRED_SIZE, 448, 
    
    GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, 487, 
    
    Short.MAX_VALUE)
            );
            
            
            textPane.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 22));
            textPane.setEditable(false);
            textPane.setToolTipText("");
            textPane.setBackground(Color.WHITE);
            
            JPanel panel_1 = new JPanel();
            panel_1.setBackground(new Color(204, 204, 204));
            
            
            button.setBackground(Color.GREEN);
            
            JButton button_1 = new JButton("u9000u51FA");
            button_1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            button_1.setBackground(Color.RED);
            
            JLabel lblNewLabel = new JLabel(" u8F93u5165u9898u6570uFF1A");
            lblNewLabel.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
            lblNewLabel.setForeground(new Color(0, 0, 0));
            
            JLabel label = new JLabel(" u7B54u5BF9u9898u6570uFF1A");
            label.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
            
            JLabel label_1 = new JLabel(" u7B54u9519u9898u6570uFF1A");
            label_1.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
            
            JLabel label_2 = new JLabel(" u4FE1u606Fu53CDu9988uFF1A");
            label_2.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
            
            textField = new JTextField();
            textField.setColumns(10);
            
            textField_1 = new JTextField();
            textField_1.setForeground(Color.GREEN);
            textField_1.setEditable(false);
            textField_1.setColumns(10);
            
            textField_2 = new JTextField();
            textField_2.setForeground(Color.RED);
            textField_2.setEditable(false);
            textField_2.setColumns(10);
            
            textField_3 = new JTextField();
            textField_3.setFont(new Font("宋体", Font.PLAIN, 12));
            textField_3.setForeground(Color.RED);
            textField_3.setEditable(false);
            textField_3.setColumns(10);
            
            JLabel label_3 = new JLabel(" u6B63u786Eu7B54u6848uFF1A");
            label_3.setFont(new Font("Dialog", Font.PLAIN, 15));
            
            JLabel label_4 = new JLabel(" u8F93u5165u7B54u6848uFF1A");
            label_4.setFont(new Font("Dialog", Font.PLAIN, 15));
            
            JLabel label_5 = new JLabel(" u5F53u524Du9898u6570uFF1A");
            label_5.setFont(new Font("Dialog", Font.PLAIN, 15));
            
            textField_4 = new JTextField();
            textField_4.setEditable(false);
            textField_4.setColumns(10);
            
            textField_5 = new JTextField();
            textField_5.setColumns(10);
            
            textField_6 = new JTextField();
            textField_6.setFont(new Font("Yu Gothic", Font.BOLD, 14));
            textField_6.setForeground(Color.CYAN);
            textField_6.setEditable(false);
            textField_6.setColumns(10);
            
            JLabel label_6 = new JLabel(" u6240u7528u65F6u95F4uFF1A");
            label_6.setFont(new Font("Dialog", Font.PLAIN, 15));
            
            textField_7 = new JTextField();
            textField_7.setFont(new Font("宋体", Font.PLAIN, 14));
            textField_7.setEditable(false);
            textField_7.setColumns(10);
            GroupLayout gl_panel_1 = new GroupLayout(panel_1);
            gl_panel_1.setHorizontalGroup(
                gl_panel_1.createParallelGroup(Alignment.TRAILING)
                    .addGroup(gl_panel_1.createSequentialGroup()
                        .addGap(11)
                        .addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 103, 
    
    Short.MAX_VALUE)
                        .addGap(110))
                    .addGroup(gl_panel_1.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                            .addGroup(gl_panel_1.createParallelGroup
    
    (Alignment.LEADING)
                                .addGroup(gl_panel_1.createParallelGroup
    
    (Alignment.LEADING)
                                    .addGroup(gl_panel_1.createSequentialGroup()
                                        .addGroup(gl_panel_1.createParallelGroup
    
    (Alignment.LEADING)
                                            .addComponent(label_5, 
    
    GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(label, 
    
    GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                                        .addPreferredGap
    
    (ComponentPlacement.RELATED))
                                    .addGroup(gl_panel_1.createSequentialGroup()
                                        .addComponent(label_3, 
    
    GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                                        .addPreferredGap
    
    (ComponentPlacement.RELATED)))
                                .addGroup(gl_panel_1.createSequentialGroup()
                                    .addComponent(label_1)
                                    .addPreferredGap(ComponentPlacement.RELATED)))
                            .addGroup(gl_panel_1.createSequentialGroup()
                                .addComponent(label_4)
                                .addPreferredGap(ComponentPlacement.RELATED)))
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                            .addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 54, 
    
    Short.MAX_VALUE)
                            .addComponent(textField_1, GroupLayout.DEFAULT_SIZE, 54, 
    
    Short.MAX_VALUE)
                            .addComponent(textField_4, GroupLayout.DEFAULT_SIZE, 54, 
    
    Short.MAX_VALUE)
                            .addGroup(gl_panel_1.createSequentialGroup()
                                .addComponent(textField, GroupLayout.PREFERRED_SIZE, 
    
    41, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED, 13, 
    
    Short.MAX_VALUE))
                            .addComponent(textField_5, GroupLayout.DEFAULT_SIZE, 54, 
    
    Short.MAX_VALUE)
                            .addComponent(textField_6, GroupLayout.DEFAULT_SIZE, 54, 
    
    Short.MAX_VALUE))
                        .addGap(66))
                    .addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING, 
    
    false)
                            .addComponent(label_6, GroupLayout.DEFAULT_SIZE, 
    
    GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(label_2, GroupLayout.DEFAULT_SIZE, 90, 
    
    Short.MAX_VALUE))
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                            .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 
    
    54, GroupLayout.PREFERRED_SIZE)
                            .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 82, 
    
    GroupLayout.PREFERRED_SIZE)
                            .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 
    
    75, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(10, Short.MAX_VALUE))
                    .addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
                        .addGap(52)
                        .addComponent(button)
                        .addContainerGap(91, Short.MAX_VALUE))
            );
            gl_panel_1.setVerticalGroup(
                gl_panel_1.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel_1.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                            .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 
    
    30, GroupLayout.PREFERRED_SIZE)
                            .addComponent(textField, GroupLayout.PREFERRED_SIZE, 30, 
    
    GroupLayout.PREFERRED_SIZE))
                        .addGap(18)
                        .addComponent(button, GroupLayout.PREFERRED_SIZE, 42, 
    
    GroupLayout.PREFERRED_SIZE)
                        .addGap(18)
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                            .addGroup(gl_panel_1.createSequentialGroup()
                                .addGap(18)
                                .addGroup(gl_panel_1.createParallelGroup
    
    (Alignment.BASELINE)
                                    .addComponent(label_5, 
    
    GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(textField_4, 
    
    GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addGroup(gl_panel_1.createParallelGroup
    
    (Alignment.LEADING)
                                    .addGroup(gl_panel_1.createSequentialGroup()
                                        .addGap(40)
                                        .addGroup(gl_panel_1.createParallelGroup
    
    (Alignment.BASELINE)
                                            .addComponent(textField_6, 
    
    GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(label_3, 
    
    GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(gl_panel_1.createParallelGroup
    
    (Alignment.BASELINE)
                                        .addComponent(textField_5, 
    
    GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_4, 
    
    GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addGroup(gl_panel_1.createParallelGroup
    
    (Alignment.LEADING)
                                    .addComponent(label, GroupLayout.PREFERRED_SIZE, 
    
    28, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(textField_1, 
    
    GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addGroup(gl_panel_1.createParallelGroup
    
    (Alignment.BASELINE)
                                    .addComponent(textField_2, 
    
    GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label_1)))
                            .addGroup(gl_panel_1.createSequentialGroup()
                                .addGap(206)
                                .addGroup(gl_panel_1.createParallelGroup
    
    (Alignment.BASELINE)
                                    .addComponent(label_2, 
    
    GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(textField_3, 
    
    GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                            .addComponent(label_6, GroupLayout.PREFERRED_SIZE, 26, 
    
    GroupLayout.PREFERRED_SIZE)
                            .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 
    
    30, GroupLayout.PREFERRED_SIZE))
                        .addGap(33)
                        .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 41, 
    
    GroupLayout.PREFERRED_SIZE)
                        .addGap(40))
            );
            panel_1.setLayout(gl_panel_1);
            
            button.addActionListener(new ActionListener(){//开始答题
                public void actionPerformed(ActionEvent e)
                {
                    count=1;
                    T=0;F=0;
                    str="";
                    z="";
                    stop=false;
                    textField_6.setText("");
                    textField_4.setText(""+count);
                    try{
                    num=Integer.parseInt(textField.getText());
                    if(num>5)
                    {
                        
                        textPane.setText("输入题数错误,请重新输入!(1~5)");
                        textField.setText("");
                    }
                    else
                    {
                        startTime=System.currentTimeMillis();
                        textField_1.setText(""+T);
                        textField_2.setText(""+F);
                        button.setVisible(false);
                        textField.setEditable(false);
                        showtext();
                    }
                    }
                    catch(NumberFormatException e1)
                    {
                        textPane.setText("输入题数错误,请重新输入!(1~5)");
                        textField.setText("");
                    }
                    
                    
                }            
            });
            textField_5.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {try{
                    if(!stop)
                    {
                    
                    
                    uanswer=Integer.parseInt(textField_5.getText());
                    if(num==count)
                    {
                        if(uanswer==ranswer) 
                        {  
                            textField_6.setText(""+ranswer);
                            textField_3.setText("答对!"); 
                            T++; 
                            textField_1.setText(""+T);    
                            count++;    
                        }  
                        else 
                        {  
                            textField_6.setText(""+ranswer);
                            textField_3.setText("答错!");
                            F++;
                            textField_2.setText(""+F);
                            count++;
                        }
                        stop=true;
                        textField_3.setText(" 答題結束!");
                        endTime=System.currentTimeMillis();
                        textField.setEditable(true);
                        button.setVisible(true);
                        textField_7.setText((endTime-startTime)/1000+"s");
                        
                    }
                    else if(uanswer==ranswer) 
                    {   
                        textField_6.setText(""+ranswer);
                        textField_3.setText("答对!"); 
                        T++; 
                        textField_1.setText(""+T);    
                        showtext();
                        count++;
                        
                    }  
                    else
                    {   
                        textField_6.setText(""+ranswer);
                        textField_3.setText("答错!");
                        F++;
                        textField_2.setText(""+F);
                        showtext();
                        count++;
                    }
                    }
                }
                    
                    catch(NumberFormatException e2)
                    {
                        textPane.setText(str+"答案格式错误,请重新输入答案!");
                        textField_5.setText("");
                    }
                
                }
            });
            GroupLayout gl_panel = new GroupLayout(panel);
            gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel.createSequentialGroup()
                        .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 243, 
    
    GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(ComponentPlacement.RELATED, 
    
    GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 196, 
    
    GroupLayout.PREFERRED_SIZE)
                        .addGap(28))
            );
            gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel.createSequentialGroup()
                        .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                            .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 484, 
    
    GroupLayout.PREFERRED_SIZE)
                            .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 
    
    GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            panel.setLayout(gl_panel);
            frame.getContentPane().setLayout(groupLayout);
            frame.setBounds(100, 100, 454, 518);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }    
        void showtext()
        {
            char op='+';
            int a=0,b=0,c=0,e=0,f=0,opnum=0,chioce=0;
            int temp=0;
            Random ra =new Random();
            e=ra.nextInt(10)+1;
            f=ra.nextInt(10)+1;
            chioce=ra.nextInt(2)+1;
            if(e%2==0)
            {
            a=ra.nextInt(10)+2;
            }
            else
            {
                a=-ra.nextInt(10)+2;
            }
            if(f%2==0)
            {
            b=ra.nextInt(10)+2;
            }
            else
            {
                b=-ra.nextInt(10)+2;
            }
            c=ra.nextInt(5);
            opnum=Math.abs(ra.nextInt())%4+1;    
            textField_5.setText("");
            switch(chioce)
            {
            case 1:
            {
            switch(opnum)  
                {  
                case 1:    
                    op='+';  
                    break;
                case 2:  
                    op='-'; 
                    break;
                case 3:  
                    op='*';    
                    break;
                case 4:  
                    op='/';  
                    break; 
                }                                               
                                                                
                if((op=='-') && (a<b)) 
                {   
                    temp=a; 
                    a=b;
                    b=temp;  
                }
                                                              
                if(op=='/')  
                {   
                    if(b==0)
                    {    
                        b=1;  
                    }   
                    a=a*b; 
                }  
                if(a>0&&b>0)
                {
                    z=String.valueOf(a)+String.valueOf(op)+String.valueOf(b)+"="+"
    ";
                }
                else if(a<0&&b>0)
                {
                    z="("+String.valueOf(a)+")"+String.valueOf(op)+String.valueOf(b)+"="+"
    ";
                }
                else if(a>0&&b<0)
                {
                    z=String.valueOf(a)+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"
    ";
                }
                else
                {
                    z="("+String.valueOf(a)+")"+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"
    ";
                }
                str=str+z;                                                  
                textPane.setText(str); 
            SiZeYunSuan core=new SiZeYunSuan();
            ranswer=core.test1(op, a, b);
                break;
            }
            case 2:
            {
                str=str+String.valueOf(c)+"!="+"
    ";                                 
                textPane.setText(str);
                SiZeYunSuan core=new SiZeYunSuan();
                ranswer=core.test2(c);
                break;
            }
            }
        }
    }
      
    新代码
  • 相关阅读:
    Java核心类库——线程Thread
    xml基本写法和dtd schema的用法,JAVA读写XML
    Java核心类库——文件和文件夹的管理File类
    使用文件依赖项缓存页输出
    根据 HTTP 标头缓存页的版本
    缓存 ASP.NET 页的某些部分
    根据请求浏览器缓存页的版本
    根据自定义字符串缓存页的版本
    缓存页的多个版本
    阿拉的宣告~~~
  • 原文地址:https://www.cnblogs.com/Russelling/p/4468130.html
Copyright © 2020-2023  润新知