赞
踩
@requires_authorization
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
TextField tf = new TextField(40); //显示文本框
StringBuffer s1 = new StringBuffer(); //记录运算数字,以及保留结果
StringBuffer s2 = new StringBuffer(); //记录运算数字,保留上一个输入的数字或运算结果
static String flag = new String(); //标记运算符号:+,-,*,/
boolean start = true; //标记运算开始或结束,保证一次运算之后,第二次进行运算时能同时清空显示界面,即s1为空
public void init(){
//主界面
JFrame f = new JFrame("计算器");
JPanel p1 = new JPanel();
p1.add(tf);
f.add(p1,BorderLayout.NORTH);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1,3));
//动作监听器
ActionListener listen =new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//输入数字0~9
if(arg0.getActionCommand()=="0"){
if(!s1.toString().equals(new String())){
if(!start){
//s1清零,保证可以重新输入数字
s1.delete(0, s1.length());
}
start = true;
tf.setText(s1.append("0").toString());
}
}
if(arg0.getActionCommand()=="1"){
if(!start){
s1.delete(0, s1.length());
}
start = true;
tf.setText(s1.append("1").toString());
}
if(arg0.getActionCommand()=="2"){
if(!start){
s1.delete(0, s1.length());
}
start = true;
tf.setText(s1.append("2").toString());
}
if(arg0.getActionCommand()=="3"){
if(!start){
s1.delete
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。