编写程序,如下所示。设计一个计算两个数的四则运算的GUI程序,操作符用菜单项表示,输入值和运算结果用文本框表示。注意:如果文本框输入的不是数值型的数据或做除法运算时除数为0,则要对程序作异常处理。

编写程序,如下所示。设计一个计算两个数的四则运算的GUI程序,操作符用菜单项表示,输入值和运算结果用文本框表示。注意:如果文本框输入的不是数值型的数据或做除法运算时除数为0,则要对程序作异常处理。
仅供学习

package day01;

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

public class Text4 extends JFrame {
    public static class cannian extends JFrame {
        MenuBar menuBar;
        Menu m1, m2;
        MenuItem menuItem1, menuItem2, menuItem3, menuItem4, menuItem5;
        JLabel a1;
        JLabel a2;
        JLabel a3;
        JLabel a4;
        JTextField b1, b2;

        public cannian() {
            menuBar = new MenuBar();
            //设置菜单栏
            this.setMenuBar(menuBar);
            //创建菜单
            m1 = new Menu("运算", true);
            //向菜单栏中添加菜单
            menuBar.add(m1);
            //创建选项
            menuItem1 = new MenuItem("加+");
            menuItem2 = new MenuItem("减-");
            menuItem3 = new MenuItem("乘");
            menuItem4 = new MenuItem("除/");
            //创建菜单
            m2 = new Menu("程序", true);
            menuBar.add(m2);
            //创建选项
            menuItem5 = new MenuItem("退出");
            m2.add(menuItem5);
            //添加二级菜单
            m1.add(menuItem1);
            m1.add(menuItem2);
            m1.add(menuItem3);
            m1.add(menuItem4);
            m2.add(menuItem5);
            //创建无文本标签和文本框
            a1 = new JLabel("数值一:");
            a2 = new JLabel("数值二:");
            a3 = new JLabel("运算结果");
            a4 = new JLabel("");
            b1 = new JTextField();
            b2 = new JTextField();
            this.setLayout(null);
            a1.setBounds(60, 60, 70, 30);
            b1.setBounds(180, 60, 70, 30);
            a3.setBounds(60, 360, 70, 30);
            a4.setBounds(200, 360, 70, 30);
            a2.setBounds(60, 200, 70, 30);
            b2.setBounds(180, 200, 70, 30);
            a1.setForeground(Color.BLUE);
            a2.setForeground(Color.BLUE);
            a3.setForeground(Color.RED);
            a4.setForeground(Color.RED);
            this.add(a1);
            this.add(a2);
            this.add(a3);
            this.add(a4);
            this.add(b1);
            this.add(b2);
            ButtonListener ac = new ButtonListener();
            menuItem1.addActionListener(ac);
            menuItem2.addActionListener(ac);
            menuItem3.addActionListener(ac);
            menuItem4.addActionListener(ac);
            menuItem5.addActionListener(ac);
            this.setSize(600, 600);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }

        public class ButtonListener implements ActionListener {
            //重写ActionListener接口中的事件处理方法actionPerformed()
            public void actionPerformed(ActionEvent e) {
                String str1 =b1.getText();//与后面的setText对比记忆
                String str2 =b2.getText();
                int num1 = Integer.parseInt(str1);
                int num2 = Integer.parseInt(str2);
                Object source = e.getSource();
                if (source == menuItem1) {//+
                    int value1 = Integer.parseInt(b1.getText());
                    int value2 = Integer.parseInt(b2.getText());
                    a4.setText(Integer.toString(value1+value2));

                } else if (source == menuItem2) {//-
                    int value1 = Integer.parseInt(b1.getText());
                    int value2 = Integer.parseInt(b2.getText());
                    a4.setText(Integer.toString(value1-value2));
                } else if (source == menuItem3) {//*
                    int value1 = Integer.parseInt(b1.getText());
                    int value2 = Integer.parseInt(b2.getText());
                    a4.setText(Integer.toString(value1*value2));
                } else if (source == menuItem4) {///

                    try {
                        int value1 = Integer.parseInt(b1.getText());
                        int value2 = Integer.parseInt(b2.getText());
                        a4.setText(Integer.toString(value1/value2));
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        System.out.println("除数不能为0");
                    }
                } else if (source == menuItem5) {//exit
                    System.exit(0);
                }
            }
        }
    }

    public static void main(String[] args) {
        cannian a = new cannian();
        a.setVisible(true);
    }
}

你可能感兴趣的:(日常作业)