《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();