这是用java编写的计算器的源代码,界面粗糙,在elipse中运行通过,就两个类:第一个类frame ,代码如下
package jisuanqi;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class frame {
static double aa=0;//寄存器
static int bb=0;//判断是否为连续输入运算符
static int cc=0;
static char dd='d';//记录前一次运算符的值
public static void main(String args[]){
JFrame fm=new JFrame("计算器");
fm.setSize(220,180);
//fm.setMaximumSize(null);
fm.setLocation(new Point(200,100));
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container=fm.getContentPane();
GridBagLayout layout=new GridBagLayout();
container.setLayout(layout);
GridBagConstraints gg=new GridBagConstraints();
JTextField txt=new JTextField("0");
txt.setHorizontalAlignment(JTextField.RIGHT);
txt.setEditable(false);
txt.setBackground(Color.white);
gg.gridx=0;
gg.gridy=0;
gg.fill=GridBagConstraints.HORIZONTAL;
gg.gridheight=1;
gg.gridwidth=15;
container.add(txt,gg);
gg.fill=GridBagConstraints.NONE;
gg.gridy=1;
gg.gridwidth=2;
JButton btn7=new JButton("7");
Mylistener mylistener7 =new Mylistener(btn7.getText(),txt);
btn7.addActionListener(mylistener7);
container.add(btn7,gg);
JButton btn8=new JButton("8");
Mylistener mylistener8 =new Mylistener(btn8.getText(),txt); btn8.addActionListener(mylistener8);
gg.gridx=2;
container.add(btn8,gg);
JButton btn9=new JButton("9");
Mylistener mylistener9 =new Mylistener(btn9.getText(),txt); btn9.addActionListener(mylistener9);
gg.gridx=4;
container.add(btn9,gg);
JButton btnc=new JButton("/");
gg.gridx=6;
container.add(btnc,gg);
Mylistener mylistenerc =new Mylistener(btnc.getText(),txt); btnc.addActionListener(mylistenerc);
JButton btnb=new JButton("B");
gg.gridx=8;
container.add(btnb,gg);
Mylistener mylistenerb =new Mylistener(btnb.getText(),txt); btnb.addActionListener(mylistenerb);
gg.gridx=0;
gg.gridy=2;
JButton btn4=new JButton("4");
Mylistener mylistener4 =new Mylistener(btn4.getText(),txt); btn4.addActionListener(mylistener4);
container.add(btn4,gg);
JButton btn5=new JButton("5");
gg.gridx=2;
Mylistener mylistener5 =new Mylistener(btn5.getText(),txt); btn5.addActionListener(mylistener5);
container.add(btn5,gg);
JButton btn6=new JButton("6");
gg.gridx=4;
Mylistener mylistener6 =new Mylistener(btn6.getText(),txt); btn6.addActionListener(mylistener6);
container.add(btn6,gg);
JButton btncf=new JButton("*");
gg.gridx=6;
Mylistener mylistenercf =new Mylistener(btncf.getText(),txt); btncf.addActionListener(mylistenercf);
container.add(btncf,gg);
gg.gridx=8;
Mylistener mylistenerce =new Mylistener(btnce.getText(),txt); btnce.addActionListener(mylistenerce);
container.add(btnce,gg);
gg.gridx=0;
gg.gridy=3;
JButton btn1=new JButton("1");
container.add(btn1,gg);
Mylistener mylistener1 =new Mylistener(btn1.getText(),txt); btn1.addActionListener(mylistener1);
JButton btn2=new JButton("2");
gg.gridx=2;
Mylistener mylistener2 =new Mylistener(btn2.getText(),txt); btn2.addActionListener(mylistener2);
container.add(btn2,gg);
JButton btn3=new JButton("3");
gg.gridx=4;
Mylistener mylistener3 =new Mylistener(btn3.getText(),txt); btn3.addActionListener(mylistener3);
container.add(btn3,gg);
JButton btnjf=new JButton("-");
gg.gridx=6;
Mylistener mylistenerjf =new Mylistener(btnjf.getText(),txt); btnjf.addActionListener(mylistenerjf);
container.add(btnjf,gg);
JButton btncc=new JButton("C");
gg.gridx=8;
Mylistener mylistenercc =new Mylistener(btncc.getText(),txt); btncc.addActionListener(mylistenercc);
container.add(btncc,gg);
gg.gridx=0;
gg.gridy=4;
JButton btn0=new JButton("0");
container.add(btn0,gg);
Mylistener mylistenero =new Mylistener(btn0.getText(),txt); btn0.addActionListener(mylistenero);
JButton btnqf=new JButton("+/-");
gg.gridx=2;
Mylistener mylistenerqf =new Mylistener(btnqf.getText(),txt); btnqf.addActionListener(mylistenerqf);
container.add(btnqf,gg);
gg.gridx=4;
Mylistener mylistenerd =new Mylistener(btnd.getText(),txt);
btnd.addActionListener(mylistenerd);
container.add(btnd,gg);
JButton btnjiaf=new JButton("+");
gg.gridx=6;
Mylistener mylistenerfff =new Mylistener(btnjiaf.getText(),txt);
btnjiaf.addActionListener(mylistenerfff);
container.add(btnjiaf,gg);
JButton btndy=new JButton("=");
gg.gridx=8;
container.add(btndy,gg);
Mylistener mylistenerdy =new Mylistener(btndy.getText(),txt);
btndy.addActionListener(mylistenerdy);
fm.setVisible(true);
}
}
第二个类Mylistener 代码如下:
package jisuanqi;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
public class Mylistener implements ActionListener {
private String a;//button上的值
private JTextField txt;//txt的引用
Mylistener(String aa,JTextField txt){//构造方法
this.a=aa;
this.txt=txt;
}
void init(){
frame.aa=0;//寄存器
frame.bb=0;//判断是否为连续输入运算符
frame.cc=0;
frame.dd='d';//记录前一次运算符的值
this.txt.setText("0");
}
public void actionPerformed(ActionEvent e){
System.out.println(a);
if(a=="0"| a=="1"| a=="2" | a=="3"| a=="4"| a=="5"| a=="6"| a=="7" | a=="8" | a=="9" | a=="."){
if(frame.bb!=0){this.txt.setText("0");frame.bb=0;}
System.out.println("数字"+"b"+frame.bb);
if(this.txt.getText().length()==1){
//当前文本框里的字符串的长度为一
if(a=="."){//当前输入为小数点
this.txt.setText(this.txt.getText()+".");
}else{//当前输入不为小数点
if(this.txt.getText()=="0" & this.a=="0"){
this.txt.setText("0");
}else if(this.txt.getText().charAt(0)=='0'& this.a!="0"){
this.txt.setText(a);
}else if(this.txt.getText().charAt(0) != '0'){
this.txt.setText(this.txt.getText()+a);
}
}
}
else{//当前文本框里的字符串的长度不为一
if(a=="."){//当前输入是. 就查找txt的值中有不有 .
if(this.txt.getText().indexOf('.')== -1){//没出现.
this.txt.setText(this.txt.getText()+a);
}else {
//原字符串不变
}
}else{
this.txt.setText(this.txt.getText()+a);
}
}
}
//**************************************************
if(a=="+" | a=="-" | a=="*" | a=="/" ){
frame.bb++;
if(this.txt.getText().charAt(this.txt.getText().length()-1)=='.'){//处理以.结尾的字符串
this.txt.setText(this.txt.getText().substring(0,this.txt.getText().length ()-1));
}//如果字符串以.结尾就把字符串的最后以为抹去
if(frame.cc==0){//第一次输入运算符
frame.aa=Double.valueOf(this.txt.getText()).doubleValue();
frame.cc=1;
frame.dd=this.a.charAt(0);//记录前一次输入运算符的值
}else{//在这里面进行四则运算
//加法运算
if(frame.dd=="+".charAt(0)){
frame.aa+=Double.valueOf(this.txt.getText()).doubleValue();
this.txt.setText(String.valueOf(frame.aa).toString());
frame.dd=this.a.charAt(0);
}else if(frame.dd=="-".charAt(0)){
frame.aa-=Double.valueOf(this.txt.getText()).doubleValue();
this.txt.setText(String.valueOf(frame.aa).toString());
frame.dd=this.a.charAt(0);
}else if(frame.dd=="*".charAt(0)){
frame.aa*=Double.valueOf(this.txt.getText()).doubleValue();
this.txt.setText(String.valueOf(frame.aa).toString());
frame.dd=this.a.charAt(0);
}else if(frame.dd=="/".charAt(0)){
if(this.txt.getText().charAt(0)=="0".charAt(0)){//除零是不允许的
System.out.println("被除数不能为零");
this.init();
}else{
double
ff=Double.valueOf(this.txt.getText()).doubleValue();
frame.aa=frame.aa/ff;
this.txt.setText(String.valueOf(frame.aa).toString());
frame.dd=this.a.charAt(0);}
}
}
}
if(a=="B"){
if(this.txt.getText().length()==1 ){
this.txt.setText("0");
}
if(this.txt.getText().length()!=1 &
this.txt.getText().charAt(0)!='0'){
this.txt.setText(this.txt.getText().substring(0,this.txt.getText().length ()-1));
}
}
if(a=="+/-"){
if(this.txt.getText().charAt(0)!='-'){
this.txt.setText("-"+this.txt.getText());
}else{
this.txt.setText(this.txt.getText().substring(1));
}
}
if(a=="E"){
this.init();
}
if(a=="C"){
this.txt.setText("0");
}
if(a=="="){
if(frame.cc!=0){
if(frame.dd=="+".charAt(0)){
frame.aa+=Double.valueOf(this.txt.getText()).doubleValue();
this.txt.setText(String.valueOf(frame.aa).toString());
//frame.dd=this.a.charAt(0);
}else if(frame.dd=="-".charAt(0)){
frame.aa-=Double.valueOf(this.txt.getText()).doubleValue();
this.txt.setText(String.valueOf(frame.aa).toString());
//frame.dd=this.a.charAt(0);
}else if(frame.dd=="*".charAt(0)){
frame.aa*=Double.valueOf(this.txt.getText()).doubleValue();
this.txt.setText(String.valueOf(frame.aa).toString());
//frame.dd=this.a.charAt(0);
}else if(frame.dd=="/".charAt(0)){
if(this.txt.getText().charAt(0)=="0".charAt(0)){//除零是不允许的
System.out.println("被除数不能为零");
this.init();
}else{
double
ff=Double.valueOf(this.txt.getText()).doubleValue();
frame.aa=frame.aa/ff;
this.txt.setText(String.valueOf(frame.aa).toString());
//frame.dd=this.a.charAt(0);
}
}
frame.aa=0;//寄存器
frame.bb=1;//判断是否为连续输入运算符
frame.cc=0;
frame.dd='d';
}
}
}
}
本文档为【计算器设计java版的源代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。