GUI--JFrame学习02(实现加减法)

发布时间 2023-11-23 17:07:17作者: 201812

实现代码

package gui;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.Random;

public class TestGui extends JFrame {
    String strs[]=new String[8];
//    int rs[] = new int[8];
    JPanel root;
    JLabel n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12;
    JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12;
    int rs1,rs2,rs3,rs4,rs5,rs6,rs7,rs8;
    JButton postBtn,resetBtn;
    //窗体初始化
    public TestGui(){

        //定义面板
        super("简单口算题生成器");
        root = new JPanel();
        setContentPane(root);
        setLayout(null);
        InitGlobalFont(new Font("仿宋", Font.PLAIN, 30));

        //获取题目
        n1 = new JLabel(create());
        n1.setBounds(25,10,90,60);
        root.add(n1);
        t1=new JTextField();
        t1.setBounds(120,10,70,55);
        t1.setBackground(Color.WHITE);
        root.add(t1);
        n2 = new JLabel(create());
        n2.setBounds(220,10,90,60);
        root.add(n2);
        t2=new JTextField();
        t2.setBounds(320,10,70,55);
        t2.setBackground(Color.WHITE);
        root.add(t2);
        n3 = new JLabel(create());
        n3.setBounds(25,80,90,60);
        root.add(n3);
        t3=new JTextField();
        t3.setBounds(120,80,70,55);
        t3.setBackground(Color.WHITE);
        root.add(t3);
        n4 = new JLabel(create());
        n4.setBounds(220,80,90,60);
        root.add(n4);
        t4 = new JTextField();
        t4.setBackground(Color.WHITE);
        t4.setBounds(320,80,70,55);
        root.add(t4);
        //80+70=150
        n5=new JLabel(create());
        n5.setBounds(25,150,90,60);
        root.add(n5);
        t5=new JTextField();
        t5.setBackground(Color.WHITE);
        t5.setBounds(120,150,70,55);
        root.add(t5);
        n6 = new JLabel(create());
        n6.setBounds(220,150,90,60);
        root.add(n6);
        t6 = new JTextField();
        t6.setBounds(320,150,70,55);
        t6.setBackground(Color.WHITE);
        root.add(t6);
        //150+70=220
        n7=new JLabel(create());
        n7.setBounds(25,220,90,60);
        root.add(n7);
        t7=new JTextField();
        t7.setBackground(Color.WHITE);
        t7.setBounds(120,220,70,55);
        root.add(t7);
        n8 = new JLabel(create());
        n8.setBounds(220,220,90,60);
        root.add(n8);
        t8 = new JTextField();
        t8.setBounds(320,220,70,55);
        t8.setBackground(Color.WHITE);
        root.add(t8);
        //220+70=290
        n9=new JLabel(create());
        n9.setBounds(25,290,90,60);
        root.add(n9);
        t9=new JTextField();
        t9.setBackground(Color.WHITE);
        t9.setBounds(120,290,70,55);
        root.add(t9);
        n10 = new JLabel(create());
        n10.setBounds(220,290,90,60);
        root.add(n10);
        t10 = new JTextField();
        t10.setBounds(320,290,70,55);
        t10.setBackground(Color.WHITE);
        root.add(t10);
        //290+70=360
        n11=new JLabel(create());
        n11.setBounds(25,360,90,60);
        root.add(n11);
        t11=new JTextField();
        t11.setBackground(Color.WHITE);
        t11.setBounds(120,360,70,55);
        root.add(t11);
        n12 = new JLabel(create());
        n12.setBounds(220,360,90,60);
        root.add(n12);
        t12 = new JTextField();
        t12.setBounds(320,360,70,55);
        t12.setBackground(Color.WHITE);
        root.add(t12);
        postBtn = new JButton("提交");
        postBtn.setBounds(100,450,110,55);
        postBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                JOptionPane.showMessageDialog(null,"提交成功","提示信息",JOptionPane.INFORMATION_MESSAGE);
            }
        });
        root.add(postBtn);
        resetBtn = new JButton("重置");
        resetBtn.setBounds(250,450,110,55);
        resetBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //隐藏旧窗口
                setVisible(false);
                new TestGui();

            }
        });
        root.add(resetBtn);


        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(400,300,450,600);
        setVisible(true);
    }

    //主函数
    public static void main(String[] args) {
        new TestGui();
    }

    //生成算数式
    public String create(){
//        int num=0;
        int x,y,z;
        Random rand=new Random();
        x= rand.nextInt(100)+1;
        y=rand.nextInt(100)+1;
        z=rand.nextInt(2);
        String str="";
        if (z==0){
//            num=x+y;
            str=x+"+"+y+"=";
            return str;
        }else {
//            num=x-y;
            str=x+"-"+y+"=";
            return str;
        }
    }

    //统一字体
    private static void InitGlobalFont(Font font) {
        FontUIResource fontRes = new FontUIResource(font);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys();
             keys.hasMoreElements(); ) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontRes);
            }
        }
    }


}