java计算机代码. java计算机代码怎么设置小数点只能打一个
跪求简单计算器的java代码,要入门级的编程~~
//////////////////////////这个很简单,方法也差不多////////////埋举掘////////
为黄岛等地区用户提供了全套网页设计制作服务,及黄岛网站建设行业解决方案。主营业务为网站制作、成都网站设计、黄岛网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator extends JFrame implements ActionListener
{
///////////////菜单
JMenuBar mb;
JMenu mEdit,mFind,mHelp;
JMenuItem mEditCopy,mEditPaste;
JMenuItem mFindStandarded,mFindScience,mFindGroup;
JMenuItem mHelpTopic,mHelpAbout;
JPanel pan;
/////////////按钮
JTextField tf;
JButton butnull;
JButton butBackspace,butCE,butC;
JButton butMC,butMR,butMS,butM;
JButton but1,but2,but3,but4,but5,but6,but7,but8,but9,but0;
JButton butChu,butSqrt,butCheng,butYu,butJian,but1x,butDot,butJia,butEqual,butJJ;
Calculator()
{
mb =new JMenuBar();
mEdit =new JMenu("编辑(E)");
mEdit.setMnemonic('E');
mFind =new JMenu("查看(F)");
mFind.setMnemonic('F');
mHelp =new JMenu("帮助(H)");
mFind.setMnemonic('H');
mEditCopy =new JMenuItem("复制(C)");
mEditCopy.setMnemonic('C');
mEditPaste =new JMenuItem("粘贴(P)");
mEditCopy.setMnemonic('P');
mFindStandarded=new JMenuItem("标准型(T)");
mFindStandarded.setMnemonic('T');
mFindScience=new JMenuItem("科学型(S)");
mFindScience.setMnemonic('S');
mFindGroup =new JMenuItem("数字分组(I)");
mFindGroup.setMnemonic('I');
mHelpTopic =new JMenuItem("帮助主题(H)");
mHelpTopic.setMnemonic('H');
mHelpAbout =new JMenuItem("关于我们弯核(A)");
mHelpAbout.setMnemonic('A');
////////////////////////////添加mb组件beginning
mb.add(mEdit);
mb.add(mFind);
mb.add(mHelp);
mEdit.add(mEditCopy);
mEdit.add(mEditPaste);
mFind.add(mFindStandarded);
mFind.add(mFindScience);
mFind.add(mFindGroup);
mHelp.add(mHelpTopic);
mHelp.add(mHelpAbout);//////////////ending
////////////////////////***创建计算机按钮***////////////////////
Color red =new Color(250,0,0);
Color green =new Color(0,127,0);
butnull =new JButton("?");
butnull.setForeground(red);
butnull.addActionListener(this);
butBackspace =new JButton("Backspace");
butBackspace.setForeground(green);
butCE =new JButton("答乱CE");
butCE.setForeground(green);
butC =new JButton("C");
butC.addActionListener(this);
butC.setForeground(red);
butMC =new JButton("MC");
butMC.setForeground(green);
butMR =new JButton("MR");
butMR.setForeground(green);
butMS =new JButton("MS");
butMS.setForeground(green);
butM =new JButton("M+");
butM.setForeground(green);
butChu =new JButton("/");
butChu.addActionListener(this);
butChu.setForeground(red);
butCheng =new JButton("*");
butCheng.addActionListener(this);
butCheng.setForeground(red);
butJian =new JButton("-");
butJian.addActionListener(this);
butJian.setForeground(red);
butJia =new JButton("+");
butJia.addActionListener(this);
butJia.setForeground(red);
butEqual =new JButton("=");
butEqual.addActionListener(this);
butEqual.setForeground(red);
Color blue =new Color(0,0,250);
butSqrt =new JButton("sqrt");
butSqrt.setForeground(green);
butYu =new JButton("%");
butYu.setForeground(green);
but1x =new JButton("1/x");
but1x.setForeground(green);
butDot =new JButton(".");
butDot.setForeground(blue);
butDot.addActionListener(this);
butJJ =new JButton("+/-");
butJJ.setForeground(blue);
butJJ.addActionListener(this);
but1 =new JButton("1");
but1.addActionListener(this);
but1.setForeground(blue);
but2 =new JButton("2");
but2.addActionListener(this);
but2.setForeground(blue);
but3 =new JButton("3");
but3.addActionListener(this);
but3.setForeground(blue);
but4 =new JButton("4");
but4.addActionListener(this);
but4.setForeground(blue);
but5 =new JButton("5");
but5.addActionListener(this);
but5.setForeground(blue);
but6 =new JButton("6");
but6.addActionListener(this);
but6.setForeground(blue);
but7 =new JButton("7");
but7.addActionListener(this);
but7.setForeground(blue);
but8 =new JButton("8");
but8.addActionListener(this);
but8.setForeground(blue);
but9 =new JButton("9");
but9.addActionListener(this);
but9.setForeground(blue);
but0 =new JButton("0");
but0.addActionListener(this);
but0.setForeground(blue);///////////////////////////////ending
pan =new JPanel(null);//创建最大的panel
tf =new JTextField("0");//输入显示框
tf.setBounds(5,0,337,25);//显示框设置大小位置
tf.setBackground(Color.white);
tf.setHorizontalAlignment(JTextField.RIGHT);//输入框设置位子居右显示
tf.setEditable(false);
JPanel pa=new JPanel(new BorderLayout());//所有按钮的panel
pa.setBounds(5,35,340,240);
JPanel pp=new JPanel(new BorderLayout());//null,MC,MR,MS,M+的panel;pa的西
JPanel aa=new JPanel(new BorderLayout());//其余所有Button的panel;pa的中
JPanel p =new JPanel(new GridLayout(4,1));//MC,MR,MS,M+的Panel,pp的中 北是一个butnull
JPanel a =new JPanel(new GridLayout(1,3));//backspace,ce,c的panel,aa的北
JPanel a2=new JPanel(new GridLayout(4,5));//其余的button的panel,aa的中
pa.add(pp,BorderLayout.WEST);
pa.add(aa,BorderLayout.CENTER);
pp.add(butnull,BorderLayout.NORTH);
pp.add(p,BorderLayout.CENTER);
p.add(butMC);
p.add(butMR);
p.add(butMS);
p.add(butM);
aa.add(a,BorderLayout.NORTH);
aa.add(a2,BorderLayout.CENTER);
a.add(butBackspace);
a.add(butCE);
a.add(butC);
a2.add(but7);
a2.add(but8);
a2.add(but9);
a2.add(butChu);
a2.add(butSqrt);
a2.add(but4);
a2.add(but5);
a2.add(but6);
a2.add(butCheng);
a2.add(butYu);
a2.add(but1);
a2.add(but2);
a2.add(but3);
a2.add(butJian);
a2.add(but1x);
a2.add(but0);
a2.add(butJJ);
a2.add(butDot);
a2.add(butJia);
a2.add(butEqual);
this.add(pan);
pan.add(tf);
pan.add(pa);
this.setTitle("我的计算器");
this.setJMenuBar(mb);
this.setBounds(200,300,353,330);//设置窗口大小
this.setResizable(false);
this.setVisible(true);
}
public static void main(String args[])
{
try
{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (Exception ex)
{
ex.printStackTrace();
}
new Calculator();
}
////////////////////********以下是监听设置和运算*******///////////////////
boolean b=true;//初始值设置为真未点运算符号
String fuhao="";//保存最后一次运算符号,用于进行上次结果的运算
String lastCommand="";//保存按等号前的最后的运算符号
double lastValue=0.0;//保存按等号钱的最后的输入框里的值
double value=0.0;//原值,当你点完运算符的时候计算之前的值
double fuzhi=0.0;
public void actionPerformed(ActionEvent click)/////////////////////行为监听设置
{
if(click.getSource()==butJJ)
{
fuzhi=Double.parseDouble(tf.getText());
if(tf.getText().equals("0")||tf.getText().equals("0."))
{
boolean b=true;
String fuhao="";
double value=0.0;
tf.setText("0");
}
else if(fuzhi0)
{
tf.setText((-fuzhi)+"");
}
else
{
tf.setText("-"+tf.getText());
}
}
if(click.getSource()==butnull)
{
tf.setText("本计算器版权所有归青软培训--胡迅--2008年10月18日著 ");
}
if(click.getSource()==butC)
{
tf.setText("0");
lastCommand="";
lastValue=0.0;
b=true;
fuhao="";
value=0.0;
}
if(click.getSource()==but1)
{
if(b==false)
{
tf.setText("1");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("1");
}
else
{
tf.setText(tf.getText()+1);
}
}
if(click.getSource()==but2)
{
if(b==false)
{
tf.setText("2");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("2");
}
else
{
tf.setText(tf.getText()+2);
}
}
if(click.getSource()==but3)
{
if(b==false)
{
tf.setText("3");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("3");
}
else
{
tf.setText(tf.getText()+3);
}
}
if(click.getSource()==but4)
{
if(b==false)
{
tf.setText("4");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("4");
}
else
{
tf.setText(tf.getText()+4);
}
}
if(click.getSource()==but5)
{
if(b==false)
{
tf.setText("5");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("5");
}
else
{
tf.setText(tf.getText()+5);
}
}
if(click.getSource()==but6)
{
if(b==false)
{
tf.setText("6");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("6");
}
else
{
tf.setText(tf.getText()+6);
}
}
if(click.getSource()==but7)
{
if(b==false)
{
tf.setText("7");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("7");
}
else
{
tf.setText(tf.getText()+7);
}
}
if(click.getSource()==but8)
{
if(b==false)
{
tf.setText("8");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("8");
}
else
{
tf.setText(tf.getText()+8);
}
}
if(click.getSource()==but9)
{
if(b==false)
{
tf.setText("9");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("9");
}
else
{
tf.setText(tf.getText()+9);
}
}
if(click.getSource()==but0)
{
if(b==false)
{
tf.setText("0");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("0");
}
else
{
tf.setText(tf.getText()+0);
}
}
if(click.getSource()==butDot)
{
/////////code/////////////////////////////////////////////////////////////////////////////////////////////////////////???
int izhi=Integer.parseInt(tf.getText());
double dzhi=Double.parseDouble(tf.getText());
if(tf.getText().equals("0"))
{
tf.setText("0.");
}
else
{
if(tf.getText().equals("0."))
{
tf.setText("0.");
}
else
{
if(dzhi0)
{
izhi=izhi;
dzhi=dzhi;
if(dzhi==izhi)
{
tf.setText(tf.getText()+".");
}
}
else
{
izhi=-izhi;
dzhi=-dzhi;
if(dzhi==izhi)
{
tf.setText(tf.getText()+".");
}
}
}
}
}
if(click.getSource()==butJia)///////////////////////////////////////////////"+"
{
if(tf.getText().equals("0"))
{
}
else if(fuhao=="")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=tfvaluejia;
tf.setText(""+value);
b=false;
fuhao="+";
}
else if(fuhao=="*")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
}
else if(fuhao=="/")
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
}
else if(fuhao=="-")
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
}
else if(fuhao=="=")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=tfvaluejia;
b=false;
}
else
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());//2
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
}
lastValue=Double.parseDouble(tf.getText());
fuhao="+";
lastCommand="+";
}
if(click.getSource()==butCheng)////////////////////////////////////////////////////////////"*"
{
if(tf.getText().equals("0"))
{
}
else if(fuhao=="")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
tf.setText(""+value);
b=false;
fuhao="*";
}
else if(fuhao=="+")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
}
else if(fuhao=="/")
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
}
else if(fuhao=="-")
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
}
else if(fuhao=="=")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
b=false;
}
else
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
}
lastValue=Double.parseDouble(tf.getText());
fuhao="*";
lastCommand="*";
}
if(click.getSource()==butChu)//////////////////////////////////////////////"/"
{
if(tf.getText().equals("0"))
{
}
else if(fuhao=="")
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=tfvalueChu;
tf.setText(""+value);
b=false;
fuhao="/";
}
else if(fuhao=="+")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
}
else if(fuhao=="*")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
}
else if(fuhao=="-")
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
}
else if(fuhao=="=")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
b=false;
}
else
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
}
lastValue=Double.parseDouble(tf.getText());
fuhao="/";
lastCommand="/";
}
if(click.getSource()==butJian)////////////////////////////////////////////////////////////////////////"-"
{
if(tf.getText().equals("0"))
{
}
else if(fuhao=="")
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=tfvaluejian;
tf.setText(""+value);
b=false;
fuhao="/";
}
else if(fuhao=="+")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
}
else if(fuhao=="*")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
}
else if(fuhao=="/")
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
}
else if(fuhao=="=")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
b=false;
}
else
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
}
lastValue=Double.parseDouble(tf.getText());
fuhao="-";
lastCommand="-";
}
if(click.getSource()==butEqual)//////////////////////////////////////////"="
{
if(tf.getText().equals("0"))
{
}
if(fuhao=="+")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
}
else if(fuhao=="*")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
}
else if(fuhao=="/")
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
}
else if(fuhao=="-")
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
}
else if(fuhao=="=")
{
if(lastCommand=="+")
{
value = value +lastValue;
tf.setText(""+value);
b=false;
}
else if(lastCommand=="-")
{
value = value - lastValue;
tf.setText(""+value);
b=false;
}
if(lastCommand=="*")
{
value = value *lastValue;
tf.setText(""+value);
b=false;
}
if(lastCommand=="/")
{
value = value /lastValue;
tf.setText(""+value);
b=false;
}
}
fuhao="=";
}
}
}
网站题目:java计算机代码. java计算机代码怎么设置小数点只能打一个
当前网址:http://ybzwz.com/article/dcighoi.html