求简单java写计算器代码加减乘除

或者帮忙给个思路吧恩如何考虑编计算器呢我以前用VB编写过简单计算器有什么同理吗都得怎么长吗能给出个思路吗... 或者帮忙给个思路吧 恩 如何考虑编计算器呢
我以前用VB编写过简单计算器 有什么同理吗
都得怎么长吗 能给出个思路吗
展开
 我来答
binuisoft
推荐于2017-11-27 · TA获得超过1449个赞
知道小有建树答主
回答量:385
采纳率:0%
帮助的人:422万
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener
{
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字

// 构造者
public Calculator()
{
setTitle("Calculator");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i < 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}

// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
if (btn == clear)
{
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
}
else if (btn == equal)
{
operate('=');
equaled = true;
}
else if (btn == plus)
{
operate('+');
equaled = false;
}
else if (btn == minus)
{
operate('-');
equaled = false;
}
else if (btn == multi)
{
operate('*');
equaled = false;
}
else if (btn == div)
{
operate('/');
equaled = false;
}
else if (btn == changePN)
{
operate('p');
operate('=');
equaled = true;
}
else if (btn == sqrt)
{
operate('s');
operate('=');
equaled = true;
}
else
{
if (equaled)
storedNumber = 0;
for (int i = 0; i < 10; i++)
if (btn == numbers[i])
{
if (operation.getText().equals("0"))
operation.setText("" + i);
else if(! operated)
operation.setText(operation.getText() + i);
else
{
operation.setText("" + i);
operated = false;
}
}
if (btn == dot && ! dotExist)
{
operation.setText(operation.getText() + ".");
dotExist = true;
}
}
}
// 进行运算的方法
private void operate(char operator)
{
double currentNumber = Double.valueOf(operation.getText()).doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' && equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}

// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}

// 主方法初始化并显示窗口
public static void main(String[] args)
{
Calculator calc = new Calculator();
calc.setSize(290, 400);
calc.setVisible(true);
}
}
百度网友361ed70
2009-04-14 · 超过30用户采纳过TA的回答
知道答主
回答量:100
采纳率:0%
帮助的人:72万
展开全部
应该相同吧,发个我写的,里面的图片没有,运行就不行了,看看重点部分的吧,参考下子
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.awt.event.*;
class Jst {

JFrame j1=new JFrame("计算器");
double re,rt,n;
String s=new String("");
String s1=new String("");
String s2=new String("");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
URL url=getClass().getResource("go.gif");
Icon ic=new ImageIcon(url);
JLabel je=new JLabel(new ImageIcon(getClass().getResource("go1.jpg")));
Container c=j1.getContentPane();
JTextArea tf=new JTextArea(s);
JLabel ja=new JLabel(s2="第一次尝试,OK!");
char p,a;
JButton b1=new JButton(new ImageIcon(getClass().getResource("1.jpg")));
JButton b2=new JButton(new ImageIcon(getClass().getResource("2.jpg")));
JButton b3=new JButton(new ImageIcon(getClass().getResource("3.jpg")));
JButton b4=new JButton(new ImageIcon(getClass().getResource("4.jpg")));
JButton b5=new JButton(new ImageIcon(getClass().getResource("5.jpg")));
JButton b6=new JButton(new ImageIcon(getClass().getResource("6.jpg")));
JButton b7=new JButton(new ImageIcon(getClass().getResource("7.jpg")));
JButton b8=new JButton(new ImageIcon(getClass().getResource("8.jpg")));
JButton b9=new JButton(new ImageIcon(getClass().getResource("9.jpg")));
JButton ba=new JButton(new ImageIcon(getClass().getResource("10.jpg")));
JButton bb=new JButton(new ImageIcon(getClass().getResource("12.jpg")));
JButton bc=new JButton(new ImageIcon(getClass().getResource("13.jpg")));
JButton bd=new JButton(new ImageIcon(getClass().getResource("11.jpg")));
JButton bf=new JButton(new ImageIcon(getClass().getResource("15.jpg")));
JButton bg=new JButton(new ImageIcon(getClass().getResource("16.jpg")));
JButton bh=new JButton(new ImageIcon(getClass().getResource("14.jpg")));
Toolkit kit=Toolkit.getDefaultToolkit();

public void we(){
Image im=kit.createImage(getClass().getResource("图标.gif"));
j1.setIconImage(im);
tf.setLineWrap(true);
tf.setBackground(Color.BLACK);
c.setLayout(new BorderLayout());
c.setBackground(Color.WHITE);
j1.setVisible(true);
j1.setBounds(514,78,545,370);
JLabel l=new JLabel(ic,JLabel.CENTER);
l.setFont(new Font("计算器",Font.BOLD,40));
l.setSize(400,5);
j1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
c.add(l,BorderLayout.NORTH);
c.setBackground(new Color(226,235,244));
tf.setFont(new Font(s+p+s1,Font.BOLD,23));
tf.setForeground(Color.orange) ;
tf.setSize(40,150);
ja.setForeground(Color.YELLOW);
ja.setFont(new Font(s2,Font.BOLD,15));
p2.setLayout(new GridLayout(3,1,0,0));
p2.setSize(350,300);
p2.setVisible(true);
p2.add(tf);
p2.add(je);
p2.add(ja);
p2.setBackground(Color.BLACK);
p1.setBorder(BorderFactory.createLineBorder(new Color(197,215,235),5 ));
p2.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 5));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(ba);
p1.add(bb);
p1.add(bc);
p1.add(bd);
p1.add(bf);
p1.add(bg);
p1.add(bh);
b1.setBorderPainted(false);
b2.setBorderPainted(false);
b3.setBorderPainted(false);
b4.setBorderPainted(false);
b5.setBorderPainted(false);
b6.setBorderPainted(false);
b7.setBorderPainted(false);
b8.setBorderPainted(false);
b9.setBorderPainted(false);
ba.setBorderPainted(false);
bb.setBorderPainted(false);
bc.setBorderPainted(false);
bd.setBorderPainted(false);
bh.setBorderPainted(false);
bf.setBorderPainted(false);
bg.setBorderPainted(false);
b1.addActionListener(new action1());
b2.addActionListener(new action1());
b3.addActionListener(new action1());
b4.addActionListener(new action1());
b5.addActionListener(new action1());
b6.addActionListener(new action1());
b7.addActionListener(new action1());
b8.addActionListener(new action1());
b9.addActionListener(new action1());
ba.addActionListener(new action1());
bb.addActionListener(new action1());
bc.addActionListener(new action1());
bd.addActionListener(new action1());
bf.addActionListener(new action1());
bg.addActionListener(new action1());
p1.setLayout(new GridLayout(4,4,0,0));
p1.setSize(50,150);
p1.setVisible(true);
c.add(p1,BorderLayout.WEST);
c.add(p2,BorderLayout.EAST);
bh.addFocusListener(new FocusListener(){

public void focusLost(FocusEvent d){

ja.setText("请依次输入数值!");
}
public void focusGained(FocusEvent d){

bh.addActionListener(new action());
}
}) ;

}
class action1 implements ActionListener{
public void actionPerformed(ActionEvent e){

if(e.getSource()==b1){
if(p==0)
{
s=s+"1"; tf.setText(s);}
else

{
s1=s1+"1";tf.setText(s+p+s1);}
}
if(e.getSource()==b2){
if(p==0){
s=s+"2";
tf.setText(s);
}
else
{
s1=s1+"2";
tf.setText(s+p+s1);
}
}

if(e.getSource()==b3){
if(p==0)
{
s=s+"3";
tf.setText(s);
}
else
{
s1=s1+"3";tf.setText(s+p+s1);}}
if(e.getSource()==b4){
if(p==0)
{
s=s+"4";
tf.setText(s);
}
else
{
s1=s1+"4";
tf.setText(s+p+s1);
}
}

if(e.getSource()==b5){
if(p==0)
{
s=s+"5";tf.setText(s);}
else
{
s1=s1+"5";tf.setText(s+p+s1);}

}
if(e.getSource()==b6){
if(p==0)
{
s=s+"6";tf.setText(s);}
else
{
s1=s1+"6";tf.setText(s+p+s1);}

}
if(e.getSource()==b7){
if(p==0)
{
s=s+"7";tf.setText(s);}
else
{
s1=s1+"7";tf.setText(s+p+s1);}

}
if(e.getSource()==b8){
if(p==0)
{
s=s+"8";
tf.setText(s);}
else
{
s1=s1+"8";tf.setText(s+p+s1);}

}
if(e.getSource()==b9){

if(p==0)
{
s=s+"9";tf.setText(s);}
else
{
s1=s1+"9";tf.setText(s+p+s1);}

}
if(e.getSource()==ba){
if(p==0)
{
s=s+"0";tf.setText(s);}
else
{ s1=s1+"0";tf.setText(s+p+s1);}

}
if(e.getSource()==bb){
if(p>0)
{

re=Double.parseDouble(s);
rt=Double.parseDouble(s1);
int x=p;
switch(x){

case '+':n=rt+re;s=String.valueOf(n);tf.setText(s+"+");break;
case '-':n=re-rt;s=String.valueOf(n);tf.setText(s+"+");break;
case '*':n=rt*re;s=String.valueOf(n);tf.setText(s+"+");break;
case '/':n=re/rt;s=String.valueOf(n);tf.setText(s+"+");break;
}
p='+';
s1="";
}

else
{if(s.length()<1)
{ s="0";ja.setText("请正确输入第一个数!");}
p='+';
tf.setText(s+"+");}

}
if(e.getSource()==bc){
if(p>0)
{

re=Double.parseDouble(s);
rt=Double.parseDouble(s1);
int x=p;
switch(x){

case '+':n=rt+re;s=String.valueOf(n);tf.setText(s+"-");break;
case '-':n=re-rt;s=String.valueOf(n);tf.setText(s+"-");break;
case '*':n=rt*re;s=String.valueOf(n);tf.setText(s+"-");break;
case '/':n=re/rt;s=String.valueOf(n);tf.setText(s+"-");break;
}
s1="";
p='-';
}

else
{if(s.length()<1)

{s="0";ja.setText("请输入第一个数!");}
p='-';
tf.setText(s+"-");}

}
if(e.getSource()==bd){
if(p>0)
{
re=Double.parseDouble(s);
rt=Double.parseDouble(s1);
int x=p;
switch(x){

case '+':n=rt+re;s=String.valueOf(n);tf.setText(s+"*");break;
case '-':n=re-rt;s=String.valueOf(n);tf.setText(s+"*");break;
case '*':n=rt*re;s=String.valueOf(n);tf.setText(s+"*");break;
case '/':n=re/rt;s=String.valueOf(n);tf.setText(s+"*");break;
}
s1="";
p='*';
}

else
{ if(s.length()<1){
s="0";
ja.setText("请输入第一个数!");
}
p='*';
tf.setText(s+"*");
}
}
if(e.getSource()==bf){
if(p>0)
{

re=Double.parseDouble(s);
rt=Double.parseDouble(s1);
int x=p;
switch(x){

case '+':n=rt+re;s=String.valueOf(n);tf.setText(s+"/");break;
case '-':n=re-rt;s=String.valueOf(n);tf.setText(s+"/");break;
case '*':n=rt*re;s=String.valueOf(n);tf.setText(s+"/");break;
case '/':n=re/rt;s=String.valueOf(n);tf.setText(s+"/");break;
}
s1="";
p='/';
}

else{
if(s.length()<1){
s="0";
ja.setText("请正确输入第一个数!");}
p='/';
tf.setText(s+"/");}
}
if(e.getSource()==bg){
if(p<1)
s=s+".";
else
{s1=s1+".";
tf.setText(s+p+s1);}
}}
}
class action implements ActionListener{
public void actionPerformed(ActionEvent e){

if(s.length()==0){
s="0";
ja.setText("请重新输入第一个数!");}
if(s1.length()==0)
{s1="0";}
re=Double.parseDouble(s);
rt=Double.parseDouble(s1);
int x=p;
switch(x){
case '+':n=rt+re; tf.setText(s+p+s1+"="+n);break;
case '-':n=re-rt; tf.setText(s+p+s1+"="+n);break;
case '*':n=rt*re; tf.setText(s+p+s1+"="+n);break;
case '/':n=re/rt;if(rt==0){ tf.setText(s+p+s1+"="+"无效结果");}else tf.setText(s+p+s1+"="+n) ;break;
default:s2="您的输入有误!";}
p=0;s1=s="";
tf.setCaretPosition(0);
}}
}

