用java实现一个简单的计算器。
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,没有反应? 展开
/*
* @(#)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();
}
}
运行界面:
我给你一个很简单的吧。你自己学习一下。。
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);
}
}
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)函数中,又怎么能触发事件呢……
你修改看看吧,这样应该就可以用了
public void actionPerformed(ActionEvent e){
if(e.getSource()== Button0)
Text1.setText(Button0.getLabel());
}
}
if(e.getActionCommand().equals("Button1"))
Text1.setText(e.getActionCommand());
}
注意你equals的对象不是Button0!