每日总结-23.11.23

package kousuanti;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class math {
    static java.util.Scanner cin = new java.util.Scanner(System.in);
    static public void main(String[] args) {
        JFrame jf =new JFrame();
        JPanel Jp = new JPanel();//申请容器
        jf.setLayout(null);
        jf.setSize(260, 400);//设置窗体大小
        jf.setLocationRelativeTo(null);//设置窗体于屏幕中央位置
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//点击叉叉可以关闭窗体
        jf.setVisible(true);//组件可见
        Jp.setLayout(null);
        jf.setResizable(true);//窗体不可变大或者变小
        jf.setContentPane(Jp);//不知道
        jf.setVisible(true);

        String temp="";
        JLabel la[]=new JLabel[10];//创造题目标签数组
        JTextField jt[] = new JTextField[10];//创造文本框数组
        topic T[] = new topic[10];//随机申请十道题目

        for(int i=0;i<T.length;i++){
            temp="";
            T[i]=new topic();
            temp=temp+"第"+(1+i)+"题."+T[i].data_first+""+T[i].operator+""+T[i].data_last+"=";//题目拼接
            la[i]=new JLabel(temp);//对数组中的标签申请空间并初始化
            la[i].setBounds(20, 40+i*25, 100, 20);//设置标签的位置
            Jp.add(la[i]);//加入容器中
        }
        for(int i=0;i<T.length;i++){
            jt[i]=new JTextField(12);//申请文本框空间并初始化
            jt[i].setVisible(true);
            jt[i].setBounds(95, 40+i*25, 40, 20);//设置文本框位置
            Jp.add(jt[i]);
        }


        JButton button =new JButton("提交");//申请按钮实例

        button.setBounds(85, 300, 60, 30);//设置按钮位置
        jf.add(button);//按钮加入
        JLabel END[]=new JLabel[10];//申请单题目对错标签

        button.addActionListener(new ActionListener(){//加入按钮监听器
            public void actionPerformed(ActionEvent e) {
                int score=0;
                int end[]=new int[10];//申请题目分数数组
                for(int i=0;i<10;i++) {
                    end[i]=Integer.valueOf(jt[i].getText());//得到单题目分数
                    if(end[i]==T[i].end) {//如果对了
                        END[i]=new JLabel("对啦");
                        score++;
                    }
                    else {//否则
                        END[i]=new JLabel("错误,答案是"+T[i].end);
                    }
                    END[i].setBounds(135, 40+i*25, 130, 20);
                }
                //Jp.removeAll();删除容器组件
                Jp.repaint();//重画
                /*分别加入组件*/
                for(int i=0;i<10;i++) {
                    Jp.add(END[i]);
                }
                JLabel Score=new JLabel("总分:"+String.valueOf(score));
                Score.setBounds( 100, 10 , 60, 20);
                Jp.add(Score);
                /*刷新*/
                Jp.updateUI();
            }
        });
    }
}

 

posted @ 2023-11-23 22:16  lao_bing  阅读(20)  评论(0)    收藏  举报