用JAVA做了一个简易计算器来巩固之前学的swing方面的知识。
实现了如下功能:加减乘除、小数点、负数、清屏。主要的问题在于要将显示的逻辑和运算的逻辑分开,输入的逻辑和显示的逻辑分开。这样实现起来就不会太乱。
package com.lovo.mbt59.caculator;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ViewOfCaculator extends JFrame{
String operator ;
double formerNum = 0;
double laterNum = 0;
double result = 0;
String showNum = "0";
JTextField jtf;
boolean isDouble = false;
boolean isNegative = true;
public ViewOfCaculator()
{
super("坦克的计算器");
setSize(300,460);
setLocationRelativeTo(null);
setLayout(new GridLayout(2,1));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btncls = new JButton("cls");
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btn0 = new JButton("0");
JButton add = new JButton("+");
JButton minus = new JButton("-");
JButton divide = new JButton("/");
JButton multiply = new JButton("*");
JButton dot = new JButton(".");
JButton equal = new JButton("=");
jtf = new JTextField();
JPanel jp = new JPanel();
JPanel jp2 = new JPanel();
jp.setSize(290, 170);
jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS));
btncls.setSize(new Dimension(220, 25));
// jtf.setPreferredSize(new Dimension(280,140));
jtf.setFont(new Font("1",Font.PLAIN,50));
jtf.setHorizontalAlignment(JTextField.RIGHT);
jtf.setText(showNum);
jtf.setEditable(false);
jp.add(jtf);
btncls.setSize(140, 140);
jp.add(btncls);
jp2.setLayout(new GridLayout(4,4,15,17));
jp2.add(btn7);
jp2.add(btn8);
jp2.add(btn9);
jp2.add(divide);
jp2.add(btn4);
jp2.add(btn5);
jp2.add(btn6);
jp2.add(multiply);
jp2.add(btn1);
jp2.add(btn2);
jp2.add(btn3);
jp2.add(minus);
jp2.add(btn0);
jp2.add(dot);
jp2.add(equal);
jp2.add(add);
add(jp);
add(jp2);
//注册事件监听器
NumActionListener mal = new NumActionListener();
OpActionListener oal = new OpActionListener();
btn1.addActionListener(mal);
btn2.addActionListener(mal);
btn3.addActionListener(mal);
btn4.addActionListener(mal);
btn5.addActionListener(mal);
btn6.addActionListener(mal);
btn7.addActionListener(mal);
btn8.addActionListener(mal);
btn9.addActionListener(mal);
btn0.addActionListener(mal);
divide.addActionListener(oal);
add.addActionListener(oal);
multiply.addActionListener(oal);
minus.addActionListener(oal);
equal.addActionListener(new EqualActionListener());
dot.addActionListener(new DotActionListener());
btncls.addActionListener(new ClsActionListener());
setVisible(true);
}
public void showNum()
{
DecimalFormat df = new DecimalFormat("######.####");
double temp = 0;
try
{
temp = Double.parseDouble(showNum);
}
catch(Exception e)
{
}
if(!((temp - (int)temp)==0))
{
showNum = df.format(temp);
}
else if(isDouble)
{
}
else if(showNum.equals("除数不能为0!"))
{
}
else
{
int i = (int)temp;
showNum = String.valueOf(i);
}
jtf.setText(showNum);
}
//编写小数点的事件监听器
public class DotActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(isDouble == false)
{
showNum = showNum + ".";
isDouble = true;
showNum();
}
}
}
//编写数字按键的事件监听器
public class NumActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Object obj = arg0.getSource();
JButton jb = (JButton)obj;
String temp = jb.getText();
if(!(isNegative==true)&&Double.parseDouble(showNum) == 0 )
{
showNum = String.valueOf(temp);
showNum();
}
else
{
showNum = showNum + String.valueOf(temp);
showNum();
}
isNegative = false;
}
}
//编写运算符按键的事件监听器
public class OpActionListener implements ActionListener
{
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Object obj = arg0.getSource();
JButton jb = (JButton)obj;
operator = jb.getText();
if(operator.equals("-")&&isNegative==true&&result==0)
{
showNum = "-";
}
else
{
formerNum = Double.parseDouble(showNum);
showNum = "0";
isDouble = false;
}
}
}
//编写等号的事件监听器
public class EqualActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
laterNum = Double.parseDouble(showNum);
isNegative = true;
if(operator.equals("+"))
{
result = formerNum + laterNum;
showNum = String.valueOf(result);
showNum();
}
else if(operator.equals("-"))
{
result = formerNum - laterNum;
showNum = String.valueOf(result);
showNum();
}
else if(operator.equals("*"))
{
result = formerNum * laterNum;
showNum = String.valueOf(result);
showNum();
}
else if(operator.equals("/"))
{
if(laterNum == 0)
{
showNum = "除数不能为0!";
showNum();
}
else
{
result = formerNum / laterNum;
showNum = String.valueOf(result);
showNum();
}
}
else
{
showNum();
}
formerNum = result;
}
}
//清屏按钮的事件监听器
public class ClsActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
formerNum = 0;
laterNum = 0;
result = 0;
showNum = "0";
isDouble = false;
isNegative = true;
showNum();
}
}
public static void main(String[] args)
{
ViewOfCaculator voc = new ViewOfCaculator();
}
}