JAVA 用JAVA设计程序,实现一个计算器,并能实现计算

头像
30岁男人的世界   2021-09-18   4752浏览
已有130条回答
头像
beyond45678
2022-08-05

package whf;
import java.awt.*;
import java.awt.event.*;
public class Jisuan extends Frame
implements ActionListener
{
TextField tf=new TextField();
Panel p=new Panel();
Button a0=new Button("0");
Button a1=new Button("1");
Button a2=new Button("2");
Button a3=new Button("3");
Button a4=new Button("4");
Button a5=new Button("5");
Button a6=new Button("6");
Button a7=new Button("7");
Button a8=new Button("8");
Button a9=new Button("9");
Button b1=new Button("+");
Button b2=new Button("-");
Button b3=new Button("*");
Button b4=new Button("CE");
Button b5=new Button("=");
public Jisuan()
{

this.setTitle("我的计算器");
this.setLayout(new BorderLayout());
this.add(tf,BorderLayout.NORTH);
this.add(p,BorderLayout.CENTER);
p.setLayout(new GridLayout(3,5));
/*for(int i=0;i<10;i++)
{
Button btemp=new Button(""+i);
p.add(btemp,""+i);
btemp.addActionListener(this);
}*/
p.add(a0);
p.add(a1);
p.add(a2);
p.add(a3);
p.add(a4);
p.add(a5);
p.add(a6);
p.add(a7);
p.add(a8);
p.add(a9);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
a0.addActionListener(this);
a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4.addActionListener(this);
a5.addActionListener(this);
a6.addActionListener(this);
a7.addActionListener(this);
a8.addActionListener(this);
a9.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
this.setBounds(100,100,400,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int ys=0;int sum1=0;int sum2=0;
if(e.getSource()==a0)
{
tf.setText(tf.getText()+0);
}else if(e.getSource()==a1)
{
tf.setText(tf.getText()+1);
}
else if(e.getSource()==a2)
{
tf.setText(tf.getText()+2);
}else if(e.getSource()==a3)
{
tf.setText(tf.getText()+3);

}else if(e.getSource()==a4)
{
tf.setText(tf.getText()+4);
}else if(e.getSource()==a5)
{
tf.setText(tf.getText()+5);
}else if(e.getSource()==a6)
{
tf.setText(tf.getText()+6);
}else if(e.getSource()==a7)
{
tf.setText(tf.getText()+7);
}else if(e.getSource()==a8)
{
tf.setText(tf.getText()+8);
}else if(e.getSource()==a9)
{
tf.setText(tf.getText()+9);
}else if(e.getSource()==b1)
{
ys=1;
sum1=Integer.parseInt(tf.getText().trim());
tf.setText("");
}else if(e.getSource()==b2)
{
ys=2;
sum1=Integer.parseInt(tf.getText().trim());
tf.setText("");
}else if(e.getSource()==b3)
{
ys=3;
sum1=Integer.parseInt(tf.getText().trim());
tf.setText("");
}else if(e.getSource()==b4)
{
sum=sum1=sum2=0;
tf.setText("");
}else if(e.getSource()==b5)
{
sum2=Integer.parseInt(tf.getText().trim());
switch(ys)
{
case 1:tf.setText(Int.toString((sum1+sum2)).trim());
break;
case 2:tf.setText(Int.toString((sum1-sum2)).trim());
break;
case 3:tf.setText(Int.toString((sum1*sum2)).trim());
break;
}

}
}
public static void main(String args[])
{
new Jisuan();
}
}

89

头像
今天属于1
2022-04-08

不知道你想实现什么样的计算。

72

相关问题

最新问答

排水论文在哪发?
伊兰0518 2021-09-19

小区市外排水论文发哪个杂志可以呢?我需要发表一篇这方面的论文。

word转pdf,为什么不显示图片图片?
花花的老妈 2021-09-19

我想把论文从word格式转换成PDF格式,用的金山WPS,可转换完成之后,里面的流程图就不见了,空白~~这是为什么呢?谁能帮我解决一下!谢谢!

公众号与小程序有什么区别
汤糖躺烫湯 2021-09-19

公众号与小程序有什么区别

如何制作电子小报
dream959595 2021-09-19

镀铬什么意思
autumngold 2021-09-19

镀铬什么意思

中国电影艺术的思想
幸福顺延 2021-09-19

中国针灸大纲作者是谁?
王子麻麻 2021-09-19

热门问答

排水论文在哪发?
伊兰0518 2021-09-19

小区市外排水论文发哪个杂志可以呢?我需要发表一篇这方面的论文。

word转pdf,为什么不显示图片图片?
花花的老妈 2021-09-19

我想把论文从word格式转换成PDF格式,用的金山WPS,可转换完成之后,里面的流程图就不见了,空白~~这是为什么呢?谁能帮我解决一下!谢谢!

公众号与小程序有什么区别
汤糖躺烫湯 2021-09-19

公众号与小程序有什么区别

如何制作电子小报
dream959595 2021-09-19

镀铬什么意思
autumngold 2021-09-19

镀铬什么意思

中国电影艺术的思想
幸福顺延 2021-09-19

中国针灸大纲作者是谁?
王子麻麻 2021-09-19

Coptyright © www.lw85.com电脑版