语言编程练习"/>
JAVA语言编程练习
设计一个简单计算器,如下图所示。在“操作数”标签右侧的两个文本框输入操作数,当单击操作符+,-,×,÷按钮时,对两个操作数进行运算并将结果填入到“计算结果”标签右侧的文本框中
。
完成基本功能后,增加对用户输入数据的验证,如果用户输入的不是数值型,给出提示。
提示:
(1)如利用文本框的String getText()方法返回字符串s,需要将其利用Integer.parseInt(s)转成整数或 double Double.parseDouble(s)转换成浮点型数才能运算,运算完毕后的结果,需要再用String.valueOf方法再转换回字符串才能利用文本框的setText方法显示回到文本框中。
(2)对不是数字组成的字符串,将其转换成int,或double或float时,会出现NumberFormatException异常。比如,Integer.parseInt("a123")
思路:
1、分析布局及所需要的组件
2、添加组件并设计排版(建议排版使用Box组件)
3、实现程序与客户的交互功能
代码:
simpleCalculator类:
//simpleCalculator类
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;public class simpleCalculator extends JFrame{//四个按钮JButton jb1 = new JButton("+");JButton jb2 = new JButton("-");JButton jb3 = new JButton("*");JButton jb4 = new JButton("/");JButton clear = new JButton("ESC");//三个文本框JTextField num1 = new JTextField(12);JTextField num2 = new JTextField(12);JTextField result = new JTextField(12);//三个标签JLabel lab1 = new JLabel("操作数 1:");JLabel lab2 = new JLabel("操作数 2:");JLabel lab3 = new JLabel("计算结果 :");//三个垂直盒一个水平盒Box baseBox,leftBox,midleBox,rightBox;//构造函数simpleCalculator(){//创建左盒子leftBox = Box.createVerticalBox();leftBox.add(lab1);leftBox.add(Box.createVerticalStrut(30));leftBox.add(lab2);leftBox.add(Box.createVerticalStrut(30));leftBox.add(lab3);//创建中间盒midleBox = Box.createVerticalBox();midleBox.add(num1);midleBox.add(Box.createVerticalStrut(10));midleBox.add(num2);midleBox.add(Box.createVerticalStrut(10));midleBox.add(result);//创建右盒子rightBox = Box.createVerticalBox();rightBox.add(jb1);rightBox.add(Box.createVerticalStrut(10));rightBox.add(jb2);rightBox.add(Box.createVerticalStrut(10));rightBox.add(jb3);rightBox.add(Box.createVerticalStrut(10));rightBox.add(jb4);rightBox.add(Box.createVerticalStrut(10));rightBox.add(clear);//创建母盒baseBox = Box.createHorizontalBox();baseBox.add(leftBox);baseBox.add(Box.createHorizontalStrut(30));baseBox.add(midleBox);baseBox.add(Box.createHorizontalStrut(30));baseBox.add(rightBox);//添加事件响应Alistener ac = new Alistener();jb1.addActionListener(ac);jb2.addActionListener(ac);jb3.addActionListener(ac);jb4.addActionListener(ac);clear.addActionListener(ac);result.addActionListener(ac);//光标在第三个格子时按下回车清零//创建窗口JFrame jf = new JFrame("SimpleCalculator");jf.add(baseBox);jf.setLayout(new FlowLayout());jf.setBounds(200, 200, 400, 300);jf.setVisible(true);jf.setDefaultCloseOperation(EXIT_ON_CLOSE);}class Alistener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getActionCommand().equals("+")){try {String n1 = num1.getText();String n2 = num2.getText();double n1_= Double.parseDouble(n1);double n2_= Double.parseDouble(n2);double n3 = n1_+n2_;String n3_= String.valueOf(n3);result.setText(n3_);}catch(NumberFormatException q){lab1.setForeground(Color.RED);lab1.setText("输入格式有误");lab2.setForeground(Color.RED);lab2.setText("输入格式有误");}}else if(e.getActionCommand().equals("-")){try {String n1 = num1.getText();String n2 = num2.getText();double n1_= Double.parseDouble(n1);double n2_= Double.parseDouble(n2);double n3 = n1_-n2_;String n3_= String.valueOf(n3);result.setText(n3_);}catch(NumberFormatException q){lab1.setForeground(Color.RED);lab1.setText("输入格式有误");lab2.setForeground(Color.RED);lab2.setText("输入格式有误");}}else if(e.getActionCommand().equals("*")){try {String n1 = num1.getText();String n2 = num2.getText();double n1_= Double.parseDouble(n1);double n2_= Double.parseDouble(n2);double n3 = n1_*n2_;String n3_= String.valueOf(n3);result.setText(n3_);}catch(NumberFormatException q){lab1.setForeground(Color.RED);lab1.setText("输入格式有误");lab2.setForeground(Color.RED);lab2.setText("输入格式有误");}}else if(e.getActionCommand().equals("/")){try {String n1 = num1.getText();String n2 = num2.getText();double n1_= Double.parseDouble(n1);double n2_= Double.parseDouble(n2);double n3 = n1_/n2_;String n3_= String.valueOf(n3);result.setText(n3_);}catch(NumberFormatException q){lab1.setForeground(Color.RED);lab1.setText("输入格式有误");lab2.setForeground(Color.RED);lab2.setText("输入格式有误");}}else if(e.getActionCommand().equals("ESC")||e.getSource()==result){num1.setText(" ");num2.setText(" ");result.setText(" ");lab1.setForeground(Color.BLACK);lab2.setForeground(Color.BLACK);lab3.setForeground(Color.BLACK);lab1.setText("操作数 1:");lab2.setText("操作数 2:");lab3.setText("计算结果 :");}}}}
Main类:
public class Main {public static void main(String[] args) {// TODO Auto-generated method stubnew simpleCalculator();}}
运行结果:
更多推荐
JAVA语言编程练习
发布评论