用Java设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算

2022-03-16 教育 109阅读
下午发
五分钟搞定,先给你发个简单的,照例子自己去做你想要的计算器
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;
}
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com