Java计算器源代码.pdf

上传人:索**** 文档编号:76194149 上传时间:2023-03-08 格式:PDF 页数:45 大小:78.58KB
返回 下载 相关 举报
Java计算器源代码.pdf_第1页
第1页 / 共45页
Java计算器源代码.pdf_第2页
第2页 / 共45页
点击查看更多>>
资源描述

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

1、Java计算器源代码该计算器有两个主要部分,标准计算器和科学计算器,科学计算器功能尚未完成。用的知识:按钮,菜单,布局,面板,窗口,事件,对话框,标签,单选钮。有兴趣的读者可以完成剩下的功能。组成:三个源程序:Cal.java,BPanel.java,KPanel.java/Cal.java/package cal;import java.awt.CardLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Gr

2、idLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.InputEvent;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.

3、ButtonGroup;import javax.swing.ImageIcon;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JRadioButtonMenuItem;import javax.swing.KeyStroke;pub

4、lic class Cal extends JFrame/*/private static final long serialVersionUID=1L;private JMenuBar mb=null;private JMenu m1=null;private JMenu m2=null;private JMenu m4=null;private JMenuItem mt1=null;public staticJMenuItem mt2=null;private JRadioButtonMenuItem mt3=null;private JRadioButtonMenuItem mt4=nu

5、ll;private JMenuItem mt6=null;private JMenuItem mt7=null;private BPanel bp=new BPanel();private KPanel kp=new KPanel();private JPanel p=new JPanel();private static boolean card=true;public static boolean fz=false;public static String str=new String();private JPanel p1=new JPanel();private CardLayout

6、 c1=new CardLayout();private JLabel b=new JLabel();static Cal calc;final JDialog d1=new JDialog();final JDialog d2=new JDialog();public static Color bc=new Color(255,204,204);public static Color qc=Color.cyan;private Font f=new Font(宋体,Font.BOLD,15);private Cal()super(计算器);this.setLocation(400,250);

7、this.setForeground(qc);this.setBackground(bc);this.setIconImage(new ImageIcon(jsq.jpg).getImage();JFrame.setDefaultLookAndFeelDecorated(true);p=(JPanel)this.getContentPane();p.setLayout(new FlowLayout();p.add(b);p.add(p1);p1.setLayout(c1);p1.add(card1,bp);p1.add(card2,kp);this.setJMenuBar(getMb();in

8、itDialog1();initDialog2();this.pack();this.setResizable(false);this.setVisible(true);protected void processWindowEvent(WindowEvent e)if(e.getID()=WindowEvent.WINDOW_CLOSING)System.exit(0);private JMenuBar getMb()mb=new JMenuBar();mb.setPreferredSize(new Dimension(110,35);mb.setForeground(qc);mb.setB

9、ackground(bc);mb.add(getM1();mb.add(getM2();mb.add(getM4();return mb;private JMenu getM1()m1=new JMenu(编辑(F);m1.setFont(f);m1.setBackground(bc);m1.add(getMt1();m1.add(getMt2();return m1;private JMenu getM2()m2=new JMenu(类型(L);m2.setFont(f);m2.setBackground(bc);m2.add(getMt3();m2.add(getMt4();ButtonG

10、roup bg=new ButtonGroup();bg.add(mt3);mt3.setSelected(true);bg.add(mt4);return m2;private JMenu getM4()m4=new JMenu(帮助(H);m4.setFont(f);m4.setBackground(bc);m4.add(getMt6();m4.add(getMt7();return m4;private JMenuItem getMt1()mt1=new JMenuItem(复制);mt1.setFont(f);mt1.setAccelerator(KeyStroke.getKeyStr

11、oke(KeyEvent.VK_C,InputEvent.CTRL_MASK);mt1.setPreferredSize(new Dimension(130,30);mt1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)try if(card)if(bp.getTextField().getSelectedText().isEmpty()str=bp.getTextField().getText();else str=bp.getTextField().getSelectedTex

12、t();catch(Exception tx)str=bp.getTextField().getText();if(!str.isEmpty()&!str.equals(0)mt2.setEnabled(true);BPanel.jp2.setEnabled(true););return mt1;private JMenuItem getMt2()mt2=new JMenuItem(粘贴);mt2.setFont(f);mt2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK);mt2.setPre

13、ferredSize(new Dimension(130,30);mt2.setEnabled(false);mt2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)if(card)bp.getTextField().setText(str);fz=true;/esle/kp.getTextField().setText(str););return mt2;private JRadioButtonMenuItem getMt3()mt3=new JRadioButtonMenuIte

14、m(标准计算器);mt3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_MASK);mt3.setFont(f);mt3.setPreferredSize(new Dimension(130,30);mt3.addItemListener(new ItemListener()public void itemStateChanged(ItemEvent e)c1.first(p1););return mt3;private JRadioButtonMenuItem getMt4()mt4=new JRadi

15、oButtonMenuItem(科学计算器);mt4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K,InputEvent.CTRL_MASK);mt4.setFont(f);mt4.setPreferredSize(new Dimension(130,30);mt4.addItemListener(new ItemListener()public void itemStateChanged(ItemEvent e)c1.last(p1););return mt4;private JMenuItem getMt6()mt6=new JMe

16、nuItem(关于计算器);mt6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK);mt6.setFont(f);mt6.setPreferredSize(new Dimension(130,30);mt6.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)d1.setVisible(true););return mt6;private JMenuItem getMt7()mt7=new

17、 JMenuItem(帮助);mt7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK);mt7.setFont(f);mt7.setPreferredSize(new Dimension(130,30);mt7.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)d2.setVisible(true););return mt7;public void initDialog1()d1.setT

18、itle(关于计算器);JDialog.setDefaultLookAndFeelDecorated(true);String s1=计算器由标准计算器和科学计算器组成;String s2=j2se 版本:jdk1.6。本人 QQ:1286908118;Font font=new Font(宋体,Font.BOLD,20);JLabel l1=new JLabel(s1);JLabel l2=new JLabel(s2);JPanel dp=(JPanel)d1.getContentPane();dp.setLayout(new GridLayout(2,1);dp.setBackground

19、(Color.pink);dp.setForeground(Color.pink);l1.setForeground(Color.blue);l1.setFont(font);dp.add(l1);l2.setForeground(Color.red);l2.setFont(font);dp.add(l2);d1.setSize(400,200);d1.setFont(f);d1.setLocation(500,300);d1.setResizable(false);d1.addWindowListener(new WindowAdapter()public void windowClosin

20、g(WindowEvent e)d1.setVisible(false););public void initDialog2()d2.setTitle(帮助);String s1=复制快捷键为ctrl+c;String s2=粘贴快捷键为ctrl+v;Font font=new Font(宋体,Font.BOLD,20);JLabel l1=new JLabel(s1);JLabel l2=new JLabel(s2);JPanel dp=(JPanel)d2.getContentPane();dp.setLayout(new GridLayout(2,1);dp.setBackground(

21、Color.pink);dp.setForeground(Color.pink);l1.setFont(font);l1.setForeground(Color.blue);dp.add(l1);l2.setFont(font);l1.setForeground(Color.blue);dp.add(l2);d2.setSize(new Dimension(400,240);d2.setResizable(false);d2.setFont(f);d2.setLocation(500,300);JDialog.setDefaultLookAndFeelDecorated(true);d2.ad

22、dWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)d2.setVisible(false););public static void main(String args)calc=new Cal();/BPanel.java/package cal;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.GridLayout;i

23、mport java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JButton;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swi

24、ng.JPopupMenu;import javax.swing.JTextField;import javax.swing.KeyStroke;import javax.swing.SwingUtilities;public class BPanel extends JPanel implements ActionListener/*/private static final long serialVersionUID=1L;JTextField tf=null;private JButton s1=null;private JButton s2=null;private JButton s