public class Js{
public static void main(String args[]){
new Jst().we();

}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
kwpen
2009-04-14 · 超过25用户采纳过TA的回答
知道答主
回答量:75
采纳率:0%
帮助的人:78.3万
展开全部
我给个程序你参考参考,其实原理都基本上是一样的,在实现的方法上有所不同

import java.awt.Color;
import java.awt.GridLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;

public class CalculatorJFrame extends JFrame implements ActionListener
{
private JTextField text; //显示计算结果的文本行

private char op; //当前运算符
private float value; //当前计算结果值
private boolean first; //表示是否为数值的第1个数字

public CalculatorJFrame()
{
super("Calculator");
this.setSize(280,210);
this.setLocation(300,240);

this.display(); //显示图形用户界面
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);

op='+';
value=0.0f;
first=true;
}

private void display() //图形用户界面
{
text = new JTextField("0.",30);
text.setHorizontalAlignment(JTextField.RIGHT); //设置对齐方式,右对齐
text.setEditable(false); //只能显示,不允许编辑
this.add(text,"North");

JPanel panel = new JPanel(new GridLayout(5,4,5,5));
this.add(panel);
panel.add(new JButton("sqrt"));
panel.add(new JButton("+/-"));

JButton button_backspace = new JButton("Backspace");
panel.add(button_backspace);
button_backspace.addActionListener(this); //为按钮注册单击事件监听器

JButton button_clear = new JButton("C");
panel.add(button_clear);
button_clear.addActionListener(this);

JButton button_seven = new JButton("7");
panel.add(button_seven);
button_seven.addActionListener(this);

JButton button_eight = new JButton("8");
panel.add(button_eight);
button_eight.addActionListener(this);

JButton button_nine = new JButton("9");
panel.add(button_nine);
button_nine.addActionListener(this);

JButton button_division = new JButton("/");
panel.add(button_division);
button_division.addActionListener(this);

JButton button_four = new JButton("4");
panel.add(button_four);
button_four.addActionListener(this);

JButton button_five = new JButton("5");
panel.add(button_five);
button_five.addActionListener(this);

JButton button_six = new JButton("6");
panel.add(button_six);
button_six.addActionListener(this);

JButton button_multiplication = new JButton("*");
panel.add(button_multiplication);
button_multiplication.addActionListener(this);

JButton button_one = new JButton("1");
panel.add(button_one);
button_one.addActionListener(this);

JButton button_two = new JButton("2");
panel.add(button_two);
button_two.addActionListener(this);

JButton button_three = new JButton("3");
panel.add(button_three);
button_three.addActionListener(this);

JButton button_minus = new JButton("-");
panel.add(button_minus);
button_minus.addActionListener(this);

JButton button_zero = new JButton("0");
panel.add(button_zero);
button_zero.addActionListener(this);

JButton button_radixpoint = new JButton(".");
panel.add(button_radixpoint);
button_radixpoint.addActionListener(this);

JButton button_equalto = new JButton("=");
panel.add(button_equalto);
button_equalto.addActionListener(this);

JButton button_plus = new JButton("+");
panel.add(button_plus);
button_plus.addActionListener(this);
}

public void actionPerformed(ActionEvent e) //按钮单击的事件处理方法
{
String current = e.getActionCommand(); //获取按钮标签
if (current.equals("C")) //清除
{
text.setText("0."); //重新设置文本内容
value = 0.0f;
first = true;
return;
}

if (current.equals("Backspace")) //退格
{
String str=text.getText().substring(0,text.getText().length()-1);
text.setText(str);
if (str.equals(""))
{
text.setText("0.");
first = true;
}
return;
}

if (current.compareTo("0")>=0 && current.compareTo("9")<=0 //0~9数字按钮
|| current.equals("."))
{
if (first)
{
text.setText("");
first = false;
}
text.setText(text.getText()+current);
}
else
{
System.out.print(""+value+op+text.getText()+"=");
if (op=='+')
value += Float.parseFloat(text.getText()); //转换成浮点数后运算
if (op=='-')
value -= Float.parseFloat(text.getText());
if (op=='*')
value *= Float.parseFloat(text.getText());
if (op=='/')
value /= Float.parseFloat(text.getText());

System.out.println(""+value);
text.setText(""+value);
op = current.charAt(0); //字符串中取第1个字符
first = true;
}
}

public static void main(String arg[])
{
new CalculatorJFrame();
}
}

