[讨论]我这有一个计算器的程序,有些地主看不明白,请指教喔
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
class win extends JFrame implements ActionListener{
static double n=0;
static double m1=0,m2=0;
static double temp=0;
static int flag=0;
JTextField jt1;
JLabel jl1,jl2,wife;
JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jb0,jbgl;
JButton plus,minus,multiply,divide,equal;
win(){
Container c=getContentPane();
c.setLayout(null);
jt1=new JTextField("0");
jt1.setSize(210,40);
jt1.setLocation(20,40);
c.add(jt1);
jl1=new JLabel("Tracy's calculator");
jl1.setSize(120,20);
jl1.setLocation(120,20);
c.add(jl1);
plus=new JButton("+");
plus.setSize(50,30);
plus.setLocation(260,300);
plus.addActionListener(this);
c.add(plus);
minus=new JButton("-");
minus.setSize(50,30);
minus.setLocation(260,240);
minus.addActionListener(this);
c.add(minus);
multiply=new JButton("*");
multiply.setSize(50,30);
multiply.setLocation(260,180);
multiply.addActionListener(this);
c.add(multiply);
divide=new JButton("/");
divide.setSize(50,30);
divide.setLocation(260,120);
divide.addActionListener(this);
c.add(divide);
equal=new JButton("=");
equal.setSize(50,30);
equal.setLocation(180,300);
equal.addActionListener(this);
c.add(equal);
jb0=new JButton("0");
jb0.setSize(50,30);
jb0.setLocation(20,300);
jb0.addActionListener(this);
c.add(jb0);
jb1=new JButton("1");
jb1.setSize(50,30);
jb1.setLocation(20,240);
jb1.addActionListener(this);
c.add(jb1);
jb2=new JButton("2");
jb2.setSize(50,30);
jb2.setLocation(100,240);
jb2.addActionListener(this);
c.add(jb2);
jb3=new JButton("3");
jb3.setSize(50,30);
jb3.setLocation(180,240);
jb3.addActionListener(this);
c.add(jb3);
jb4=new JButton("4");
jb4.setSize(50,30);
jb4.setLocation(20,180);
jb4.addActionListener(this);
c.add(jb4);
jb5=new JButton("5");
jb5.setSize(50,30);
jb5.setLocation(100,180);
jb5.addActionListener(this);
c.add(jb5);
jb6=new JButton("6");
jb6.setSize(50,30);
jb6.setLocation(180,180);
jb6.addActionListener(this);
c.add(jb6);
jb7=new JButton("7");
jb7.setSize(50,30);
jb7.setLocation(20,120);
jb7.addActionListener(this);
c.add(jb7);
jb8=new JButton("8");
jb8.setSize(50,30);
jb8.setLocation(100,120);
jb8.addActionListener(this);
c.add(jb8);
jb9=new JButton("9");
jb9.setSize(50,30);
jb9.setLocation(180,120);
jb9.addActionListener(this);
c.add(jb9);
jbgl=new JButton("C");
jbgl.setSize(50,30);
jbgl.setLocation(100,300);
jbgl.addActionListener(this);
c.add(jbgl);
setSize(350,400);
setLocation(100,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
String str="";
if(e.getSource()==jb0){
n=n*10+0; //红色部份
str=String.valueOf(n);
jt1.setText(str);
}
if(e.getSource()==jb1){
n=n*10+1;
str=String.valueOf(n);
jt1.setText(str);
}
if(e.getSource()==jb2){
n=n*10+2;
str=String.valueOf(n);
jt1.setText(str);
}
if(e.getSource()==jb3){
n=n*10+3;
str=String.valueOf(n);
jt1.setText(str);
}
if(e.getSource()==jb4){
n=n*10+4;
str=String.valueOf(n);
jt1.setText(str);
}
if(e.getSource()==jb5){
n=n*10+5;
str=String.valueOf(n);
jt1.setText(str);
}
if(e.getSource()==jb6){
n=n*10+6;
str=String.valueOf(n);
jt1.setText(str);
}
if(e.getSource()==jb7){
n=n*10+7;
str=String.valueOf(n);
jt1.setText(str);
}
if(e.getSource()==jb8){
n=n*10+8;
str=String.valueOf(n);
jt1.setText(str);
}
if(e.getSource()==jb9){
n=n*10+9;
str=String.valueOf(n);
jt1.setText(str);
}
if(e.getSource()==plus){
temp=Double.parseDouble(jt1.getText());
while(true){
if(flag==1){
m1=temp; //蓝色部份
m2=m1+m2;
flag=1;
break;
}
if(flag==2){
m1=temp;
m2=m2-m1;
flag=1;
break;
}
if(flag==3){
m1=temp;
m2=m2*m1;
flag=1;
break;
}
if(flag==4){
m1=temp;
m2=m2/m1;
flag=1;
break;
}
if(flag==0){
m2=temp;
flag=1;
break;
}
}
str=String.valueOf(m2);
jt1.setText(str);
n=0;
}
if(e.getSource()==minus){
temp=Double.parseDouble(jt1.getText());
while(true){
if(flag==1){
m1=temp;
m2=m1+m2;
flag=2;
break;
}
if(flag==2){
m1=temp;
m2=m2-m1;
flag=2;
break;
}
if(flag==3){
m1=temp;
m2=m2*m1;
flag=2;
break;
}
if(flag==4){
m1=temp;
m2=m2/m1;
flag=2;
break;
}
if(flag==0){
m2=temp;
flag=2;
break;
}
}
str=String.valueOf(m2);
jt1.setText(str);
n=0;
}
if(e.getSource()==multiply){
temp=Double.parseDouble(jt1.getText());
while(true){
if(flag==1){
m1=temp;
m2=m1+m2;
flag=3;
break;
}
if(flag==2){
m1=temp;
m2=m2-m1;
flag=3;
break;
}
if(flag==3){
m1=temp;
m2=m2*m1;
flag=3;
break;
}
if(flag==4){
m1=temp;
m2=m2/m1;
flag=3;
break;
}
if(flag==0){
m2=temp;
flag=3;
break;
}
}
str=String.valueOf(m2);
jt1.setText(str);
n=0;
}
if(e.getSource()==divide){
temp=Double.parseDouble(jt1.getText());
while(true){
if(flag==1){
m1=temp;
m2=m1+m2;
flag=4;
break;
}
if(flag==2){
m1=temp;
m2=m2-m1;
flag=4;
break;
}
if(flag==3){
m1=temp;
m2=m2*m1;
flag=4;
break;
}
if(flag==4){
m1=temp;
m2=m2/m1;
flag=4;
break;
}
if(flag==0){
m2=temp;
flag=4;
break;
}
}
str=String.valueOf(m2);
jt1.setText(str);
n=0;
}
if(e.getSource()==equal){
temp=Double.parseDouble(jt1.getText());
while(true){
if(flag==1){
m1=temp;
m2=m1+m2;
flag=0;
break;
}
if(flag==2){
m1=temp;
m2=m2-m1;
flag=0;
break;
}
if(flag==3){
m1=temp;
m2=m2*m1;
flag=0;
break;
}
if(flag==4){
m1=temp;
m2=m2/m1;
flag=0;
break;
}
if(flag==0){
m2=temp;
flag=0;
break;
}
}
str=String.valueOf(m2);
jt1.setText(str);
n=0;
}
if(e.getSource()==jbgl){
m1=0;
m2=0;
flag=0;
str=String.valueOf(m2);
jt1.setText(str);
n=0;
}
}
}
class Calculator{
public static void main(String args[]){
win tracy=new win();
}
}
问题1:红色部份那个n为什么要乘以10?可以不乘吗?
问题2:蓝色部份看不明白是什么意思,哪位大虾能不能给我讲一下。谢过了喔~~~~
在线关注