用java实现一个简单的计算器。

importjava.awt.*;importjava.awt.event.*;importjava.awt.event.ActionEvent;importjava.a... import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends Frame implements WindowListener,ActionListener{

/**定义变量
*/
TextField Text1=new TextField("");
TextField Text2=new TextField("");
TextField Text3=new TextField("");
Button Button7=new Button("7");
Button Button8=new Button("8");
Button Button9=new Button("9");
Button addButton=new Button("+");
Button Button4=new Button("4");
Button Button5=new Button("5");
Button Button6=new Button("6");
Button subButton=new Button("-");
Button Button1=new Button("1");
Button Button2=new Button("2");
Button Button3=new Button("3");
Button mulButton=new Button("x");
Button Button0=new Button("0");
Button equalButton=new Button("=");
Button divButton=new Button("/");

/**实现一个窗口
*/
public static void main(String arg[]){
Calculator myFrame=new Calculator();
myFrame.setVisible(true);
myFrame.addWindowListener(new Calculator());
}

/**窗口关闭 打开 最小化 最大化的功能
*/
public void windowClosing(WindowEvent e) {
System.exit(1);
}
public void windowOpened(WindowEvent e) {
};
public void windowIconified(WindowEvent e) {
};
public void windowDeiconified(WindowEvent e) {
};
public void windowClosed(WindowEvent e) {
};
public void windowActivated(WindowEvent e) {
};
public void windowDeactivated(WindowEvent e) {
}

/**构造Calculator函数
*/
public Calculator(){

/*
分成两大区域
*/
setSize(400,500);
Panel panelText=new Panel();
Panel panelButton=new Panel();
setLayout(new GridLayout(2,1));
add(panelText);
add(panelButton);

/**
添加显示框
*/
panelText.setLayout(new GridLayout(3,1));
panelText.add(Text1);
panelText.add(Text2);
panelText.add(Text3);

/**
添加按钮
*/
panelButton.setLayout(new GridLayout(4,4));
panelButton.add(Button7);
panelButton.add(Button8);
panelButton.add(Button9);
panelButton.add(addButton);
panelButton.add(Button4);
panelButton.add(Button5);
panelButton.add(Button6);
panelButton.add(subButton);
panelButton.add(Button1);
panelButton.add(Button2);
panelButton.add(Button3);
panelButton.add(mulButton);
panelButton.add(Button0);
panelButton.add(equalButton);
panelButton.add(divButton);

/**按钮
*/
}
public void actionPerformed(ActionEvent e){
Button0.addActionListener(this);
if(e.getActionCommand().equals("Button1"))
Text1.setText(e.getActionCommand());
}
}
为什么按Button0,没有反应?
展开
 我来答
pieryon
2015-06-17 · 知道合伙人数码行家
pieryon
知道合伙人数码行家
采纳数:14411 获赞数:166862
获取软件设计师高级职称 万达金融最佳创新奖

向TA提问 私信TA
展开全部

/*

 * @(#)JCalculator.java 1.00 06/17/2015

 */

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

/**

 * A simple calculator program.

 * <p>I saw this program in a QQ group, and help a friend correct it.</p>

 *

 * @author Singyuen Yip

 * @version 1.00 12/29/2009

 * @see JFrame

 * @see ActionListener

 */

public class JCalculator extends JFrame implements ActionListener {

    /**

     * Serial Version UID

     */

    private static final long serialVersionUID = -169068472193786457L;

    /**

     * This class help close the Window.

     * @author Singyuen Yip

     *

     */

    private class WindowCloser extends WindowAdapter {

       public void windowClosing(WindowEvent we) {

           System.exit(0);

       }

    }

 

    int i;

    // Strings for Digit & Operator buttons.

    private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*","1",

           "2", "3", "-", ".", "0", "=", "+" };

    // Build buttons.

    JButton[] buttons = new JButton[str.length];

    // For cancel or reset.

    JButton reset = new JButton("CE");

    // Build the text field to show the result.

    JTextField display = new JTextField("0");

   

    /**

     * Constructor without parameters.

     */

    public JCalculator() {

       super("Calculator");

       // Add a panel.

       JPanel panel1 = new JPanel(new GridLayout(4, 4));

       // panel1.setLayout(new GridLayout(4,4));

       for (i = 0; i < str.length; i++) {

           buttons[i] = new JButton(str[i]);

           panel1.add(buttons[i]);

       }

       JPanel panel2 = new JPanel(new BorderLayout());

       // panel2.setLayout(new BorderLayout());

       panel2.add("Center", display);

       panel2.add("East", reset);

       // JPanel panel3 = new Panel();

       getContentPane().setLayout(new BorderLayout());

       getContentPane().add("North", panel2);

       getContentPane().add("Center", panel1);

       // Add action listener for each digit & operator button.

       for (i = 0; i < str.length; i++)

           buttons[i].addActionListener(this);

       // Add listener for "reset" button.

       reset.addActionListener(this);

       // Add listener for "display" button.

       display.addActionListener(this);

       // The "close" button "X".

       addWindowListener(new WindowCloser());

       // Initialize the window size.

       setSize(800, 800);

       // Show the window.

       // show(); Using show() while JDK version is below 1.5.

       setVisible(true);

       // Fit the certain size.

       pack();

    }  

   

    public void actionPerformed(ActionEvent e) {

       Object target = e.getSource();

       String label = e.getActionCommand();

       if (target == reset)

           handleReset();

       else if ("0123456789.".indexOf(label) > 0)

           handleNumber(label);

       else

           handleOperator(label);

    }

    // Is the first digit pressed?

    boolean isFirstDigit = true;

    /**

     * Number handling.

     * @param key the key of the button.

     */

    public void handleNumber(String key) {

       if (isFirstDigit)

           display.setText(key);

       else if ((key.equals(".")) && (display.getText().indexOf(".") < 0))

           display.setText(display.getText() + ".");

       else if (!key.equals("."))

           display.setText(display.getText() + key);

       isFirstDigit = false;

    }

   

    /**

     * Reset the calculator.

     */

    public void handleReset() {

       display.setText("0");

       isFirstDigit = true;

       operator = "=";

    }

 

    double number = 0.0;

    String operator = "=";

   

    /**

     * Handling the operation.

     * @param key pressed operator's key.

     */

    public void handleOperator(String key) {

       if (operator.equals("+"))

           number += Double.valueOf(display.getText());

       else if (operator.equals("-"))

           number -= Double.valueOf(display.getText());

       else if (operator.equals("*"))

           number *= Double.valueOf(display.getText());

       else if (operator.equals("/"))

           number /= Double.valueOf(display.getText());

       else if (operator.equals("="))

           number = Double.valueOf(display.getText());

       display.setText(String.valueOf(number));

       operator = key;

       isFirstDigit = true;

    }

   

    public static void main(String[] args) {

       new JCalculator();

    }

}