/*
程序设计说明如下。
1、也可以使用双精度浮点数,将字符串转换双精度浮点数的方法如下:
Double.parseDouble(text.getText())

2、switch语句的表达式只能是整数,不能是字符,例如,下列语句的逻辑含义正确,但运算结果不正确。
switch (op)
{
case '+': value += Float.parseFloat(text.getText()); //转换成浮点数后运算
case '-': value -= Float.parseFloat(text.getText());
case '*': value *= Float.parseFloat(text.getText());
case '/': value /= Float.parseFloat(text.getText());
}

3、错误处理未做
例如,连接单击当数点【.】或【=】按钮等。
*/
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
267025676
2009-04-14 · TA获得超过260个赞
知道答主
回答量:190
采纳率:0%
帮助的人:156万
展开全部
package test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;

public class Calculator
{

String str1="0"; //运算数1 初值一定为0 为了程序的安全
String str2="0"; //运算数2
String fh="+"; //运算符
String jg="";//结果

//状态开关 重要
int k1=1;//开关1 用于选择输入方向 将要写入str2或 str2
int k2=1;//开关2 符号键 次数 k2>1说明进行的是2+3-9+8 这样的多符号运算
int k3=1;//开关3 str1 是否可以被清0 ==1时可以 !=1时不能被清0
int k4=1;//开关4 str2 同上
int k5=1;//开关5 控制小数点可否被录入 ==1时可以 !=1 输入的小数点被丢掉
JButton jicunqi; //寄存器 记录 是否连续按下符号键
Vector vt=new Vector(20,10);

JFrame frame=new JFrame("sunshine---计算器");
JTextField jg_TextField=new JTextField(jg,20);//20列
JButton clear_Button=new JButton("清除");
JButton button0=new JButton("0");
JButton button1=new JButton("1");
JButton button2=new JButton("2");
JButton button3=new JButton("3");
JButton button4=new JButton("4");
JButton button5=new JButton("5");
JButton button6=new JButton("6");
JButton button7=new JButton("7");
JButton button8=new JButton("8");
JButton button9=new JButton("9");
JButton button_Dian=new JButton(".");
JButton button_jia=new JButton("+");
JButton button_jian=new JButton("-");
JButton button_cheng=new JButton("*");
JButton button_chu=new JButton("/");
JButton button_dy=new JButton("=");
////////////////////////////////////////////////////////////////////////
public static void main(String[] args)
{
Calculator calculator=new Calculator();
}

/////////////////////////////////////////////////////////////////////////
Calculator()
{
button0.setMnemonic(KeyEvent.VK_0);//等效键
//其它 等效键 略,

jg_TextField.setHorizontalAlignment(JTextField.RIGHT );//文本框 右对齐

JPanel pan=new JPanel();
pan.setLayout(new GridLayout(4,4,5,5));//四行四列 边距为5像素
pan.add(button7);
pan.add(button8);
pan.add(button9);
pan.add(button_chu);
pan.add(button4);
pan.add(button5);
pan.add(button6);
pan.add(button_cheng);
pan.add(button1);
pan.add(button2);
pan.add(button3);
pan.add(button_jian);
pan.add(button0);
pan.add(button_Dian);
pan.add(button_dy);
pan.add(button_jia);
pan.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));//pan对象的边距

