当前位置: 代码迷 >> Java相关 >> 计算器问题
  详细解决方案

计算器问题

热度:231   发布时间:2006-11-20 22:42:29.0
计算器问题

帮我看看怎么改啊
package lesson4;

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

public class Calculator {
private JFrame frame;

private Container contentPane;

private JPanel centerPanel;

private JTextField resultTxt;

private DigitalButton[] digBtn = new DigitalButton[10];

private OperatorButton[] operBtn = new OperatorButton[6];

private static double a=0;

private static double b=0;

private boolean tab=true;
public Calculator() {
// 1 choose a container
frame = new JFrame("Calculator");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(250,300);
contentPane = frame.getContentPane();
initGUI();
}

private void initGUI() {
// 2 choose a layout manager
contentPane.setLayout(new BorderLayout());

// 3 add component
resultTxt = new JTextField(80);
contentPane.add(resultTxt, BorderLayout.NORTH);

// 3.1 choose a container
centerPanel = new JPanel();

// 3.2 choose a layout manager;
centerPanel.setLayout(new GridLayout(4, 4));

resultTxt.setText("0.0");
// 3.3 add component
for (int i = 0; i <= 9; i++) {
digBtn[i] = new DigitalButton(i);

// 3.4 add eventHandler
digBtn[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DigitalButton db = (DigitalButton) e.getSource();
double d1 = db.getValue();
if(tab==true)
{a=a*10+d1;resultTxt.setText(Double.toString(a));
System.out.println("a:"+a);
}
else
{b=b*10+d1;resultTxt.setText(Double.toString(b));
System.out.println("b:"+b);
}
}
});
centerPanel.add(digBtn[i]);
}

operBtn[0] = new OperatorButton("+");
operBtn[1] = new OperatorButton("-");
operBtn[2] = new OperatorButton("*");
operBtn[3] = new OperatorButton("/");
operBtn[4] = new OperatorButton("=");
operBtn[5] = new OperatorButton("C");

for (int i = 0; i <= 5; i++) {
// 3.4 add event Handler
operBtn[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OperatorButton db = (OperatorButton) e.getSource();
String c=db.getOperator();
if(c.equals("C")){
a=0;b=0;
resultTxt.setText("0.0");
tab=true;
}
if(c.equals("+"))
{
tab=false;
a+=b; //怎么让b赋值后再来加啊!
}
else if(c.equals("-"))
{
tab=false;
a-=b;

}
else if(c.equals("/"))
{
tab=false;
a/=b;

}
else if(c.equals("*"))
{
tab=false;
a*=b;

}
else
{
tab=true;
System.out.println("=:"+a);
resultTxt.setText(Double.toString(a));
a=0;b=0;
}
}
});
centerPanel.add(operBtn[i]);
}
contentPane.add(centerPanel, BorderLayout.CENTER);
}

public void go() {
frame.setVisible(true);
}

public static void main(String[] args) {
(new Calculator()).go();
}
}

class DigitalButton extends JButton {
private double value;

public double getValue() {
return value;
}

public DigitalButton(int value) {
super(value + "");
this.value = value;
}
}

class OperatorButton extends JButton {
private String operator;

public String getOperator() {
return operator;
}

public OperatorButton(String operator) {
super(operator);
this.operator = operator;
}
}

搜索更多相关的解决方案: 计算器  

----------------解决方案--------------------------------------------------------
你都无说出问题所在,叫人家点改?
----------------解决方案--------------------------------------------------------

我本人不太赞成这种帖代码让别人出来改的模式


----------------解决方案--------------------------------------------------------
我顶楼上的。。。。而且。。。。即使要改,楼主也得说说改什么,有什么问题,希望改成什么样子吧~虾米都不说,我给你改成个文本编辑器你能愿意吗~
----------------解决方案--------------------------------------------------------

哈哈 可以啊 文本编辑器 也不错啊


----------------解决方案--------------------------------------------------------
楼主无耻啊
----------------解决方案--------------------------------------------------------

本想和你共享一下我自己的代码的,我的实现了多重运算了.哎楼主既然这样,那就算拉


----------------解决方案--------------------------------------------------------
  相关解决方案