import javax.swing.*; import java.awt.*; import java.awt.event.*; public class calculate { public static void main(String[] args) { jxframe kj=new jxframe(); kj.setVisible(true);

hundredcarriageΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 5 μήνες)

52 εμφανίσεις

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class calculate

{


public static void main(String[] args) {


jxframe kj=new jxframe();


kj.setVisible(true);


kj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

}

class jxframe
extends JFrame

{


JTextField w;


private boolean pd;


private String bj;


private double result;


public jxframe()


{



setTitle("
计算
");



pd=true;



bj="=";



result=0;



w=new JTextField(20);



add(w,"North");



add(new m(),"Center");



pack();





}


class m extends JComponent


{



public m()



{





setLayout(new GridLayout(4,4));




addbutton("7",new sz());




addbutton("8",new sz());




addbutton("9",new sz());




addbutton("/",new jx());




addbutton("4",new sz());




addbutton("5",new sz());




ad
dbutton("6",new sz());




addbutton("*",new jx());




addbutton("1",new sz());




addbutton("2",new sz());




addbutton("3",new sz());




addbutton("+",new jx());




addbutton("0",new sz());




addbutton(".",new sz());




addbutton("=",new jx());




addbut
ton("
-
",new jx());



}



public void addbutton(String name,ActionListener e)



{




JButton an=new JButton(name);




an.addActionListener(e);




add(an);



}



class sz implements ActionListener



{




public void actionPerformed(ActionEvent e)




{





String sb=e.getActionCommand();





if(pd)





{






w.setText("");






pd=false;





}





w.setText(w.getText()+sb);




}



}



class jx implements ActionListener



{




public void actionPerformed(ActionEvent e)




{





String jb=e.getActionCommand()
;





if(pd)





{






jb.equals("
-
");






w.setText(jb);






pd=false;





}





else{





js(Double.parseDouble(w.getText()));





bj=jb;





pd=true;







}




}



}



public void js(double jsb)



{




if(bj.equals("/")) result/=jsb;




if(bj.equals("*")) result*=jsb;




if(bj.equals("+")) result+=jsb;




if(bj.equals("
-
")) result
-
=jsb;




if(bj.equals("=")) result=jsb;




w.setText(""+result);



}


}

}