JPanel pan2=new JPanel();
pan2.add(jg_TextField);

JPanel pan3=new JPanel(); //为什么要 多此一句呢? 因为我不会设置 按钮的大小
pan3.setLayout(new FlowLayout());
pan3.add(clear_Button);
//clear_Button.setSize(10,10);//设置清零按钮的大小 吗的 不好使 !!

frame.setLocation(300, 200); //主窗口 出现在位置
frame.setResizable(false); //不能调大小
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(pan2,BorderLayout.NORTH);
frame.getContentPane().add(pan,BorderLayout.CENTER);
frame.getContentPane().add(pan3,BorderLayout.SOUTH);

frame.pack();
frame.setVisible(true);

//以上是 控件 和 布局
//下面是事件处理 程 序

//--------------- 数 字 键 ----------------
class JianTing implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ss=((JButton)e.getSource()).getText();
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if (k1==1)
{
if(k3==1)
{
str1="";
k5=1;//还原开关k5状态
}
str1=str1+ss;
//k2=1;
k3=k3+1;
//System.out.println(str1);
jg_TextField.setText(str1);//显示

}
else if(k1==2)
{
if (k4==1)
{
str2="";
k5=1; //还原开关k5状态
}
str2=str2+ss;
//k2=2;
k4=k4+1;
///////////////测试////////////////
jg_TextField.setText(str2);
}

}
}

