求最简单计算器
哪位大妈给个只能加减乘除的计算器,男人简单就好!! 搜索更多相关的解决方案:
求最简单计算器代码
----------------解决方案--------------------------------------------------------
回复 楼主 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(this.compute()).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");
}
}
----------------解决方案--------------------------------------------------------
呵呵,楼上的仁兄很是强悍啊
----------------解决方案--------------------------------------------------------
那么长呀 给个3个JTextField 1个JComboBox(+ - * /) 1个JButton(=) 就OK了
----------------解决方案--------------------------------------------------------
曾经也写过,但是都忘记了
----------------解决方案--------------------------------------------------------
我写了一个,但编译时中间有个错误,我改了几次还是通过不了编译,我已标出。希望大家也帮我改改,谢了。
import java.awt.*;
import java.awt.event.*;
public class C3 extends Frame implements ActionListener
{
static C3 frm=new C3();
static TextField txf1=new TextField("");
static TextField txf2=new TextField("");
static TextField txf3=new TextField("");
static Button btn1=new Button("加");
static Button btn2=new Button("减");
static Button btn3=new Button("乘");
static Button btn4=new Button("除");
public static void mian(String args[])
{
btn1.addActionListener(frm);
btn2.addActionListener(frm);
btn3.addActionListener(frm);
btn4.addActionListener(frm);
frm.setSize(300,150);
frm.setLayout(null);
frm.setBackground(Color.yellow);
frm.setTitle("简易计算器");
txf1.setBounds(30,40,70,30);
txf2.setBounds(110,40,70,30);
txf3.setBounds(190,40,70,30);
btn1.setBounds(30,100,50,30);
btn2.setBounds(90,100,50,30);
btn3.setBounds(150,100,50,30);
btn4.setBounds(210,100,50,30);
frm.add(txf1);
frm.add(txf2);
frm.add(txf3);
frm.add(btn1);
frm.add(btn2);
frm.add(btn3);
frm.add(btn4);
frm.setVisible(true);
}
Double A1,A2,C1,C2,C3;
public void actionPerformed(ActionEvent e)
{
A1=((Double)valueOf(txf1.getText())).doubleValue(); //此处提示编译出错
A2=((Double)valueOf(txf2.getText())).doubleValue();
C1=A1+A2;
C2=A1-A2;
C3=A1*A2;
C4=A1/A2;
Button btn=(Button)e.getSource();
if(btn==btn1)
txf3.setText(String.valueOf(C1));
else if(btn==btn2)
txf3.setText(String.valueOf(C2));
else if(btn==btn3)
txf3.setText(String.valueOf(C3));
else if(btn==btn4)
txf3.setText(String.valueOf(C4));
}
}
----------------解决方案--------------------------------------------------------