用JAVA编写的计算器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用JAVA编写的计算器相关的知识,希望对你有一定的参考价值。
package main;import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener
static Panel pan = new Panel();
static JTextField textField = new JTextField("0");
static Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bp, ba, bs, bm, bd,
be, bc, bt, bf, bh;
private StringBuffer temp = new StringBuffer("");
private String optValue = "0";
private String optType="";
private boolean isChoiseOptType=true;
public void init()
b0 = new Button("0");
b0.addActionListener(this);
b1 = new Button("1");
b1.addActionListener(this);
b2 = new Button("2");
b2.addActionListener(this);
b3 = new Button("3");
b3.addActionListener(this);
b4 = new Button("4");
b4.addActionListener(this);
b5 = new Button("5");
b5.addActionListener(this);
b6 = new Button("6");
b6.addActionListener(this);
b7 = new Button("7");
b7.addActionListener(this);
b8 = new Button("8");
b8.addActionListener(this);
b9 = new Button("9");
b9.addActionListener(this);
bp = new Button(".");
bp.addActionListener(this);
ba = new Button("+");
ba.addActionListener(this);
bs = new Button("-");
bs.addActionListener(this);
bm = new Button("*");
bm.addActionListener(this);
bd = new Button("/");
bd.addActionListener(this);
be = new Button("=");
be.addActionListener(this);
bc = new Button("c");
bc.addActionListener(this);
bt = new Button("退格");
bt.addActionListener(this);
bf = new Button("1/x");
bf.addActionListener(this);
bh = new Button("+/-");
bh.addActionListener(this);
this.setTitle("计算机");
this.setLayout(null);
this.setSize(260, 300);
this.setResizable(false);
GridLayout grid = new GridLayout(4, 5);
pan.setLayout(grid);
pan.setBounds(20, 60, 150, 120);
textField.setBounds(20, 35, 150, 20);
textField.setBackground(Color.cyan);
textField.setHorizontalAlignment(textField.RIGHT);
textField.setEditable(false);
pan.add(b1);
pan.add(b2);
pan.add(b3);
pan.add(ba);
pan.add(bc);
pan.add(b4);
pan.add(b5);
pan.add(b6);
pan.add(bs);
pan.add(bt);
pan.add(b7);
pan.add(b8);
pan.add(b9);
pan.add(bm);
pan.add(bf);
pan.add(b0);
pan.add(bh);
pan.add(bp);
pan.add(bd);
pan.add(be);
this.add(textField);
this.add(pan);
public static void main(String[] args)
Calculator frm = new Calculator();
frm.init();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent e)
String value="0";
if (e.getSource().equals(b0))
this.temp.append(b0.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
else if (e.getSource().equals(b1))
this.temp.append(b1.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
else if (e.getSource().equals(b2))
this.temp.append(b2.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
else if (e.getSource().equals(b3))
this.temp.append(b3.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
else if (e.getSource().equals(b4))
this.temp.append(b4.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
else if (e.getSource().equals(b5))
this.temp.append(b5.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
else if (e.getSource().equals(b6))
this.temp.append(b6.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
else if (e.getSource().equals(b7))
this.temp.append(b7.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
else if (e.getSource().equals(b8))
this.temp.append(b8.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
else if (e.getSource().equals(b9))
this.temp.append(b9.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
else if (e.getSource().equals(bp))
if(this.temp.length()<=0)
this.temp.append("0");
this.temp.append(bp.getLabel());
this.textField.setText(this.temp.toString());
isChoiseOptType=false;
else if (e.getSource().equals(ba))
if(!isChoiseOptType)
value=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1)
value=value.substring(0,value.length()-1);
this.optValue=value;
this.temp=new StringBuffer("");
this.optType=ba.getLabel();
isChoiseOptType=true;
else if (e.getSource().equals(bs))
if(!isChoiseOptType)
value=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1)
value=value.substring(0,value.length()-1);
this.optValue=value;
this.temp=new StringBuffer("");
this.optType=bs.getLabel();
isChoiseOptType=true;
else if (e.getSource().equals(bm))
if(!isChoiseOptType)
value=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1)
value=value.substring(0,value.length()-1);
this.optValue=value;
this.temp=new StringBuffer("");
this.optType=bm.getLabel();
isChoiseOptType=true;
else if (e.getSource().equals(bd))
if(!isChoiseOptType)
value=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1)
value=value.substring(0,value.length()-1);
this.optValue=value;
this.temp=new StringBuffer("");
this.optType=bd.getLabel();
isChoiseOptType=true;
else if (e.getSource().equals(be))
if(!this.optType.equals(""))
BigDecimal opt1=new BigDecimal(this.optValue);
value=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1)
value=value.substring(0,value.length()-1);
BigDecimal opt2=new BigDecimal(value);
BigDecimal result=new BigDecimal(0);
if(this.optType.equals("+"))
result=opt1.add(opt2);
else if(this.optType.equals("-"))
result=opt1.subtract(opt2);
else if(this.optType.equals("*"))
result=opt1.multiply(opt2);
else if(this.optType.equals("/"))
result=opt1.divide(opt2);
else if(this.optType.equals("%"))
result=opt1.remainder(opt2);
this.textField.setText(result.toString());
this.temp=new StringBuffer("");
isChoiseOptType=false;
this.optValue="0";
else if (e.getSource().equals(bc))
this.temp=new StringBuffer();
this.textField.setText("0");
else if (e.getSource().equals(bt))
value=this.textField.getText();
value=value.substring(0,value.length()-1);
if(value.indexOf("-")>=0 && value.length()<=1)
value="0";
this.temp=new StringBuffer("");
else
this.temp=new StringBuffer(value);
this.textField.setText(value);
else if (e.getSource().equals(bh))
value=this.textField.getText();
if(value.indexOf("-")==0)
value=value.substring(1,value.length());
else
value="-"+value;
this.temp=new StringBuffer(value);
this.textField.setText(value);
else if (e.getSource().equals(bf))
this.optValue=this.textField.getText();
if(value.lastIndexOf(".")==value.length()-1)
this.optValue=this.optValue.substring(0,this.optValue.length()-1);
Integer opt1=new Integer(this.optValue);
if(!opt1.toString().equals("0"))
this.textField.setText(1.0/opt1.intValue()+"");
System.out.println(1/opt1.intValue()+"");
else
this.textField.setText("0");
this.temp=new StringBuffer("");
this.optType="";
this.optValue="0";
参考技术A 直接上代码,不过估计会被吞
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator
//计算器面板
private JFrame f = new JFrame("Calculator");
//输入面扳
private JPanel inputPanel = new JPanel();
//加减乘除面板
private JPanel operationPanel = new JPanel();
//数字面板
private JPanel buttonsPanel = new JPanel();
//输入数据文本框
private JTextField input = new JTextField(20);
//退格键
private JButton backspace = new JButton("BackSpace");
//清空
private JButton CE = new JButton("CE ");
//删除
private JButton C = new JButton("C ");
//四则运算符号键
private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mul = new JButton("*");
private JButton div = new JButton("/");
//小数点
private JButton decimal = new JButton(".");
//等号
private JButton equal = new JButton("=");
//数字键
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");
private String num1 = "";//保存第一个运算数字
private String operator = "";//保存运算符号
public static void main(String[] args)
new Calculator();//new计算器实例
public Calculator()
//添加组件,布局
inputPanel.add(input);
f.add(inputPanel, BorderLayout.NORTH);
operationPanel.add(backspace);
operationPanel.add(CE);
operationPanel.add(C);
f.add(operationPanel, BorderLayout.CENTER);
buttonsPanel.add(add);
buttonsPanel.add(sub);
buttonsPanel.add(mul);
buttonsPanel.add(div);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(zero);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(decimal);
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(equal);
buttonsPanel.setLayout(new GridLayout(4, 4));
f.add(buttonsPanel, BorderLayout.SOUTH);
//注册各个组件监听事件
backspace.addMouseListener(new OperationMouseListener());
CE.addMouseListener(new OperationMouseListener());
C.addMouseListener(new OperationMouseListener());
decimal.addMouseListener(new OperationMouseListener());
equal.addMouseListener(new OperationMouseListener());
//注册四则运算监听
add.addMouseListener(new CalcMouseListener());
sub.addMouseListener(new CalcMouseListener());
mul.addMouseListener(new CalcMouseListener());
div.addMouseListener(new CalcMouseListener());
//注册数字监听事件
zero.addMouseListener(new NumberMouseListener());
one.addMouseListener(new NumberMouseListener());
two.addMouseListener(new NumberMouseListener());
three.addMouseListener(new NumberMouseListener());
four.addMouseListener(new NumberMouseListener());
five.addMouseListener(new NumberMouseListener());
six.addMouseListener(new NumberMouseListener());
seven.addMouseListener(new NumberMouseListener());
eight.addMouseListener(new NumberMouseListener());
nine.addMouseListener(new NumberMouseListener());
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
private class NumberMouseListener implements MouseListener
public void mouseClicked(MouseEvent e)
if(input.getText().trim().equals("0"))//如果文本框已经是0,结果还是0
input.setText(((JButton)e.getSource()).getText().trim());
else//否则的话,把0添加到后面,譬如文本框是1,结果就为10
input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
public void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)
private class CalcMouseListener implements MouseListener
//如果输入的是运算符号,保存第一个结果和运算符号
public void mouseClicked(MouseEvent e)
num1 = input.getText().trim();input.setText("");
operator = ((JButton)e.getSource()).getText().trim();
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
public void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)
private class OperationMouseListener implements MouseListener
public void mouseClicked(MouseEvent e)
if(e.getSource() == backspace)//退格键,删除一个直到没有字符删除
String inputText = input.getText();
if(inputText.length() > 0)
input.setText(inputText.substring(0, inputText.length() - 1));
else if(e.getSource() == C)
input.setText("0");//C,清空所有运算数字和符号
num1 = "";
else if(e.getSource() == CE)
input.setText("0");//CE-->将文本框置为0
else if(e.getSource() == decimal)
String text = input.getText().trim();
//如果按了小数点,如果文本框已经有小数点,不做任何操作,否则在结果后面加上小数点
if(text.indexOf(".") == -1)
input.setText(text.concat("."));
else if(e.getSource() == equal)
//如果是等号
if(!operator.trim().equals(""))
if(!input.getText().trim().equals(""))
double result = 0D;
if(operator.equals("+"))//执行加法运算
result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());
else if(operator.equals("-"))//减法运算
result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());
else if(operator.equals("*"))//乘法运算
result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());
else if(operator.equals("/"))//除法运算
result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());
//格式化最终结果,保留两位小数点
input.setText(new DecimalFormat("0.00").format(result));
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
public void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)
用java编写一个倒计时器代码。
要求:1.从60秒开始倒计时并显示在界面2中。
2.当等于0秒时,跳转到界面1。
3.当在界面2中点击按钮1进入界面3时,界面2中的倒计时会暂停(即进入界面3后不会跳转到界面1)
总得来说,就是类似ATM机上的倒计时功能。
对了。需要界面2跳转到界面1时,界面2会消失。
以上是关于用JAVA编写的计算器的主要内容,如果未能解决你的问题,请参考以下文章