25、3=null;private JButton s4=null;private JButton s5=null;private JButton s6=null;private JButton s7=null;private JButton s8=null;private JButton s9=null;private JButton s0=null;private JButton f1=null;private JButton f2=null;private JButton f3=null;private JButton f4=null;private JButton f5=null;priva

26、te JButton f6=null;private JButton c1=null;private JButton c2=null;private JButton d1=null;private JButton d2=null;private JPopupMenu jpm=null;private JMenuItem jp1=null;public static JMenuItem jp2=null;private JPanel pp=new JPanel();private static boolean add=false,sub=false,mul=false,div=false,qy=

27、false,sz=false,jg=false,v1=false,v2=false;private static double value1=0d;private static double value2=0d;private int num=0;private Font f=new Font(宋体,Font.BOLD,16);private Font sign=new Font(宋体,Font.BOLD,15);private Color c=new Color(10,170,10);public BPanel()/BPanel 的构造方法initButton();initPopupMenu

28、();initTextField();this.setLayout(new BorderLayout();this.add(North,tf);this.add(Center,pp);pp.setLayout(new GridLayout(4,5,3,2);pp.add(s7);pp.add(s8);pp.add(s9);pp.add(c1);pp.add(c2);pp.add(s4);pp.add(s5);pp.add(s6);pp.add(f1);pp.add(f3);pp.add(s1);pp.add(s2);pp.add(s3);pp.add(f2);pp.add(f4);pp.add

29、(s0);pp.add(d1);pp.add(d2);pp.add(f5);pp.add(f6);this.setVisible(true);this.setBackground(Cal.bc);this.setForeground(Cal.qc);private void initButton()/初始化按钮s1=new JButton(1);s1.setPreferredSize(new Dimension(80,50);s1.setBackground(Cal.bc);s1.setForeground(c);s1.setFont(f);s1.addActionListener(this)

30、;s2=new JButton(2);s2.setPreferredSize(new Dimension(80,50);s2.setBackground(Cal.bc);s2.setForeground(c);s2.setFont(f);s2.addActionListener(this);s3=new JButton(3);s3.setPreferredSize(new Dimension(80,50);s3.setBackground(Cal.bc);s3.setForeground(c);s3.setFont(f);s3.addActionListener(this);s4=new JB

31、utton(4);s4.setPreferredSize(new Dimension(80,50);s4.setBackground(Cal.bc);s4.setForeground(c);s4.setFont(f);s4.addActionListener(this);s5=new JButton(5);s5.setPreferredSize(new Dimension(80,50);s5.setBackground(Cal.bc);s5.setForeground(c);s5.setFont(f);s5.addActionListener(this);s6=new JButton(6);s

32、6.setPreferredSize(new Dimension(80,50);s6.setBackground(Cal.bc);s6.setForeground(c);s6.setFont(f);s6.addActionListener(this);s7=new JButton(7);s7.setPreferredSize(new Dimension(80,50);s7.setBackground(Cal.bc);s7.setForeground(c);s7.setFont(f);s7.addActionListener(this);s8=new JButton(8);s8.setPrefe

33、rredSize(new Dimension(80,50);s8.setBackground(Cal.bc);s8.setForeground(c);s8.setFont(f);s8.addActionListener(this);s9=new JButton(9);s9.setPreferredSize(new Dimension(80,50);s9.setBackground(Cal.bc);s9.setForeground(c);s9.setFont(f);s9.addActionListener(this);s0=new JButton(0);s0.setPreferredSize(n

34、ew Dimension(80,50);s0.setBackground(Cal.bc);s0.setForeground(c);s0.setFont(f);s0.addActionListener(this);f1=new JButton(+);f1.setPreferredSize(new Dimension(80,50);f1.setFont(sign);f1.setBackground(Cal.bc);f1.setForeground(c);f1.addActionListener(this);f2=new JButton(-);f2.setPreferredSize(new Dime

35、nsion(80,50);f2.setFont(sign);f2.setBackground(Cal.bc);f2.setForeground(c);f2.addActionListener(this);f3=new JButton(*);f3.setPreferredSize(new Dimension(80,50);f3.setFont(sign);f3.setBackground(Cal.bc);f3.setForeground(c);f3.addActionListener(this);f4=new JButton(/);f4.setPreferredSize(new Dimensio

36、n(80,50);f4.setFont(sign);f4.setBackground(Cal.bc);f4.setForeground(c);f4.addActionListener(this);f5=new JButton(SPRT);f5.setPreferredSize(new Dimension(80,50);/f5.setFont(f);f5.setBackground(Cal.bc);f5.setForeground(c);f5.addActionListener(this);f6=new JButton(%);f6.setPreferredSize(new Dimension(8

37、0,50);f6.setBackground(Cal.bc);f6.setForeground(c);f6.addActionListener(this);c1=new JButton(清除);c1.setPreferredSize(new Dimension(80,50);/c1.setFont(f);c1.setBackground(Cal.bc);c1.setForeground(c);c1.addActionListener(this);c2=new JButton(删除);c2.setPreferredSize(new Dimension(80,50);/c2.setFont(f);

38、c2.setBackground(Cal.bc);c2.setForeground(c);c2.addActionListener(this);d1=new JButton(.);d1.setPreferredSize(new Dimension(80,50);d1.setFont(f);d1.setBackground(Cal.bc);d1.setForeground(c);d1.addActionListener(this);d2=new JButton(=);d2.setPreferredSize(new Dimension(80,50);d2.setFont(sign);d2.setB

39、ackground(Cal.bc);d2.setForeground(c);d2.addActionListener(this);private void initPopupMenu()/初始化弹出式菜单开始jpm=new JPopupMenu(文本框);jpm.add(getJp1();jpm.add(getJp2();/初始化弹出式菜单结束private JMenuItem getJp1()jp1=new JMenuItem(复制);jp1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK);j

40、p1.setFont(f);jp1.setPreferredSize(new Dimension(130,30);jp1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)try if(tf.getSelectedText().isEmpty()Cal.str=tf.getText();else Cal.str=tf.getSelectedText();catch(Exception tx)Cal.str=tf.getText();if(!Cal.str.isEmpty()&!Cal.

41、str.equals(0)jp2.setEnabled(true);Cal.mt2.setEnabled(true););return jp1;private JMenuItem getJp2()jp2=new JMenuItem(粘贴);jp2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK);jp2.setFont(f);jp2.setEnabled(false);jp2.setPreferredSize(new Dimension(130,30);jp2.addActionListener(

42、new ActionListener()public void actionPerformed(ActionEvent e)tf.setText(Cal.str);Cal.fz=true;);return jp2;private void initTextField()/初始化文本框tf tf=new JTextField(0,15);tf.setEditable(false);tf.setHorizontalAlignment(JTextField.RIGHT);tf.setSelectedTextColor(Color.red);tf.setSelectionColor(Color.DAR

43、K_GRAY);tf.setBackground(Cal.bc);tf.setForeground(Color.black);tf.setPreferredSize(new Dimension(250,40);Font ff=new Font(宋体,Font.BOLD,35);tf.setFont(ff);tf.addMouseListener(new MouseAdapter()public void mouseClicked(MouseEvent e)if(SwingUtilities.isRightMouseButton(e)jpm.show(tf,e.getX(),e.getY();)

44、;public JTextField getTextField()/获取当前类的文本框tf return tf;public int delete()/删除文本框 tf 末尾的字符add=false;sub=false;mul=false;div=false;qy=false;sz=true;jg=false;if(tf.getText().equals()tf.setText(0);return-1;elseStringBuffer ds=new StringBuffer(tf.getText();tf.setText(ds.deleteCharAt(ds.length()-1).toStr

45、ing();if(tf.getText().equals()tf.setText(0);return 1;/delete 结束public void actionPerformed(ActionEvent e)/ActionListener开始,响应按钮JButton source=(JButton)e.getSource();if(source=s1)/对“1”响应开始 S1();/对“1”响应结束else if(source=s2)/对“2”响应开始 S2();/对“2”响应结束else if(source=s3)/对“3”响应开始 S3();/对“3”响应结束else if(source

46、=s4)/对“4”响应开始 S4();/对“4”响应结束else if(source=s5)/对“5”响应开始 S5();/对“5”响应结束else if(source=s6)/对“6”响应开始 S6();/对“6”响应结束else if(source=s7)/对“7”响应开始 S7();/对“7”响应结束else if(source=s8)/对“8”响应开始 S8();/对“8”响应结束else if(source=s9)/对“9”响应开始 S9();/对“9”响应结束else if(source=s0)/对“0”响应开始 S0();/对“0”响应结束else if(source=d1)/对

47、“.”响应开始 D1();/对“.”响应结束else if(source=c1)/对“清除”按钮响应开始 tf.setText(0);initValue();/对“清除”按钮响应结束else if(source=c2)/对“删除”按钮响应开始delete();num=tf.getText().length();/对“删除”按钮响应结束else if(source=f1)/对“+”响应开始 F1();/对“+”响应结束else if(source=f2)/对“-”响应开始 F2();/对“-”响应结束else if(source=f3)/对“*”响应开始 F3();/对“*”响应结束else i

48、f(source=f4)/对“/”响应开始 F4();/对“/”响应结束else if(source=f5)/对“空”响应开始 F5();/对“空”响应结束else if(source=f6)/对“%”响应开始 F6();/对“%”响应结束else if(source=d2)/对“=”响应开始 try if(sz)if(!v1)value1=Double.parseDouble(tf.getText();else if(!v2)value2=Double.parseDouble(tf.getText();v2=true;calc();catch(Exception e11)tf.setText

49、(格 式 错 误!);initValue();/ActionListener结束public void initValue()jg=false;sz=false;add=false;sub=false;mul=false;div=false;qy=false;v1=false;v2=false;Cal.fz=false;num=0;public void S1()if(num15)return;if(tf.getText().equals(0)|!sz&!Cal.fz)tf.setText(1);else tf.setText(tf.getText()+1);sz=true;num=tf.ge

50、tText().length();public void S2()if(num15)return;if(tf.getText().equals(0)|!sz&!Cal.fz)tf.setText(2);else tf.setText(tf.getText()+2);sz=true;num=tf.getText().length();public void S3()if(num15)return;if(tf.getText().equals(0)|!sz&!Cal.fz)tf.setText(3);else tf.setText(tf.getText()+3);sz=true;num=tf.ge

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

当前位置:首页 > 技术资料 > 实施方案

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

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