java编程计算器.doc

上传人:asd****56 文档编号:70332506 上传时间:2023-01-19 格式:DOC 页数:6 大小:45KB
返回 下载 相关 举报
java编程计算器.doc_第1页
第1页 / 共6页
java编程计算器.doc_第2页
第2页 / 共6页
点击查看更多>>
资源描述

《java编程计算器.doc》由会员分享,可在线阅读,更多相关《java编程计算器.doc(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、import java.awt.*;import javax.swing.*;import javax.swing.border.*;import java.awt.event.*;import javax.swing.JTextArea;import javax.swing.JFrame;import java.lang.String;import java.lang.Math;class JSQContainer contentPane; GridBagLayout gridbag; GridBagConstraints c; JTextField text;JLabel label;bo

2、olean flag=true; String s1,s2,s3;String op=; StringBuffer buffer;DeletAction delet; JSQ() JFrame f= new JFrame(计算器); f.setBounds(200,200,310,230); f.setResizable(false); /不可放大 tryUIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName(); catch(Exception e) f.setJMenuBar(addJMenuBar(); conte

3、ntPane=f.getContentPane(); gridbag=new GridBagLayout(); c=new GridBagConstraints(); contentPane.setLayout(gridbag); addDisplay(); addJPanel(); ActionListener number = new NumberAction(); ActionListener operate = new OperateAction(); ActionListener other = new OtherAction();addButton(MC,0,2,Color.red

4、,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);addButton(1,1,4,Color.blue,number);addButton(0,1,5,Color.blue,number);addButton(8,2,2,Color.blue,number);addButton(5

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);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,ope

6、rate);addButton(-,4,4,Color.red,operate);addButton(+,4,5,Color.red,operate);addButton(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); /创建菜单publi

7、c JMenuBar addJMenuBar() Font t=new Font(sanserif,Font.PLAIN,12); JMenuBar mb=new JMenuBar(); JMenu m1=new JMenu(编辑(E); JMenuItem copy=new JMenuItem(复制(C) Ctrl+C); JMenuItem paste=new JMenuItem(粘贴(P) Ctrl+v); m1.add(copy);m1.add(paste);m1.setFont(t); JMenu m2=new JMenu(查看(V); JMenuItem bz=new JMenuI

8、tem(标准型(T); JMenuItem kx=new JMenuItem( 科学型(S); JMenuItem sz=new JMenuItem( 数字分组(I); m2.add(bz);m2.add(kx);m2.addSeparator();m2.add(sz);m2.setFont(t); JMenu m3=new JMenu(帮助(H); JMenuItem content=new JMenuItem(帮助主题(H); JMenuItem about=new JMenuItem(关于计算器(A); m3.add(content);m3.addSeparator();m3.add(a

9、bout);m3.setFont(t); mb.add(m1);mb.add(m2);mb.add(m3); return mb; /创建文本框private void addDisplay() text = new JTextField(); text.setText();text.setEditable(false);text.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED);text.setHorizontalAlignment(JTextField.RIGHT); c.gridx=0; c.gridy=0; c

10、.gridwidth=6; c.gridheight=1; c.anchor=GridBagConstraints.CENTER;/设置组件位置 c.fill=GridBagConstraints.BOTH;/组件填充网格 c.insets=new Insets(0,1,1,1); gridbag.setConstraints(text,c); contentPane.add(text); /加按钮 public void addJPanel() JPanel pane=new JPanel(); label=new JLabel( ); label.setBorder(BorderFacto

11、ry.createBevelBorder(BevelBorder.LOWERED); pane.add(label); ActionListener delet=new DeletAction(); JButton button1=new JButton(Backspace); button1.setForeground(Color.red); pane.add(button1); JButton button2=new JButton(CE); button2.setForeground(Color.red); pane.add(button2); JButton button3=new J

12、Button(C); button3.setForeground(Color.red); pane.add(button3); button2.setPreferredSize(new Dimension(73,26); button3.setPreferredSize(new Dimension(73,26); button1.addActionListener(delet); button2.addActionListener(delet); button3.addActionListener(delet); c.gridx=0;c.gridy=1;c.gridwidth=6;c.grid

13、height=1;c.anchor=GridBagConstraints.CENTER;c.fill=GridBagConstraints.BOTH;c.insets=new Insets(1,1,1,1);gridbag.setConstraints(pane,c);contentPane.add(pane); private void addButton(String label,int row,int column,Color color,ActionListener action) JButton b=new JButton(label); c.gridx=row; c.gridy=c

14、olumn; c.gridwidth=1; c.gridheight=1; c.anchor=GridBagConstraints.CENTER; c.fill=GridBagConstraints.BOTH; c.insets=new Insets(1,1,1,1); b.setForeground(color); gridbag.setConstraints(b,c); b.addActionListener(action); contentPane.add(b);/注册监听器private class NumberAction implements ActionListenerpubli

15、c void actionPerformed(ActionEvent e) String num = e.getActionCommand(); if(flag) String temp = text.getText(); text.setText(temp+num);else text.setText(num); flag = true; private class OperateAction implements ActionListenerpublic void actionPerformed(ActionEvent e) String operate = e.getActionComm

16、and(); if(!operate.equals(=) op=operate; s1=text.getText();flag=false; else if(operate.equals(=) String s2 = text.getText(); double dou1 = Double.parseDouble(s1); double dou2 = Double.parseDouble(s2); if(op.equals(+) dou1 = dou1 + dou2 ; else if(op.equals(-) dou1 = dou1 - dou2; else if(op.equals(*)

17、dou1 = dou1 * dou2; else if(op.equals(/) dou1 = dou1 / dou2; text.setText(+dou1);flag=false; private class DeletAction implements ActionListenerpublic void actionPerformed(ActionEvent e) String delet = e.getActionCommand(); if(delet.equals(CE) text.setText(); else if(delet.equals(Backspace) String s

18、tr=text.getText(); int n=str.length(); String st=str.substring(0,n-1); text.setText(st); else if(delet.equals(C) text.setText(); private class OtherAction implements ActionListenerpublic void actionPerformed(ActionEvent e) String other = e.getActionCommand(); if(other.equals(.) text.setText(text.get

19、Text()+.); else if(other.equals(sqrt) text.setText(String.valueOf(Math.pow(Double.parseDouble(text.getText(),0.5); else if(other.equals(1/x)if(Double.parseDouble(text.getText()=0)text.setText(除数不能为零);elsetext.setText(String.valueOf(Math.pow(Double.parseDouble(text.getText(),-1);flag=false;else if(other.equals(%)text.setText(String.valueOf(Double.parseDouble(text.getText()/100); public static void main(String args) new JSQ();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 其他杂项

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