//--------符 号-----------
class JianTing_fh implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ss2=((JButton)e.getSource()).getText();
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);

if(k2==1)
{
k1=2;//开关 k1 为1时,向数1写 为2时,向数2写
k5=1;
fh=ss2;
k2=k2+1;//按符号键的次数
}
else
{
int a=vt.size();
JButton c=(JButton)vt.get(a-2);

if(!(c.getText().equals("+"))&&!(c.getText().equals("-"))&&!(c.getText().equals("*"))&&!(c.getText().equals("/")))
//if(!(vt.get(a-2).getText().equals("-"))||!(vt.get(a-2).getText().equals("+"))||!(vt.get(a-2).getText().equals("*"))||!(vt.get(a-2).getText().equals("/")))
{ yuns();
str1=jg;
k1=2;//开关 k1 为1时,向数1写 为2时,向数2写
k5=1;
k4=1;
fh=ss2;
} k2=k2+1;

}

}
}

//--------清除-------
class JianTing_clear implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
k5=1;
k2=1;
k1=1;
k3=1;
k4=1;
str1="0";
str2="0";
fh="";
jg="";
jg_TextField.setText(jg);
vt.clear();
}
}

//----------------等 于 ---------------------
class JianTing_dy implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
yuns();
k1=1; //还原开关k1状态
//str1=jg;
k2=1;
k3=1;//还原开关k3状态
k4=1; //还原开关k4状态

