概述
编程实现:有四个按钮,分别为加 减 乘 除;窗口中又三个文本行,单击任意一按钮,将两个文本行的数据进行相应运算,在第单个文本行中显示结果。
窗口如图所示 例:点击*出现计算结果;
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();
}
}
最后
以上就是畅快毛巾为你收集整理的java-简单计算器窗口的全部内容,希望文章能够帮你解决java-简单计算器窗口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复