JAVA Awt 实现计算器
一构成
1计算机界面
2计算所需的堆栈
3堆栈
4主函数
二计算机界面
界面如下图所示(比较丑哈哈,懒得调整了)
外边整个是构造一个Frame
Frame f1 =newFrame("计算器");
内部:TextField + TextFiled + Panel
Panel使用BorderLayout构造器,添加了4行4列的Button
为每一个Button 和TextFiled增加ActionListener ,点击Button,对应的数字会显示到第一行TextFied
点击“=”Button将输入的字符串传入计算函数,计算出的结果显示在TEXTFiled文本框
点击”CE”所有计算结果清零
点击“C”删除一个字符
Frame实现WindowAdapter中windowClosing函数,点击×可以关闭窗口
package Code_Computer;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ComputorFrame extends Frame {
Frame f1 = new Frame("计算器");
TextField num1, num2;
String input = null;
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23;
String result = null;
String n2 = null;
public void launch() {
Panel p = new Panel(new BorderLayout());
Monitor m = new Monitor();
f1.setLayout(new BorderLayout());
num1 = new TextField(30);
num2 = new TextField(30);
num1.addActionListener(m);
num2.addActionListener(m);
p.add(num1,BorderLayout.NORTH);
p.add(num2,BorderLayout.SOUTH);
f1.add(p, BorderLayout.NORTH);
Panel p2 = new Panel(new GridLayout(6, 4));
Button b0 = new Button("%");
b1 = new Button("ex");
b23 = new Button("sq");
b2 = new Button("bs");
b3 = new Button("C");
b4 = new Button("(");
b5 = new Button(")");
b6 = new Button("/");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b10 = new Button("*");
b11 = new Button("4");
b12 = new Button("5");
b13 = new Button("6");
b14 = new Button("-");
b15 = new Button("1");
b16 = new Button("2");
b17 = new Button("3");
b18 = new Button("+");
b19 = new Button("CE");
b20 = new Button("0");
b21 = new Button(".");
b22 = new Button("=");
b0.addActionListener(m);
b1.addActionListener(m);
b2.addActionListener(m);
b3.addActionListener(m);
b4.addActionListener(m);
b5.addActionListener(m);
b6.addActionListener(m);
b7.addActionListener(m);
b8.addActionListener(m);
b9.addActionListener(m);
b10.addActionListener(m);
b11.addActionListener(m);
b12.addActionListener(m);
b13.addActionListener(m);
b14.addActionListener(m);
b15.addActionListener(m);
b16.addActionListener(m);
b17.addActionListener(m);
b18.addActionListener(m);
b19.addActionListener(m);
b20.addActionListener(m);
b21.addActionListener(m);
b22.addActionListener(m);
b23.addActionListener(m);
p2.add(b0);
p2.add(b1);
p2.add(b23);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b10);
p2.add(b11);
p2.add(b12);
p2.add(b13);
p2.add(b14);
p2.add(b15);
p2.add(b16);
p2.add(b17);
p2.add(b18);
p2.add(b19);
p2.add(b20);
p2.add(b21);
p2.add(b22);
f1.add(p2, BorderLayout.SOUTH);
f1.pack();
f1.addWindowListener(new MyWindowMonitor());
f1.setVisible(true);
}
class MyWindowMonitor extends WindowAdapter {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
}
class Monitor implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(!e.getActionCommand().equals("=")){
if(e.getActionCommand().equals("C")){
input = input.subSequence(0, input.length() - 1).toString();
System.out.println("input-----CCCC" + input);
//num1.setText((input.subSequence(4, input.length() )).toString());
}else{
input += e.getActionCommand();
}
System.out.println("input-----" + input);
num1.setText((input.subSequence(4, input.length() )).toString());
System.out.println("num1-----" + (input.subSequence(4, input.length() )).toString());
}
if(e.getActionCommand().equals("CE")){
input = null;
num1.setText("0");
num2.setText("0");
}
if(e.getActionCommand().equals("=")){
result = (input.subSequence(4, input.length() )).toString();
System.out.println("result-----" + result);
Expression ex = new Expression();
String n = ex.calc(result).toString();
num2.setText(n);
}
}
}
}