下午发
五分钟搞定,先给你发个简单的,照例子自己去做你想要的计算器
packageaaa;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassYunSuanimplementsActionListener,ItemListener
{
publicstaticJFramejf;
publicstaticContainerc;
publicstaticJTextFieldjtf1,jtf2,jtf3;
publicstaticJButtonjb1;
publicstaticChoicec2;
publicstaticStringfuhao="";
publicYunSuan()
{
jf=newJFrame("加减乘除运算");
c=jf.getContentPane();
c.setLayout(newFlowLayout());
jtf1=newJTextField(10);
jtf2=newJTextField(10);
jtf3=newJTextField(10);
c2=newChoice();
jb1=newJButton("=");
}
publicvoidJiemian()
{
c.add(jtf1);
c2.addItemListener(this);
c2.add("");
c2.add("+");
c2.add("-");
c2.add("*");
c2.add("/");
c.add(c2);
c.add(jtf2);
jb1.addActionListener(this);
c.add(jb1);
c.add(jtf3);
jf.setLocation(200,100);
jf.setVisible(true);
jf.pack();
}
publicstaticvoidmain(Stringargs[])
{
YunSuanys=newYunSuan();
ys.Jiemian();
}
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
doubles1=Integer.parseInt(jtf1.getText());
doubles2=Integer.parseInt(jtf2.getText());
doubleresult=0;
if(fuhao.equals("+"))
{
result=s1+s2;
Stringresult2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("-"))
{
result=s1-s2;
Stringresult2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("*"))
{
result=s1*s2;
Stringresult2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("/"))
{
result=s1/s2;
Stringresult2=String.valueOf(result);
jtf3.setText(result2);
}
}
publicvoiditemStateChanged(ItemEventie)
{
if(ie.getSource()==c2)
{
Stringstr1=c2.getSelectedItem();
fanhui(str1);
}
}
publicStringfanhui(Stringstr2)
{
returnfuhao=str2;
}
}