java编写计算器
发布时间:2020-12-15 03:22:40 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import javax.swing.Icon;imp
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; class Courser implements ActionListener{ private JFrame jf=new JFrame("newComputer"); private JTextField jte=new JTextField(); private Icon icon=new ImageIcon("A:"+File.separator+"b.jpg"); private JLabel jl=new JLabel(icon); private JButton j0,j1,j2,j3,j4,j5,j6,j7,j8,j9,j11,j12,j13,j14,j15,j16,j17,j18; private Double w=0.0; private String s1=""; private Double d1=0.0; private String s2=""; private Double d2=0.0; private String s3=""; private Double d3=0.0; private boolean boo1=false; private boolean boo2=false; private boolean boo3=false; public Courser(){ this.Jbutton(); this.Setsize(); jf.add(jl); jf.add(jte); jf.add(j1); jf.add(j2); jf.add(j3); jf.add(j4); jf.add(j5); jf.add(j6); jf.add(j7); jf.add(j8); jf.add(j9); jf.add(j0); jf.add(j11); jf.add(j12); jf.add(j13); jf.add(j14); jf.add(j15); jf.add(j16); jf.add(j17); jf.add(j18); jf.setVisible(true); } public void Setsize(){ jte.setText("0.0"); jf.setLayout(null); jte.setEnabled(false); jte.setHorizontalAlignment(JTextField.RIGHT); jf.setSize(390,350); jl.setSize(390,350); jte.setBounds(10,10,350,40); j1.setBounds(10,60,80,40); j2.setBounds(100,40); j3.setBounds(190,40); j11.setBounds(280,40); j4.setBounds(10,110,40); j5.setBounds(100,40); j6.setBounds(190,40); j12.setBounds(280,40); j7.setBounds(10,160,40); j8.setBounds(100,40); j9.setBounds(190,40); j13.setBounds(280,40); j0.setBounds(10,210,40); j18.setBounds(100,40); j17.setBounds(190,40); j14.setBounds(280,40); j15.setBounds(10,260,170,40); j16.setBounds(190,40); } public void Jbutton(){ j0=JB("0"); j1=JB("1"); j2=JB("2"); j3=JB("3"); j4=JB("4"); j5=JB("5"); j6=JB("6"); j7=JB("7"); j8=JB("8"); j9=JB("9"); j11=JB("+"); j13=JB("*"); j14=JB("/"); j15=JB("backspace"); j16=JB("c"); j17=JB("="); j18=JB("."); } public JButton JB(String s){ JButton jbu=new JButton(s); jbu.addActionListener(this); return jbu; } public void actionPerformed(ActionEvent e) { JButton b=(JButton)e.getSource(); if(Double.parseDouble(jte.getText())==0.0){ boo1=false; } if(b==j1||b==j2||b==j3||b==j4||b==j5||b==j6||b==j7||b==j8||b==j9){/ if(boo2==false){ // if(boo1){ // s1+=b.getText(); // jte.setText(jte.getText()+b.getText()); // }else{ // if(boo3){ // boo3=false; // s1+=b.getText(); // jte.setText(jte.getText()+b.getText()); // }else{ // s1=b.getText(); // jte.setText(s1); // boo1=true; // } // } // }if(boo2==true){ // if(boo1){ // s2+=b.getText(); // jte.setText(s2); // }else{ // if(boo3){ // boo3=false; // s2+=b.getText(); // jte.setText(s2); // }else{ // s2=b.getText(); // jte.setText(s2); // boo1=true; // } // } // }if("="==s3){ // boo2=false; // s1=""; // s2=""; // } // } if(b==j0){ if(boo2==false){ if(boo1){ s1+=b.getText(); jte.setText(jte.getText()+b.getText()); }else{ s1=b.getText(); jte.setText("0"); } }if(boo2==true){ if(boo1){ s2+=b.getText(); jte.setText(s2); }else{ s2=b.getText(); jte.setText("0.0"); } }if("="==s3){ boo2=false; s1=""; s2=""; } } if(b==j18){ if("="==s3){ boo2=false; s1=""; s2=""; } if(boo2==false){ if(s1!=""){ if(!(s1.contains("."))){ s1+=b.getText(); jte.setText(s1); boo3=true; } } } if(boo2==true){ if(s2!=""){ if(!(s2.contains("."))){ s2+=b.getText(); jte.setText(s2); boo3=true; } } } } if(b==j11||b==j12||b==j13||b==j14||b==j17){ if(s1!=""&&s2==""){ boo2=true; } if(s2!=""){ d1=Double.parseDouble(s1); d2=Double.parseDouble(s2); char c=s3.charAt(0); switch (c){ case'+':{ d3=d1+d2; break; } case'-':{ d3=d1-d2; break; } case'*':{ d3=d1*d2; break; } case'/':{ d3=d1/d2; break; } } jte.setText(""+d3); s1=""+d3; s2=""; } s3=b.getText(); } if(b==j16){ s1="0.0"; s2="0.0"; d1=0.0; d2=0.0; jte.setText("0.0") ; } if(b==j15){ if(boo2==false){ if(jte.getText().length()==1){ s1="0.0"; s2="0.0"; d1=0.0; d2=0.0; jte.setText("0.0"); }else{ s1=jte.getText().substring(0,jte.getText().length()-1); jte.setText(jte.getText().substring(0,jte.getText().length()-1)); } }else{ if(jte.getText().length()==1){ s2="0.0"; d2=0.0; jte.setText("0.0"); }else{ s2=jte.getText().substring(0,jte.getText().length()-1)); } } } } } public class NewCom { public static void main(String[] args) { new Courser(); } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |