1、Java计算器源代码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;impo
2、rt java.awt.GridLayout;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;impor
3、t javax.swing.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
4、.KeyStroke;public 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 static JMenuItem mt2=null; private JRadioButtonMenuItem mt3=null; priv
5、ate JRadioButtonMenuItem mt4=null; 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 J
6、Panel p1=new JPanel(); private CardLayout 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);
7、 private Cal() super( 计算器 ); this.setLocation(400, 250); 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
8、(c1); p1.add(card1,bp); p1.add(card2,kp); this.setJMenuBar(getMb(); initDialog1(); 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 JM
9、enuBar(); mb.setPreferredSize(new Dimension(110, 35); mb.setForeground(qc); mb.setBackground(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 ge
10、tM2() m2=new JMenu( 类型(L) ); m2.setFont(f); m2.setBackground(bc); m2.add(getMt3(); m2.add(getMt4(); ButtonGroup 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(
11、getMt7(); return m4; private JMenuItem getMt1() mt1=new JMenuItem( 复制 ); mt1.setFont(f); mt1.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_C,InputEvent.CTRL_MASK); mt1.setPreferredSize(new Dimension(130, 30); mt1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)
12、try if(card) if(bp.getTextField().getSelectedText().isEmpty() str=bp.getTextField().getText(); else str=bp.getTextField().getSelectedText(); catch(Exception tx) str=bp.getTextField().getText(); if(!str.isEmpty()&!str.equals(0) mt2.setEnabled(true); BPanel.jp2.setEnabled(true); ); return mt1; private
13、 JMenuItem getMt2() mt2=new JMenuItem( 粘贴 ); mt2.setFont(f); mt2.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_V,InputEvent.CTRL_MASK); mt2.setPreferredSize(new Dimension(130, 30); mt2.setEnabled(false); mt2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(ca
14、rd) bp.getTextField().setText(str); fz=true; /esle /kp.getTextField().setText(str); ); return mt2; private JRadioButtonMenuItem getMt3() mt3=new JRadioButtonMenuItem(标准计算器); mt3.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_B,InputEvent.CTRL_MASK); mt3.setFont(f); mt3.setPreferredSize(new Dimen
15、sion(130, 30); mt3.addItemListener(new ItemListener() public void itemStateChanged(ItemEvent e) c1.first(p1); ); return mt3; private JRadioButtonMenuItem getMt4() mt4=new JRadioButtonMenuItem(科学计算器); mt4.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_K,InputEvent.CTRL_MASK); mt4.setFont(f); mt4.
16、setPreferredSize(new Dimension(130, 30); mt4.addItemListener(new ItemListener() public void itemStateChanged(ItemEvent e) c1.last(p1); ); return mt4; private JMenuItem getMt6() mt6=new JMenuItem(关于计算器); mt6.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_G,InputEvent.CTRL_MASK); mt6.setFont(f); m
17、t6.setPreferredSize(new Dimension(130, 30); mt6.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) d1.setVisible(true); ); return mt6; private JMenuItem getMt7() mt7=new JMenuItem( 帮助 ); mt7.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_H,InputEvent.CTRL_MASK); mt
18、7.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.setTitle( 关于计算器 ); JDialog.setDefaultLookAndFeelDecorated(true); String s1=计算器由标准计算器和科学计算器
19、组成; 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(Color.pink); dp.setForeground(Color.pink); l1.setForeground(Color.blue); l1
20、.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 windowClosing(WindowEvent e) d1.setVisible(false); ); public void initDial
21、og2() 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(Color.pink); dp.setForeground(Color.pink); l1.se
22、tFont(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.addWindowListener(new WindowAdapter(
23、) 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;import java.awt.event.Actio
24、nEvent;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.swing.JPopupMenu;import javax
25、.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 s3=null; privat
26、e 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; privat
27、e 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
28、=false,qy=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(); initTextField(); this.setLayout(new BorderLayout(); this.add(North,tf); this.a