1、import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JRadioButtonMenuItem;import javax.swing.JTextField;public class Calucator extends JFrame private JTextField tf; private JPanel panel1, panel2, panel3, panel4; private JMenuBar myBar; private JMenu m
2、enu1, menu2, menu3; private JMenuItem editItem1, editItem2, help1, help2, help3; private JRadioButtonMenuItem seeItem1, seeItem2;/单选框 private JCheckBoxMenuItem seeItem3;/复选框 private ButtonGroup bgb; private String back; private boolean IfResult = true, flag = false; private String oper = =; private
3、double result = 0; private Num numActionListener; private DecimalFormat df; public Calucator() super(科学计算器);/设置标题栏 df = new DecimalFormat(#.#/保留四位小数 this.setLayout(new BorderLayout(10, 5); panel1 = new JPanel(new GridLayout(1, 3, 10, 10); panel2 = new JPanel(new GridLayout(5, 6, 5, 5);/5行6列 panel3 =
4、 new JPanel(new GridLayout(5, 1, 5, 5); panel4 = new JPanel(new BorderLayout(5, 5); /* * 菜单栏 */ myBar = new JMenuBar(); menu1 = new JMenu(编辑(E) menu2 = new JMenu(查看(V) menu3 = new JMenu(帮助(H) menu1.setFont(new Font(宋体, Font.PLAIN, 12); menu2.setFont(new Font( menu3.setFont(new Font( * 编辑栏 editItem1
5、= new JMenuItem(复制(C) Ctrl+C editItem2 = new JMenuItem(粘贴(P) Ctrl+V editItem1.setFont(new Font(,Font.PLAIN,12); editItem2.setFont(new Font( * 查看栏 seeItem1 = new JRadioButtonMenuItem(科学型(T) seeItem2 = new JRadioButtonMenuItem(标准型(S) seeItem3 = new JCheckBoxMenuItem(数字分组(I) seeItem1.setFont(new Font(
6、seeItem2.setFont(new Font( seeItem3.setFont(new Font( * 帮助栏 help1 = new JMenuItem(帮助主题(H) help2 = new JMenuItem(关于计算器(A) help1.setFont(new Font( help2.setFont(new Font( bgb = new ButtonGroup();/选项组 menu1.add(editItem1); menu1.add(editItem2); menu2.add(seeItem1); menu2.add(seeItem2); menu2.addSeparat
7、or();/添加一条分割线 menu2.add(seeItem3); menu3.add(help1); menu3.addSeparator(); menu3.add(help2); myBar.add(menu1); myBar.add(menu2); myBar.add(menu3); this.setJMenuBar(myBar); numActionListener = new Num();/实现数字监听 * 文本域,即为计算器的屏幕显示区域 tf = new JTextField(); tf.setEditable(false);/文本区域不可编辑 tf.setBackground
8、(Color.white);/文本区域的背景色 tf.setHorizontalAlignment(JTextField.RIGHT);/文字右对齐 tf.setText(0 tf.setBorder(BorderFactory.createLoweredBevelBorder(); init();/对计算器进行初始化 /* * 初始化操作 * 添加按钮 */ private void init() addButton(panel1, Backspace, new Clear(), Color.red);CEC addButton(panel2, 1/x, new Signs(), Color.magenta);log7, numActionListener, Color.blue);89, new Signs(), Color.red);n!sqrt456sinx2123-cosx3-/+, new Clear(), Color.blue);., new Dot(), Color.blue);+tan%, numActionListener, Color.orange); addBut
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1