Java实现一个升级版计算器_Java实现一个简单计算器

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Caculator extends JFrame implements ActionListener {

static public String[] keys = {"1", "2", "3", "+",

"4", "5", "6", "-",

"7", "8", "9", "*",

".", "0", "log", "/",

"=", "+/-", "Del"};

static public JButton[] jb = new JButton[keys.length];

static public JTextField jt = new JTextField();

static public JPanel jp1 = new JPanel(new GridLayout(1, 1, 2, 10));

static public JPanel jp2 = new JPanel(new GridLayout(1, 4, 2, 10));

static public JPanel jp3 = new JPanel(new GridLayout(1, 4, 2, 10));

static public JPanel jp4 = new JPanel(new GridLayout(1, 4, 2, 10));

static public JPanel jp5 = new JPanel(new GridLayout(1, 4, 2, 10));

static public JPanel jp6 = new JPanel(new GridLayout(1, 2, 2, 10));

static public JPanel jp7 = new JPanel(new GridLayout(1, 1, 2, 10));

static public JPanel jp8 = new JPanel(new GridLayout(1, 2, 2, 10));

public Caculator() {

this.setLayout(new GridLayout(6, 1, 2, 10));

this.setVisible(true);

this.setBounds(300, 300, 300, 300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

for(int i=0; i

jb[i] = new JButton(keys[i]);

}

jp1.add(jt);

for(int i=0; i<4; i++) {

jp2.add(jb[i]);

}

for(int i=4; i<8; i++) {

jp3.add(jb[i]);

}

for(int i=8; i<12; i++) {

jp4.add(jb[i]);

}

for(int i=12; i<16; i++) {

jp5.add(jb[i]);

}

jp6.add(jp7);jp6.add(jp8);

jp7.add(jb[16]);    jp8.add(jb[17]);jp8.add(jb[18]);

this.add(jp1); this.add(jp2); this.add(jp3);

this.add(jp4); this.add(jp5); this.add(jp6);

for(int i=0; i

jb[i].addActionListener(this);

}

}

public static void main(String[] args) {

Caculator c = new Caculator();

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getSource().equals(jb[0])||

e.getSource().equals(jb[1])||

e.getSource().equals(jb[2])||

e.getSource().equals(jb[3])||

e.getSource().equals(jb[4])||

e.getSource().equals(jb[5])||

e.getSource().equals(jb[6])||

e.getSource().equals(jb[7])||

e.getSource().equals(jb[8])||

e.getSource().equals(jb[9])||

e.getSource().equals(jb[11])||

e.getSource().equals(jb[15])||

e.getSource().equals(jb[13])||

e.getSource().equals(jb[10])||

e.getSource().equals(jb[12])||

e.getSource().equals(jb[14])) {

myNumButtonPerformed(e);

}

if(e.getSource().equals(jb[18])) {

myDelPerformed();

}

if(e.getSource().equals(jb[16])) {

jt.setText(String.valueOf(myValuePerformed()));

}

if(e.getSource().equals(jb[17])) {

jt.setText("-");

}

}

public void myNumButtonPerformed(ActionEvent e) {

StringBuffer s = null;

double d;

s = new StringBuffer(jt.getText());

if(s.equals(null)) {

jt.setText((String)e.getActionCommand());

} else {

s = s.append(e.getActionCommand());

jt.setText(s.substring(0));;

}

}

public double myValuePerformed() {

String s = jt.getText();

char sop = 0;

double value = 0;

double d1 = 0;

double d2 = 0;

for(int i=1; i

if((s.charAt(i) == '+')||

(s.charAt(i) == '-')||

(s.charAt(i) == '*')||

(s.charAt(i) == '/')) {

sop = s.charAt(i);

}

}

if(sop == '+') {

String[] str = s.split("\\+");

d1 = Double.parseDouble(str[0]);

d2 = Double.parseDouble(str[1]);

value = d1+d2;

}

if(sop == '-') {

String[] str = s.substring(1).split("-");

StringBuffer sb = new StringBuffer(s.substring(0, 1));

sb = sb.append(str[0]);

d1 = Double.parseDouble(sb.substring(0, sb.length()));

d2 = Double.parseDouble(str[1]);

value = d1-d2;

}

if(sop == '*') {

String[] str = s.split("\\*");

d1 = Double.parseDouble(str[0]);

d2 = Double.parseDouble(str[1]);

value = d1*d2;

}

if(sop == '/') {

String[] str = s.split("/");

d1 = Double.parseDouble(str[0]);

d2 = Double.parseDouble(str[1]);

value = d1/d2;

}

if(s.substring(0, 3).equalsIgnoreCase("log")) {

String str = s.substring(3);

double d = Double.parseDouble(str);

value = Math.log(d);

}

return value;

}

public void myDelPerformed() {

String s = null;

s = jt.getText();

try {

jt.setText(s.substring(0, s.length()-1));

} catch(StringIndexOutOfBoundsException e) {

jt.setText("0");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值