java-简单计算器窗口

编程实现:有四个按钮,分别为加 减 乘 除;窗口中又三个文本行,单击任意一按钮,将两个文本行的数据进行相应运算,在第单个文本行中显示结果

窗口如图所示 例:点击*出现计算结果;

package Chapter;import java.awt.*;
import java.awt.event.*;
public class Scomputer extends  Frame{public Scomputer(){
this.setSize(400,400);
this.setLayout(null);
this.setTitle("简单计算器");
this.setVisible(true);
this.setLayout(new FlowLayout(FlowLayout.LEFT,20,20));
Button sum=new Button("+");
Button sub=new Button("-");
Button mul=new Button("*");
Button divi=new Button("/");
TextField txt1=new  TextField();
TextField txt2=new  TextField();
TextField txt3=new  TextField();
txt3.setBounds(5, 5, 30, 20);
this.add(divi);
this.add(mul);
this.add(sub);
this.add(sum);
this.add(txt1);
this.add(txt2);
this.add(txt3);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
sum.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ 
int i=Integer.parseInt(txt1.getText());
int j=Integer.parseInt(txt2.getText());
txt3.setText(String.valueOf(i+j));
}
});
sub.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ 
int i=Integer.parseInt(txt1.getText());
int j=Integer.parseInt(txt2.getText());
txt3.setText(String.valueOf(i-j));
}
});


mul.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ 
int i=Integer.parseInt(txt1.getText());
int j=Integer.parseInt(txt2.getText());
txt3.setText(String.valueOf(i*j));
}
});
divi.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ 
int i=Integer.parseInt(txt1.getText());
int j=Integer.parseInt(txt2.getText());
txt3.setText(String.valueOf(i/j));
}
});
}
public static void main(String[] args){
new Scomputer();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值