计算器 1.0

小计算器

 

 

编写一个应用程序,有一个标题为“计算”的窗口,窗口的布局为FlowLayout布局。

设计四个按钮,分别命名为“加”、“差”、“积、”、“除”;

另外,窗口中还有三个文本框。单击相应的按钮,将两个文本框的数字做运算,在第三个文本框中显示结果。要求处理NumberFormatException异常。

/**

 * Counter.java

 * @author:王超

 * 2016年11月22日

 * com.wangchao.shangji.exercise.第七次试验.Counter

 * Copyright (c) 2007, 2016 Infopower corporation All Rights Reserved.

 */

package com.wangchao.shangji.exercise.第七次试验;

 

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextField;

 

/**

 *

 * <p>

 * 描述该类情况 {@link 代表跟谁有关系}

 * </p>

 *

 * @author 王超

 * @since 1.0

 * @date 2016年11月22日 下午6:13:26

 * @see 新建|修改|放弃

 * @see com.wangchao.shangji.exercise.第七次试验.Counter.计算器

 */

 

public class Counter extends JFrame

{

         /***/

         private static final long serialVersionUID = 1L;

 

         public static void main(String[] args)

         {

                   Counter counter = new Counter();

                   counter.setVisible(true);

         }

 

         JButton add = new JButton("加");

         JButton cut = new JButton("减");

         JButton division = new JButton("除");

         JButton take = new JButton("乘");

         JTextField text1 = new JTextField("", 7);

         JTextField text2 = new JTextField("", 7);

         JTextField text3 = new JTextField("", 7);

 

         public Counter()

         {

                   setTitle("计算器");

                   // 设置窗体大小

                   setSize(300, 300);

                   // 设置窗口位置

                   setLocation(400, 400);

                   // 关闭窗口的操作

                   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                   // 设置布局

                   setLayout(new FlowLayout());

                   // 输入框1

                   add(this.text1);

                   // 输入框2

                   add(this.text2);

                   // 结果框3

                   add(this.text3);

                   // 按钮 加法

                   add(this.add);

                   // 按钮 减法

                   add(this.cut);

                   // 按钮 乘法

                   add(this.take);

                   // 按钮 除法

                   add(this.division);

                   // 加法的监听事件

                   this.add.addActionListener(new ActionListener()

                   {

                            @Override

                            public void actionPerformed(ActionEvent e)

                            {

                                     String strText1 = getText1();

                                     Integer te1 = new Integer(strText1);

                                     String strText2 = getText2();

                                     Integer te2 = new Integer(strText2);

                                     Integer te3 = te1 + te2;

                                     setText3(te3.toString());

                            }

                   });

                   // 减法的监听事件

                   this.cut.addActionListener(new ActionListener()

                   {

                            @Override

                            public void actionPerformed(ActionEvent e)

                            {

                                     String strText1 = getText1();

                                     Integer te1 = new Integer(strText1);

                                     String strText2 = getText2();

                                     Integer te2 = new Integer(strText2);

                                     Integer te3 = te1 - te2;

                                     setText3(te3.toString());

                            }

                   });

                   // 乘法的监听事件

                   this.take.addActionListener(new ActionListener()

                   {

                            @Override

                            public void actionPerformed(ActionEvent e)

                            {

                                     String strText1 = getText1();

                                     Integer te1 = new Integer(strText1);

                                     String strText2 = getText2();

                                     Integer te2 = new Integer(strText2);

                                     Integer te3 = te1 * te2;

                                     setText3(te3.toString());

                            }

                   });

                   // 除法的监听事件

                   this.division.addActionListener(new ActionListener()

                   {

                            @Override

                            public void actionPerformed(ActionEvent e)

                            {

                                     String strText1 = getText1();

                                     Integer te1 = new Integer(strText1);

                                     String strText2 = getText2();

                                     Integer te2 = new Integer(strText2);

                                     Double te3 = te1 / te2.doubleValue();

                                     setText3(te3.toString());

                            }

                   });

         }

 

         // 获取text1的值

         public String getText1()

         {

                   if (this.text1.getText() == null)

                   {

                            throw new NumberFormatException();

                   }

                   else

                   {

                            return this.text1.getText();

                   }

         }

 

         // 获取text2的值

         public String getText2()

         {

                   if (this.text2.getText() == null)

                   {

                            throw new NumberFormatException();

                   }

                   else

                   {

                            return this.text2.getText();

                   }

         }

 

         // 设置text3的值

         public void setText3(String t3)

         {

                   this.text3.setText(t3);

         }

}

posted @ 2016-11-22 20:34  qingtianBKY  阅读(729)  评论(0编辑  收藏  举报