请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册
首页 社区 Java 计算器java代码(基本的加减乘除) 能运行 的程序 直接 发 ...

计算器java代码(基本的加减乘除) 能运行 的程序 直接 发到我

猿梦 7 天前
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class _Calc0 extends JFrame implements ActionListener{private JPanel jPanel1,jPanel2;private JTextField resultField;private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2;private boolean end,add,sub,mul,div;private String str;private double num1,num2;public _Calc0(){super(&quot;计算器&quot;);setSize(300,240);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Container con=getContentPane();con.setLayout(new BorderLayout());jPanel1=new JPanel();jPanel1.setLayout(new GridLayout(1,1));jPanel2=new JPanel();jPanel2.setLayout(new GridLayout(4,4));resultField=new JTextField(&quot;0&quot;);jPanel1.add(resultField);con.add(jPanel1,BorderLayout.NORTH);s1=new JButton(&quot;  1  &quot;);  s1.addActionListener(this);s2=new JButton(&quot;  2  &quot;);  s2.addActionListener(this);s3=new JButton(&quot;  3  &quot;);  s3.addActionListener(this);s4=new JButton(&quot;  4  &quot;);  s4.addActionListener(this);s5=new JButton(&quot;  5  &quot;);  s5.addActionListener(this);s6=new JButton(&quot;  6  &quot;);  s6.addActionListener(this);s7=new JButton(&quot;  7  &quot;);  s7.addActionListener(this);s8=new JButton(&quot;  8  &quot;);  s8.addActionListener(this);s9=new JButton(&quot;  9  &quot;);  s9.addActionListener(this);s0=new JButton(&quot;  0  &quot;);  s0.addActionListener(this);b1=new JButton(&quot;  +  &quot;);  b1.addActionListener(this);b2=new JButton(&quot;  -  &quot;);  b2.addActionListener(this);b3=new JButton(&quot;  *  &quot;);  b3.addActionListener(this);b4=new JButton(&quot;  /  &quot;);  b4.addActionListener(this);f1=new JButton(&quot;  .  &quot;);  f1.addActionListener(this);f2=new JButton(&quot;  =  &quot;);  f2.addActionListener(this);jPanel2.add(s1);jPanel2.add(s2);jPanel2.add(s3);jPanel2.add(b1);jPanel2.add(s4);jPanel2.add(s5);jPanel2.add(s6);jPanel2.add(b2);jPanel2.add(s7);jPanel2.add(s8);jPanel2.add(s9);jPanel2.add(b3);jPanel2.add(s0);jPanel2.add(f1);jPanel2.add(f2);jPanel2.add(b4);con.add(jPanel2,BorderLayout.CENTER);}public void num(int i){String s = null;s=String.valueOf(i);if(end){//如果数字输入结束,则将文本框置零,重新输入resultField.setText(&quot;0&quot;);end=false;}if((resultField.getText()).equals(&quot;0&quot;)){//如果文本框的内容为零,则覆盖文本框的内容resultField.setText(s);}else{//如果文本框的内容不为零,则在内容后面添加数字str = resultField.getText() + s;resultField.setText(str);}}public void actionPerformed(ActionEvent e){ //数字事件if(e.getSource()==s1)num(1);else if(e.getSource()==s2)num(2);else if(e.getSource()==s3)num(3);else if(e.getSource()==s4)num(4);else if(e.getSource()==s5)num(5);else if(e.getSource()==s6)num(6);else if(e.getSource()==s7)num(7);else if(e.getSource()==s8)num(8);else if(e.getSource()==s9)num(9);else if(e.getSource()==s0)num(0);//符号事件else if(e.getSource()==b1)sign(1);else if(e.getSource()==b2)sign(2);else if(e.getSource()==b3)sign(3);else if(e.getSource()==b4)sign(4);//等号else if(e.getSource()==f1){str=resultField.getText();if(str.indexOf(&quot;.&quot;)<=1){str+=&quot;.&quot;;resultField.setText(str);}}else if(e.getSource()==f2){num2=Double.parseDouble(resultField.getText());if(add){num1=num1 + num2;}else if(sub){num1=num1 - num2;}else if(mul){num1=num1 * num2;}else if(div){num1=num1 / num2;}resultField.setText(String.valueOf(num1));end=true;}}public void sign(int s){if(s==1){add=true;sub=false;mul=false;div=false;}else if(s==2){add=false;sub=true;mul=false;div=false;}else if(s==3){add=false;sub=false;mul=true;div=false;}else if(s==4){add=false;sub=false;mul=false;div=true;}num1=Double.parseDouble(resultField.getText());end=true;}public static void main(String[] args){new _Calc0();}}
猫段沛白打死·开关江笑萍压低%好,这就发
电线向依玉送来@猫电视写完了作文$这都不会   晕

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册