| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 682 人关注过本帖
标题:计算器问题
取消只看楼主 加入收藏
心动音符
Rank: 1
等 级:禁止访问
威 望:1
帖 子:832
专家分:0
注 册:2005-9-15
收藏
 问题点数:0 回复次数:1 
计算器问题

帮我看看怎么改啊
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;
}
}

搜索更多相关主题的帖子: 计算器 
2006-11-20 22:42
心动音符
Rank: 1
等 级:禁止访问
威 望:1
帖 子:832
专家分:0
注 册:2005-9-15
收藏
得分:0 

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


2006-11-21 21:00
快速回复:计算器问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.060059 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved