今日总结11.11

给之前的口算题生成软件构建合适的GUI界面,并尝试到处exe可运行文件。实现效果截图及相关代码。

 

 

package Operation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainMenu extends JFrame {

    private static final long serialVersionUID = 1L;  //定义程序序列化,1L是默认定义

    protected static String s1;
    protected static String s2;
    protected static String s3;

    private JLabel jl1 = new JLabel("欢迎来到数学答题系统");
    private JLabel jl3 = new JLabel("    ");
    private JLabel jl4 = new JLabel("        ");
    private JButton JB1 = new JButton("开始答题");
    private JPanel jp1 = new JPanel();
    private JPanel jp2 = new JPanel();
    private JPanel jp3 = new JPanel();
    private JPanel jp4 = new JPanel();
    private JPanel jp5 = new JPanel();
    private JPanel jp6 = new JPanel();
    private JPanel jp7 = new JPanel();
    private void Event() {

        JB1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                setVisible(false);
                dispose();

                new Test();
            }


        });


    }

    public MainMenu() {

        this.setTitle("欢迎来到口算答题系统");
        this.setSize(500, 450);
        this.setLocationRelativeTo(null);  //将此窗口置于屏幕的中央
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout(20,20));

        jl1.setFont(new Font("黑体",Font.BOLD,35));
        jl1.setForeground(Color.green);
        jp1.add(jl1);
        jp1.setLayout(new GridBagLayout());
        this.add(jp1,BorderLayout.NORTH);




        jp7.add(JB1);


        this.add(jp7,BorderLayout.SOUTH);


        //美化界面
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        Event();

        this.setVisible(true);
    }
    public static void main(String[] args) {

        new MainMenu();
    }


}
package Operation;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;


public class Test extends JFrame{

    private static final long serialVersionUID = 1L;  //定义程序序列化,1L是默认定义

    private int max = 50;
    private int a = 0;
    private int b = 0;
    private int c = 0;
    private int count = 1;

    private JLabel JLgross = new JLabel("共10页");
    private JLabel JLpage = new JLabel();  //页码控制
    private JLabel jl1 = new JLabel("  "); //插两个空格进去

    private JLabel JLhour = new JLabel();
    private JLabel JLminute = new JLabel();
    private JLabel JLseconds = new JLabel();

    private JButton JB1 = new JButton("提交");
    private JButton b1 = new JButton("首页");
    private JButton b2 = new JButton("上一页");
    private JButton b3 = new JButton("下一页");
    private JButton b4 = new JButton("尾页");
    private JButton b5 = new JButton("批改答案");
    private JButton b6 = new JButton("返回");

    private JLabel jl[] = new JLabel[50]; //50个题目标签
    private JTextField jtf[] = new JTextField[50];//50个文本框,存储答案
    private JTextField jta[] = new JTextField[50];//50个文本框,存储结果
    private JPanel pnl1 = new JPanel();
    private JPanel pnl2 = new JPanel();
    private JPanel pnl3 = new JPanel();
    private JPanel pnl4 = new JPanel();

    static CardLayout care = new CardLayout();

    int[] answer=new int[max];
    String[] studentAnswer=new String[max];


    public long time = 0;




