




已阅读5页,还剩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;boolean 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(); contentPane=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,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,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,operate);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); /创建菜单public 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 JMenuItem(标准型(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(about);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.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(BorderFactory.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 JButton(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.gridheight=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=column; 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 ActionListenerpublic 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.getActionCommand(); 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(*) 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 str=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.getText()+.); else if(other.equals(sqrt) text.setText(String.valueOf(Math.pow(Double.parseDouble(text.getText(),0.5); else if(other.equals(1/
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 夫妻财产分割及子女监护权变更补充协议书
- 离婚时共同财产处理与子女抚养费支付合同
- 物业托管与社区智能化建设综合服务合同
- 物业服务合同补充协议:应急维修与费用承担
- 秦娜与丈夫离婚协议书中的赡养费及子女抚养权协议
- 四川离婚协议书:共同债务分担与追偿合同
- 酒店人力咨询管理方案
- 跨境电商进口业务物流运输安全风险评价与防控报告
- 六年级品德与社会上册 第二单元 为了建设新中国 1站起来的中国人民说课稿 北师大版
- 2.4 实验活动 1 配制一定物质的量浓度的溶液 教学设计 2024-2025学年高一上学期化学人教版(2019)必修第一册
- SMT与DIP工艺制程详细流程介绍
- 管理者角色认知与转换课件
- 2023年高校教师职业道德题库附答案(完整版)
- 护理管理学考试题库与答案
- 建筑防火设计-教学课件作者-主编-李耀庄-徐彧-建筑防火设计课件
- 静脉输液风险评估
- 水力发电厂生产安全性评价
- 全国质量奖现场汇报材料(生产过程及结果)课件
- 短歌行(优质课一等奖).课件
- 北师大版小学数学五年级上册第一单元《练习一》同步练习带答案
- 球墨铸铁管规格尺寸理论重量表
评论
0/150
提交评论