str1=jg; //为7+5=12 +5=17 这种计算做准备
}
}
//----------------小数点 ---------------------
class JianTing_xiaos implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if(k5==1)
{
String ss2=((JButton)e.getSource()).getText();
if (k1==1)
{
if(k3==1)
{
str1="";
k5=1; //还原开关k5状态
}
str1=str1+ss2;
//k2=1;
k3=k3+1;
//System.out.println(str1);
jg_TextField.setText(str1);//显示

}
else if(k1==2)
{
if (k4==1)
{
str2="";
k5=1; //还原开关k5状态
}
str2=str2+ss2;
//k2=2;
k4=k4+1;
///////////////测试////////////////
jg_TextField.setText(str2);
}
}

k5=k5+1; //
}
}

//注册 监听器
JianTing_dy jt_dy=new JianTing_dy();
JianTing jt= new JianTing();//临听数字键
JianTing_fh jt_fh= new JianTing_fh();//临 听符 号键
JianTing_clear jt_c=new JianTing_clear(); //清除键
JianTing_xiaos jt_xs=new JianTing_xiaos();// 小数点 键

button7.addActionListener(jt);
button8.addActionListener(jt);
button9.addActionListener(jt);
button_chu.addActionListener(jt_fh);
button4.addActionListener(jt);
button5.addActionListener(jt);
button6.addActionListener(jt);
button_cheng.addActionListener(jt_fh);
button1.addActionListener(jt);
button2.addActionListener(jt);
button3.addActionListener(jt);
button_jian.addActionListener(jt_fh);
button0.addActionListener(jt);
button_Dian.addActionListener(jt_xs);
button_dy.addActionListener(jt_dy);
button_jia.addActionListener(jt_fh);
clear_Button.addActionListener(jt_c);

//关闭事件处理程序
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

}

//---------------计 算------------------
public void yuns()
{
double a2;//运算数1
double b2;//运算数2
String c=fh;// 运算符
double jg2=0 ;//结果

if (c.equals(""))
{
//System.out.println("请输入运算符");
jg_TextField.setText("请输入运算符");

}
else
{

System.out.println("str1:"+str1);//调试时 使 用
System.out.println("str2:"+str2);//调试时 使 用
System.out.println("运算符:"+fh);//调试时 使 用
if (str1.equals(".")) //字符串 "." 转换成double型数据时 会出错 所以手工转
str1="0.0";
if (str2.equals("."))
str2="0.0";
a2=Double.valueOf(str1).doubleValue();
b2=Double.valueOf(str2).doubleValue();

System.out.println("double型的a2:"+a2); //调试时 使 用
System.out.println("double型的b2:"+b2); //调试时 使 用

if (c.equals("+"))
{
jg2=a2+b2;
}
if (c.equals("-"))
{
jg2=a2-b2;
}
if (c.equals("*"))
{
jg2=a2*b2;
}
if (c.equals("/"))
{
if(b2==0)
{
jg2=0;//0000000000000 by 0 cu!
}
else
{
jg2=a2/b2;
}

}

System.out.println("double型a2"+fh+"b2结果:"+jg2);

System.out.println();
jg=((new Double(jg2)).toString());

jg_TextField.setText(jg);
}
}
}

参考资料: 网上

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yawin
2009-04-14 · TA获得超过190个赞
知道小有建树答主
回答量:229
采纳率:0%
帮助的人:203万
展开全部
用java写计算器难就难在事件处理,解析你输入的字符挺麻烦的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式