2021-04-24

Java入门 老师布置的组件及事件处理作业,可以来看看,练练手

组件及事件处理主要练的只是思路,因为现在大多不用java自带的GUI(图形用户界面)来进行编程,因为……长的太low了吧,不能满足需要。

老师给的作业要求:
编写一个应用程序,有一个标题为“计算”的窗口,窗口的布局为FlowLayout布局。设计四个按钮,分别命名为“加”、“差”、“积、”、“除”,另外,窗口中还有三个文本框:两个用来输入数字,一个用来显示运算结果。单击相应的按钮,将两个文本框的数字做运算,在第三个文本框中显示结果。要求处理NumberFormatException异常。

以下是我自己码的代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;

public class Calculator   {
    static JFrame jFrame = new JFrame("煜哥计算器");//标题
    public static void main(String[] args) {
        FlowLayout flowLayout = new FlowLayout();//实例化FlowLayout布局
        jFrame.setLayout(flowLayout);//jFrame为FlowLayout布局
        jFrame.setSize(200,300);//窗口宽200,长300
        JTextField text1,text2,text3;//三个文本框组件
        JButton button1,button2,button3,button4;//四个按钮组件
        text1=new JTextField(10);//输入框的长度为10列
        text2=new JTextField(10);
        text3=new JTextField(10);
        button1=new JButton("加");
        button2=new JButton("减");
        button3=new JButton("乘");
        button4=new JButton("除");
        jFrame.add(text1);
        jFrame.add(text2);
        jFrame.add(text3);
        jFrame.add(button1);
        jFrame.add(button2);
        jFrame.add(button3);
        jFrame.add(button4);

        //关闭窗口时程序就关闭:
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);//窗口显示

        button1.addActionListener(new ActionListener(){//ActionListener是匿名类
            public void actionPerformed(ActionEvent e){
                String s1=text1.getText();//接收输入
                String s2=text2.getText();
                try{ BigInteger n1=new BigInteger(s1);//将字符串转为文字
                    BigInteger n2=new BigInteger(s2);
                    n2=n1.add(n2);//计算
                    text3.setText(n2.toString());//输出
                }
                catch(NumberFormatException ee){//数字化格式异常的错误处理
                    text3.setText("请输入数字字符");
                    text1.setText(null);
                    text2.setText(null);
                }
            }
        });
        button2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String s1=text1.getText();
                String s2=text2.getText();
                try{ BigInteger n1=new BigInteger(s1);
                    BigInteger n2=new BigInteger(s2);
                    n2=n1.subtract(n2);
                    text3.setText(n2.toString());
                }
                catch(NumberFormatException ee){
                    text3.setText("请输入数字字符");
                    text1.setText(null);
                    text2.setText(null);
                }
            }
        });
        button3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String s1=text1.getText();
                String s2=text2.getText();
                try{ BigInteger n1=new BigInteger(s1);
                    BigInteger n2=new BigInteger(s2);
                    n2=n1.multiply(n2);
                    text3.setText(n2.toString());
                }
                catch(NumberFormatException ee){
                    text3.setText("请输入数字字符");
                    text1.setText(null);
                    text2.setText(null);
                }
            }
        });
        button4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String s1=text1.getText();
                String s2=text2.getText();
                try{ BigInteger n1=new BigInteger(s1);
                    BigInteger n2=new BigInteger(s2);
                    if(n2.toString()=="0"){
                        text3.setText("除数不能为0");
                    }
                    else
                    {
                        n2=n1.divide(n2);
                        text3.setText(n2.toString());
                    }
                }
                catch(NumberFormatException ee){
                    text3.setText("请输入数字字符");
                    text1.setText(null);
                    text2.setText(null);
                }
            }
        });
    }
}

最后运行窗口

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值