求最简单计算器
哪位大妈给个只能加减乘除的计算器,男人简单就好!!
回复 楼主 liuhuobi
随便写了一个 ps:有错误,我已没心思修复了,你自己按这样的思想去改吧
package pcenshao.samplecalculator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainWindow extends JFrame implements ActionListener{
private JPanel mainPanel;
private JButton one;
private JButton two;
private JButton three;
private JButton four;
private JButton five;
private JButton six;
private JButton seven;
private JButton eight;
private JButton nine;
private JButton zero;
private JButton plus;
private JButton minus;
private JButton multiply;
private JButton divide;
private JButton c;
private JButton result;
private JTextField text;
private double num1=0;
private double num2=0;
private double rst=0;
private Object[] o;
private int index=-1;
private boolean haveComputer=false;
public MainWindow(String title){
super(title);
this.setVisible(true);
this.setResizable(false);
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension size=tk.getScreenSize();
int w=(int)size.getWidth();
int h=(int)size.getHeight();
this.setBounds(w/3,h/4,260,300);
this.text=new JTextField();
text.setSize(256,40);
text.setHorizontalAlignment(JTextField.RIGHT);
this.getContentPane().add(this.text);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.intiButtons();
o=new Object[10];
for(int i=0;i<10;i++){
o[i]=null;
}
}
private void intiButtons(){
this.mainPanel=new JPanel();
mainPanel.setSize(260,220);
this.one=new JButton("1");
this.two=new JButton("2");
this.three=new JButton("3");
this.four=new JButton("4");
this.five=new JButton("5");
this.six=new JButton("6");
this.seven=new JButton("7");
this.eight=new JButton("8");
this.nine=new JButton("9");
this.zero=new JButton("0");
this.plus=new JButton("+");
this.minus=new JButton("-");
this.multiply=new JButton("*");
this.divide=new JButton("/");
this.c=new JButton("C");
this.result=new JButton("=");
GridLayout layout=new GridLayout(4,5);
mainPanel.setLayout(layout);
mainPanel.add(this.plus);
mainPanel.add(this.minus);
mainPanel.add(this.multiply);
mainPanel.add(this.divide);
mainPanel.add(this.c);
mainPanel.add(this.one);
mainPanel.add(this.two);
mainPanel.add(this.three);
mainPanel.add(this.four);
mainPanel.add(this.five);
mainPanel.add(this.six);
mainPanel.add(this.seven);
mainPanel.add(this.eight);
mainPanel.add(this.nine);
mainPanel.add(this.zero);
mainPanel.add(this.result);
this.getContentPane().add(mainPanel);
mainPanel.setLocation(0,48);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
plus.addActionListener(this);
minus.addActionListener(this);
this.divide.addActionListener(this);
this.multiply.addActionListener(this);
c.addActionListener(this);
result.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
JButton tmp=(JButton)e.getSource();
if(!(tmp.getText().equals("=")||tmp.getText().equals("C")||tmp.getText().equals("+")
||tmp.getText().equals("-")||tmp.getText().equals("*")||tmp.getText().equals("/")))
{
String str="";
if(text.getText().equals("")){
str=tmp.getText();
text.setText(str);
}else{
str=text.getText()+tmp.getText();
text.setText(str);
}
}else if(tmp.getText().equals("=")){
double tmp1=Double.valueOf(text.getText());
this.inQ(new Double(tmp1));
this.inQ(tmp.getText());
text.setText(new Double(()).toString());
}else{
text.setText("");
this.index=-1;
}
}
public void inQ(Object o){ //模拟入队
this.o[this.index+1]=o;
this.index++;
}
public Object outQ(){//模拟出队
Object d;
d=this.o[this.index];
this.index--;
return d;
}
public double compute(){
if(this.index==-1)return 0;
double result=0;
String m=null;
result=((Double)this.outQ()).doubleValue();
for(int i=0;i<this.index-1;i++){
m=(String)this.outQ();
char[] ch=m.toCharArray();
char c=ch[0];
switch(c){
case '+':result+=((Double)this.outQ()).doubleValue();
case '-':result-=((Double)this.outQ()).doubleValue();
case '*':result*=((Double)this.outQ()).doubleValue();
case '/':result/=((Double)this.outQ()).doubleValue();
}
}
return result;
}
public static void main(String args[]){
new MainWindow("SampleCalculator");
}
}