资源描述:
《java编程计算器》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、importjava.awt.*;importjavax.swing.*;importjavax.swing.border.*;importjava.awt.event.*;importjavax.swing.JTextArea;importjavax.swing.JFrame;importjava.lang.String;importjava.lang.Math;classJSQ{ContainercontentPane;GridBagLayoutgridbag;GridBagConstraintsc;JTextFi
2、eldtext;JLabellabel;booleanflag=true;Strings1,s2,s3;Stringop="";StringBufferbuffer;DeletActiondelet;JSQ(){JFramef=newJFrame("计算器");f.setBounds(200,200,310,230);f.setResizable(false);//不可放大try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}c
3、atch(Exceptione){}f.setJMenuBar(addJMenuBar());contentPane=f.getContentPane();gridbag=newGridBagLayout();c=newGridBagConstraints();contentPane.setLayout(gridbag);addDisplay();addJPanel();ActionListenernumber=newNumberAction();ActionListeneroperate=newOperateActi
4、on();ActionListenerother=newOtherAction();addButton("MC",0,2,Color.red,number);addButton("MR",0,3,Color.red,number);addButton("MS",0,4,Color.red,number);addButton("M+",0,5,Color.red,number);addButton("7",1,2,Color.blue,number);addButton("4",1,3,Color.blue,number
5、);addButton("1",1,4,Color.blue,number);addButton("0",1,5,Color.blue,number);addButton("8",2,2,Color.blue,number);addButton("5",2,3,Color.blue,number);addButton("2",2,4,Color.blue,number);addButton("+/-",2,5,Color.blue,number);addButton("9",3,2,Color.blue,number)
6、;addButton("6",3,3,Color.blue,number);addButton("3",3,4,Color.blue,number);addButton(".",3,5,Color.blue,number);addButton("/",4,2,Color.red,operate);addButton("*",4,3,Color.red,operate);addButton("-",4,4,Color.red,operate);addButton("+",4,5,Color.red,operate);ad
7、dButton("sqrt",5,2,Color.blue,other);addButton("%",5,3,Color.blue,other);addButton("1/x",5,4,Color.blue,other);addButton("=",5,5,Color.blue,operate);f.setVisible(true);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}//创建菜单publicJMenuBaraddJMenuBar(){Fontt=newF
8、ont("sanserif",Font.PLAIN,12);JMenuBarmb=newJMenuBar();JMenum1=newJMenu("编辑(E)");JMenuItemcopy=newJMenuItem("复制(C)Ctrl+C");JMenuItempaste=newJMenuItem("粘贴(P)Ctrl+v");