预览加载中,请您耐心等待几秒...
1/8
2/8
3/8
4/8
5/8
6/8
7/8
8/8

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

用java编写一个简易的计算器代码精品资料精品资料仅供学习与交流,如有侵权请联系网站删除谢谢仅供学习与交流,如有侵权请联系网站删除谢谢精品资料仅供学习与交流,如有侵权请联系网站删除谢谢importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JTextField;/*使用java语言开发一个简易计算器***/publicclassTestJsqextendsJFrameimplementsActionListener,KeyListener{privateJTextFieldjtf;privateJButtonjb_bk,jb_ce,jb_c;privateStringxs="";privatedoublesum=0;privateintfh;publicstaticvoidmain(String[]args){newTestJsq().creatCUI();}publicvoidcreatCUI(){JFramejf=newJFrame();jf.setTitle("计算器");jtf=newJTextField("0.");jtf.setHorizontalAlignment(JTextField.RIGHT);jf.add(jtf,BorderLayout.NORTH);JPaneljp_main=newJPanel(newBorderLayout());jf.add(jp_main);JPaneljp1=newJPanel(newGridLayout(1,3,1,1));jb_bk=newJButton("Backspace");jb_bk.setForeground(Color.RED);jb_bk.addActionListener(this);jb_ce=newJButton("CE");jb_ce.setForeground(Color.RED);jb_ce.addActionListener(this);jb_c=newJButton("C");jb_c.setForeground(Color.RED);jb_c.addActionListener(this);jp1.add(jb_bk);jp1.add(jb_ce);jp1.add(jb_c);jp_main.add(jp1,BorderLayout.NORTH);JPaneljp2=newJPanel(newGridLayout(4,5,1,1));Stringstr[]={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};for(inti=0;i<20;i++){JButtonjb=newJButton(str[i]);jb.addActionListener(this);jb.setForeground(Color.blue);if(i==3||i==8||i==13||i==18||i==19){jb.setForeground(Color.RED);}jp2.add(jb);}jp_main.add(jp2,BorderLayout.CENTER);jf.setResizable(false);jf.setBounds(50,50,350,350);jf.setVisible(true);}@OverridepublicvoidactionPerformed(ActionEvente){Strings=e.getActionCommand();if(s.equals("CE")||s.equals("C")){xs="";jtf.setText("0.");}elseif(s.equals("Backspace")){Stringstr=xs.substring(0,xs.length()-1);xs=str;jtf.setText(xs);}elseif(s.equals("7")||s.equals("8")||s.equals("9")||