//摸版没搞,大概只有本人能读!!
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;
public class test extends JFrame{
static int count=0,all=0,but;
static String fuhao,output="";
private JLabel promptLabel;
protected static JTextField inputField;
private JTextArea outputArea;
private JButton j0,j1,j2,j3,j4,j5,j6,j7,j8,j9,x1,x2,y1,y2,s1,s2;
static int cont[]=new int[9];
static int paixu[]=new int[50];
public test(){
super("ceshi StringTokenizer");
Container container=getContentPane();
container.setLayout(new FlowLayout());
promptLabel=new JLabel("计算器");
container.add(promptLabel);
inputField=new JTextField(20);
inputField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
StringTokenizer tokens=new StringTokenizer(event.getActionCommand());
outputArea.setText("Number of elements"+tokens.countTokens()+"\nThe tokens are:\n");
while(tokens.hasMoreTokens())
outputArea.append(tokens.nextToken()+"\n");
}
});
container.add(inputField);
j1=new JButton("1");
j2=new JButton("2");
j3=new JButton("3");
x1=new JButton("*");
x2=new JButton("/");
j4=new JButton("4");
j5=new JButton("5");
j6=new JButton("6");
y1=new JButton("+");
y2=new JButton("-");
j7=new JButton("7");
j8=new JButton("8");
j9=new JButton("9");
s1=new JButton("=");
j0=new JButton("0");
//1--------------
j1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
count++;
paixu(1);
output+=paixu[count];
inputField.setText(output);
}
});
//2--------------
j2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
count++;
paixu(2);
output+=paixu[count];
inputField.setText(output);
}
}
);
//3--------------
j3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
count++;
paixu(3);
output+=paixu[count];
inputField.setText(output);
}
}
);
//4-----------
j4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
count++;
paixu(4);
output+=paixu[count];
inputField.setText(output);
}
}
);
//5--------------
j5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
count++;
paixu(5);
output+=paixu[count];
inputField.setText(output);
}
}
);
//6---------------------
j6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
count++;
paixu(6);
output+=paixu[count];
inputField.setText(output);
}
}
);
//7----------------------
j7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
count++;
paixu(7);
output+=paixu[count];
inputField.setText(output);
}
}
);
//8------------
j8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
count++;
paixu(8);
output+=paixu[count];
inputField.setText(output);
}
}
);
//9-------------------
j9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
count++;
paixu(9);
output+=paixu[count];
inputField.setText(output);
}
}
);
//0-----------------------
j0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
count++;
paixu(0);
output+=paixu[count];
inputField.setText(output);
}
}
);
//+----------- but第二个数组的记录
y1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
fuhao="+";
cont[but]=Integer.parseInt(output.trim());//去掉空格,并存进CONT第一个值里面
count=0;//输入数堆盏清零
but++;//标识第几个数输入
output="";//output清空
}
});
//- ---------------
y2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
fuhao="-";
cont[but]=Integer.parseInt(output.trim());//去掉空格
count=0;
but++;
output="";
}
});
// *---------------
x1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
fuhao="*";
cont[but]=Integer.parseInt(output.trim());//去掉空格
count=0;
but++;
output="";
}
});
// / ---------------
x2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
fuhao="/";
cont[but]=Integer.parseInt(output.trim());//去掉空格
count=0;
but++;
output="";
}
});
//= --------------------
s1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
cont[but]=Integer.parseInt(output.trim());//but控制的第二个输入数
if(fuhao=="+")//+
{ cont[1]=cont[0]+cont[1];
String All="";
All+=cont[1];
inputField.setText(All);
output=All; //第N次运算保留
}
else if(fuhao=="-")//-
{ cont[1]=cont[0]-cont[1];
String All="";
All+=cont[1];
inputField.setText(All);
output=All;
}
else if(fuhao=="*")//*
{ cont[1]=cont[0]*cont[1];
String All="";
All+=cont[1];
inputField.setText(All);
output=All;
}
else if(fuhao=="/")// /
{ cont[1]=cont[0]/cont[1];
String All="";
All+=cont[1];
inputField.setText(All);
output=All;
}
//清零
count=0;
but-=1;
// output="";
}
});
container.add(j9);
container.add(j8);
container.add(j7);
container.add(x1);
container.add(x2);
container.add(j6);
container.add(j5);
container.add(j4);
container.add(y1);
container.add(y2);
container.add(j3);
container.add(j2);
container.add(j1);
container.add(j0);
container.add(s1);
math(); //sin等涵数的类
outputArea=new JTextArea(10,18);
outputArea.setEditable(false);
container.add(new JScrollPane(outputArea));
setSize(275,500);
setVisible(true);
}
public static void main(String args[]){
test application=new test();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//堆栈-------------------------
private void paixu(int button){
paixu[count]=button;
}
//Math涵数按钮引入
private void math(){
//加入涵数按钮
Mathhanshu hanshu=new Mathhanshu();
Container container=getContentPane();
container.add(hanshu.sin1);
container.add(hanshu.cos1);
container.add(hanshu.tan1);
container.add(hanshu.tozero);
}
}
//Math涵数按钮
class Mathhanshu {
protected JButton sin1,cos1,tan1,tozero;
public Mathhanshu(){
sin1=new JButton("sin");
cos1=new JButton("cos");
tan1=new JButton("tan");
tozero=new JButton("清零");
ActionListener listener= new TT();
sin1.addActionListener(listener);
cos1.addActionListener(listener);
tan1.addActionListener(listener);
tozero.addActionListener(listener);
}
class TT implements ActionListener{
public void actionPerformed(ActionEvent event){
if(sin1==event.getSource())
{ test.cont[test.but]=Integer.parseInt(test.output.trim()); //cont为最初的值
test.output=""+Math.sin(test.cont[test.but]);
test.inputField.setText(test.output);
}
if(cos1==event.getSource())
{ test.cont[test.but]=Integer.parseInt(test.output.trim());
test.output=""+Math.cos(test.cont[test.but]);
test.inputField.setText(test.output);
}
if(tan1==event.getSource())
{ test.cont[test.but]=Integer.parseInt(test.output.trim());
test.output=""+Math.tan(test.cont[test.but]);
test.inputField.setText(test.output);
}
if(tozero==event.getSource())//未完成
{
test.inputField.setText("");
test.but=0;
test.count=0;
test.output="";
}
}
}
}
评论