运行界面:

武汉小朱
2010-03-29 · TA获得超过2762个赞
知道小有建树答主
回答量:1212
采纳率:50%
帮助的人:880万
展开全部
说实在的,你写的这个计算器不怎么样。特别是布局。
我给你一个很简单的吧。你自己学习一下。。

import java.awt.*;
import java.awt.event.*;
/**
*
* @author zzj
*
*/
public class Jisuanqi extends WindowAdapter {
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
TextField txt;
private Button[] b = new Button[17];
private String ss[] = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2",
"3", "*", "清空", "0", "=", "/", "关闭" };
static double a;
static String s, str;//定义变量 创建对像

public static void main(String args[]) {
(new Jisuanqi()).frame();
}

public void frame() {
Frame fm = new Frame("简单计算器");
for (int i = 0; i <= 16; i++) {
b[i] = new Button(ss[i]);
}
for (int i = 0; i <= 15; i++) {
p2.add(b[i]);
} //创建按钮 并添加到P2
b[16].setBackground(Color.yellow);
txt = new TextField(15);
txt.setEditable(false);
for (int i = 0; i <= 16; i++) {
b[i].addActionListener(new buttonlistener());//添加监听器
}
b[16].addActionListener(new close());
fm.addWindowListener(this);
fm.setBackground(Color.red);
p1.setLayout(new BorderLayout());
p1.add(txt, "North");
p2.setLayout(new GridLayout(4, 4));
p3.setLayout(new BorderLayout());
p3.add(b[16]);
fm.add(p1, "North");
fm.add(p2, "Center");
fm.add(p3, "South");
fm.pack();
fm.setVisible(true);//都是些窗中设置 添加相关组件和监听器
}

public void windowClosing(WindowEvent e) {
System.exit(0);//退出系统
}

class buttonlistener implements ActionListener {//编写监听器事件 通过按键得出给果
public void actionPerformed(ActionEvent e) {
Button btn = (Button) e.getSource();
if (btn.getLabel() == "=") {
jisuan();
str = String.valueOf(a);
txt.setText(str);
s = "";
} else if (btn.getLabel() == "+") {
jisuan();
txt.setText("");
s = "+";
} else if (btn.getLabel() == "-") {
jisuan();
txt.setText("");
s = "-";
} else if (btn.getLabel() == "/") {
jisuan();
txt.setText("");
s = "/";

} else if (btn.getLabel() == "*") {
jisuan();
txt.setText("");
s = "*";
} else {
txt.setText(txt.getText() + btn.getLabel());

if (btn.getLabel() == "清空")
txt.setText("");
}
}

public void jisuan() {//编写具体计算方法
if (s == "+")
a += Double.parseDouble(txt.getText());
else if (s == "-")
a -= Double.parseDouble(txt.getText());
else if (s == "*")
a *= Double.parseDouble(txt.getText());
else if (s == "/")
a /= Double.parseDouble(txt.getText());
else
a = Double.parseDouble(txt.getText());
}
}
}

class close implements ActionListener {//退出
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
天天看资料小站
2010-03-29 · TA获得超过205个赞
知道小有建树答主
回答量:379
采纳率:0%
帮助的人:249万
展开全部
- -|||……虽然我也有计算器的代码,不过我还是给你指正下你的问题出在哪里吧。很无语……
public void actionPerformed(ActionEvent e){
Button0.addActionListener(this);
if(e.getActionCommand().equals("Button1"))
Text1.setText(e.getActionCommand());
}
你应该把这里面的Button0.addActionListener(this);添加到Calculator类函数中.因为你是在Calculator类中触发了按钮事件,然后在actionPerformed(ActionEvent e)函数中处理它。你把它放到actionPerformed(ActionEvent e)函数中,又怎么能触发事件呢……
你修改看看吧,这样应该就可以用了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
ullllu
2010-03-31 · 超过61用户采纳过TA的回答
知道小有建树答主
回答量:132
采纳率:0%
帮助的人:122万
展开全部
将Button0.addActionListener(this);移到Calculator()函数,并将按键处理代码修改如下即可正常运行

public void actionPerformed(ActionEvent e){

if(e.getSource()== Button0)
Text1.setText(Button0.getLabel());
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
机器智能技术
2010-04-05 · TA获得超过166个赞
知道答主
回答量:79
采纳率:0%
帮助的人:23.6万
展开全部
Button0.addActionListener(this);
if(e.getActionCommand().equals("Button1"))
Text1.setText(e.getActionCommand());
}

注意你equals的对象不是Button0!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式