    private void Event() {

        //提交试卷按钮
        JB1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int correctAnswer=0;

                for(int i=0,k=0;i<max;i++,k++){

                    studentAnswer[i]=jtf[k].getText().trim();

                    try {
                        if(Integer.parseInt(studentAnswer[i]) == answer[k]){
                            //将string字符串类型转换为integer整数类型
                            correctAnswer++;
                        }
                    }catch(NumberFormatException u) {

                    }
                }

                int score = 100*(int)correctAnswer/max;

                String s="共50道题\n";
                s=s+"答对"+correctAnswer+"道题\n";
                s=s+"答错"+(max-correctAnswer)+"道题\n";
                s=s+"成绩"+String.format("%d",score)+"分\n";
                s=s+"正确率:"+correctAnswer*100/50+"%\n";
                s=s+"答题时间:"+time+"秒";
                //  Object[] options ={ "确定", "取消" };  //自定义按钮上的文字
                JOptionPane.showMessageDialog(null, s,"本次答题情况",JOptionPane.ERROR_MESSAGE);
                JOptionPane.showMessageDialog(null,  "即将返回首页面", "提示",JOptionPane.ERROR_MESSAGE);
                // JOptionPane.showOptionDialog(null, "即将返回首页面", "提示",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
                new MainMenu();
                setVisible(false);
                dispose();


            }
        });

        //首页
        b1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                care.first(pnl3);
                count = 1;
                JLpage.setText("第" + count + "页");
            }
        });

        //前一页
        b2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                care.previous(pnl3);
                if ( count > 1 && count <=10) {
                    count --;
                }else {
                    count = 10 ;
                }
                JLpage.setText("第" + count + "页");
            }
        });

        //下一页
        b3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                care.next(pnl3);
                if ( count >= 1 && count < 10 ) {
                    count ++;
                }else {
                    count = 1 ;
                }
                JLpage.setText("第" + count + "页");
            }
        });

        //尾页
        b4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                care.last(pnl3);
                count = 10;
                JLpage.setText("第" + count + "页");
            }
        });
        b5.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                for(int i=0,k=0;i<max;i++,k++){

                    studentAnswer[i]=jtf[k].getText().trim();

                    try {
                        if(Integer.parseInt(studentAnswer[i]) == answer[k]){
                            jta[k].setText("回答正确!!");
                            jta[k].setForeground(Color.GREEN);
                        }else {
                            jta[k].setText("回答错误!! 正确答案为:"+jl[k].getText()+answer[k]);
                            jta[k].setForeground(Color.RED);
                        }
                    }catch(Exception u) {

                    }
                }
            }
        });
        b6.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(null, "是否返回首页","提示",JOptionPane.ERROR_MESSAGE);
                new MainMenu();
                setVisible(false);
                dispose();
            }
        });
    }


    public Test() {

        this.setTitle("开始答题");
        this.setSize(830, 500);
        this.setLocationRelativeTo(null);  //将此窗口置于屏幕的中央
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout(20,20)); //设置 各组件间水平垂直间隔为20像素



        pnl1.add(JLhour);
        pnl1.add(JLminute);
        pnl1.add(JLseconds);

        JLpage.setText("第" + count + "页");

        pnl2.setLayout(new FlowLayout());
        pnl2.add(JLgross);
        pnl2.add(JLpage);//页数
        pnl2.add(jl1);

        pnl2.add(pnl1);
        pnl2.add(JB1);

        this.add(pnl2,BorderLayout.NORTH);

        pnl3.setLayout(care);
        this.add(pnl3,BorderLayout.CENTER);
        pnl3.setBorder(BorderFactory.createLoweredBevelBorder()); //边框

        for(int i = 0 , k = 0; i < 10 ; i++ ) {

            JPanel p = new JPanel();
            p.setLayout(new GridLayout(5,2,20,20)); //网格布局5行2列,水平垂直间距都设为20像素

            for(int j = 0 ; j < 5 ; j++) {

                a=(int)(Math.random()*100+1);
                b=(int)(Math.random()*100+1);
                String random = "";
                String[] doc = {"+", "-"};
                int index = (int) (Math.random() * doc.length);
                random = doc[index];

                if(random=="-") {
                    if(a>b) {
                        jl[k] = new JLabel(a + random + b + "=");
                        jl[k].setFont(new Font("黑体",Font.BOLD,20));
                        p.add(jl[k]);
                        answer[k] = a - b ;
                    }else {
                        jl[k] = new JLabel(b + random + a + "=");
                        jl[k].setFont(new Font("黑体",Font.BOLD,20));
                        p.add(jl[k]);
                        answer[k] = b - a ;
                    }
                }
                else if(random=="+") {
                    jl[k] = new JLabel(a + random + b + "=");
                    jl[k].setFont(new Font("黑体",Font.BOLD,20));
                    p.add(jl[k]);
                    answer[k] = a + b ;

                }




                jtf[k] = new JTextField(6);
                jta[k] = new JTextField(6);
                p.add(jtf[k]);
                p.add(jta[k]);
                jtf[k].setText(null);
                jtf[k].setText(null);
                k++;
            }

            pnl3.add(p);
        }

        pnl4.setLayout(new GridLayout(1,4));

        pnl4.add(b1);
        pnl4.add(b2);
        pnl4.add(b3);
        pnl4.add(b4);
        pnl4.add(b5);
        pnl4.add(b6);
        this.add(pnl4, BorderLayout.SOUTH);

        //美化界面
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        Event();

        setVisible(true);

    }



}

 

posted @ 2023-11-11 23:31  庞司令  阅读(10)  评论(0)    收藏  举报