java计算器源代码_第1页
java计算器源代码_第2页
java计算器源代码_第3页
java计算器源代码_第4页
java计算器源代码_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

package 计算器;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.math.BigDecimal;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.border.BevelBorder;import javax.swing.text.BadLocationException;public class Counter1 extends JFrame implements ActionListener public static final int DEF_DIV_SCALE = 10; /默认的精度private JMenuBar jmBar = new JMenuBar(); / 菜单条private JMenu edit = new JMenu(“编辑(E)“);private JMenu find = new JMenu(“查看(V)“);private JMenu help = new JMenu(“帮助(H)“);/ 创建JPanel 便于管理private JPanel panTxt = new JPanel();private JPanel panBack = new JPanel();private JPanel panMen = new JPanel();private JPanel panNum = new JPanel();/ 创建文本域private JTextField txt = new JTextField();/ 创建文本标签private JLabel jlm = new JLabel();private boolean flag = false; / 是否可以消除文本private double a = 0; / 数字a的值private double b = 0; / 数字b的值private double result = 0; / 两个数字运算的结果private String sign = “; / 标记运算法则private double m = 0; / 暂时储存数据/ 创建按钮private NewButton jbBack = new NewButton(“Backspace“, Color.RED);private NewButton jbCE = new NewButton(“CE“, Color.RED);private NewButton jbC = new NewButton(“C“, Color.RED);private NewButton jbMC = new NewButton(“MC“, Color.RED);private NewButton jbMR = new NewButton(“MR“, Color.RED);private NewButton jbMS = new NewButton(“MS“, Color.RED);private NewButton jbM = new NewButton(“M+“, Color.RED);public Counter1() setSize(260, 260);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLocationRelativeTo(null);setLayout(null);setJMenuBar(jmBar);setTitle(“计算器“ );setResizable(false);init();/ Container contentPane = getContentPane();/ Controller c = new Controller(this);/ contentPane.addKeyListener(c);private void init() / 设置第一个JPanel里面的内容panTxt.setSize(230, 20);panTxt.setLocation(15, 5);/ panTxt.setBorder(BorderFactory.createLineBorder(Color.RED);panTxt.setLayout(new BorderLayout(); / 设置布局管理器txt.setHorizontalAlignment(JTextField.RIGHT); / 使光标定位在右边txt.setBorder(BorderFactory.createLoweredBevelBorder(); / 设置边框凹下去txt.setEditable(false); / 设置是否可以编辑txt.setBackground(Color.WHITE);/ txt.setEnabled(true); /设置是否激活/ txt.setFocusable(false); / 设置是否可以获得焦点(显示光标)txt.setText(“0“);add(panTxt);panTxt.add(txt);Controller c = new Controller(this);txt.addKeyListener(c);/ 设置第二个JPanel里面的内容panBack.setSize(190, 25);panBack.setLocation(52, 35);/ 画出边框便于查看/ panBack.setBorder(BorderFactory.createLineBorder(Color.RED);GridLayout layPanBack = new GridLayout(); / 创建布局管理器layPanBack.setHgap(5); / 设置布局管理器的边距panBack.setLayout(layPanBack); / 设置布局管理器/ 添加按钮jbBack.setFont(new Font(“宋体“ , 0, 12);panBack.add(jbBack);panBack.add(jbCE);panBack.add(jbC);add(panBack);/ 设置第三个JPanel里面的内容panMen.setSize(40, 140);panMen.setLocation(5, 65);GridLayout layPanMen = new GridLayout(4, 1); / 创建布局管理器layPanMen.setVgap(5); / 设置布局管理器的边距panMen.setLayout(layPanMen);/ panMen.setBorder(BorderFactory.createLineBorder(Color.RED);panMen.add(jbMC);panMen.add(jbMR);panMen.add(jbMS);panMen.add(jbM);add(panMen);/ 设置第四个JPanel里面的内容panNum.setSize(200, 140);panNum.setLocation(50, 65);/ panNum.setBorder(BorderFactory.createLineBorder(Color.RED);add(panNum);GridLayout layPanNum = new GridLayout(4, 5); / 创建布局管理器panNum.setLayout(layPanNum);newButtonNum();/ 添加菜单项jmBar.add(edit);jmBar.add(find);jmBar.add(help);/ 设置文本标签的属性和添加文本标签jlm.setBorder(BorderFactory.createBevelBorder(1);jlm.setSize(25, 25);jlm.setLocation(15, 35);add(jlm);public void newButtonNum() NewButton jb7 = new NewButton(“7“, Color.BLUE);NewButton jb8 = new NewButton(“8“, Color.BLUE);NewButton jb9 = new NewButton(“9“, Color.BLUE);NewButton jbDivide = new NewButton(“/“, Color.RED);NewButton jbSqrt = new NewButton(“sqrt“, Color.BLUE);NewButton jb4 = new NewButton(“4“, Color.BLUE);NewButton jb5 = new NewButton(“5“, Color.BLUE);NewButton jb6 = new NewButton(“6“, Color.BLUE);NewButton jbRide = new NewButton(“*“, Color.RED);NewButton jbPer = new NewButton(“%“, Color.BLUE);NewButton jb1 = new NewButton(“1“, Color.BLUE);NewButton jb2 = new NewButton(“2“, Color.BLUE);NewButton jb3 = new NewButton(“3“, Color.BLUE);NewButton jbCut = new NewButton(“-“, Color.RED);NewButton jbReciprocal = new NewButton(“1/x“, Color.BLUE);NewButton jb0 = new NewButton(“0“, Color.BLUE);NewButton jbMinus = new NewButton(“+/-“, Color.BLUE);NewButton jbDot = new NewButton(“.“, Color.BLUE);NewButton jbAdd = new NewButton(“+“, Color.RED);NewButton jbEquals = new NewButton(“=“, Color.RED);panNum.add(jb7);panNum.add(jb8);panNum.add(jb9);panNum.add(jbDivide);panNum.add(jbSqrt);panNum.add(jb4);panNum.add(jb5);panNum.add(jb6);panNum.add(jbRide);panNum.add(jbPer);panNum.add(jb1);panNum.add(jb2);panNum.add(jb3);panNum.add(jbCut);panNum.add(jbReciprocal);panNum.add(jb0);panNum.add(jbMinus);panNum.add(jbDot);panNum.add(jbAdd);panNum.add(jbEquals);public static void main(String args) try UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName(); / 设置和win7的布局一样 catch (ClassNotFoundException e) e.printStackTrace(); catch (InstantiationException e) e.printStackTrace(); catch (IllegalAccessException e) e.printStackTrace(); catch (UnsupportedLookAndFeelException e) e.printStackTrace();new Counter1().setVisible(true);private class NewButton extends JButton public NewButton(String s, Color c) super(s);/ setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED);setBorder(BorderFactory.createEmptyBorder();setForeground(c);setFocusable(false); / 设置是否可以获得焦点(显示光标)addActionListener(Counter1.this);/ 实现监听public void actionPerformed(ActionEvent e) if (e.getActionCommand() = “+“) a = Double.parseDouble(txt.getText();sign = “+“;flag = true; else if (e.getActionCommand() = “-“) a = Double.parseDouble(txt.getText();sign = “-“;flag = true; else if (e.getActionCommand() = “*“) a = Double.parseDouble(txt.getText();sign = “*“;flag = true; else if (e.getActionCommand() = “/“) a = Double.parseDouble(txt.getText();sign = “/“;flag = true; else if (e.getActionCommand() = “=“) try b = Double.parseDouble(txt.getText(); catch (NumberFormatException nfe) b = 0;try if (sign = “+“) result = doubleAdd(a, b); else if (sign = “-“) result = doubleSub(a, b); else if (sign = “*“) result = doubleMul(a, b); else if (sign = “/“ else result = b; catch (NumberFormatException nfe) result = 0;a = result;if (sign = “/“ else txt.setText(result + “);flag = true; else if (e.getActionCommand() = “C“) a = 0;result = 0;sign = “;txt.setText(“0“); else if (e.getActionCommand() = “CE“) txt.setText(“0“); else if (e.getActionCommand() = “Backspace“) try if (txt.getText().length() 1) txt.setText(txt.getText(0, txt.getText().length() - 1); else txt.setText(“0“); catch (BadLocationException e1) txt.setText(“0“); else if (e.getActionCommand() = “sqrt“) a = Double.parseDouble(txt.getText();result = Math.sqrt(a);a = result;txt.setText(result + “);flag = true; else if (e.getActionCommand() = “%“) b = Double.parseDouble(txt.getText();result = doubleMul(doubleDiv(b, 100), a);txt.setText(result + “);flag = true; else if (e.getActionCommand() = “1/x“) a = Double.parseDouble(txt.getText();result = 1 / a;a = result;txt.setText(result + “);flag = true; else if (e.getActionCommand() = “+/-“) txt.setText(“ + (-Double.parseDouble(txt.getText(); else if (e.getActionCommand() = “MC“) m = 0;jlm.setText(“); else if (e.getActionCommand() = “MR“) txt.setText(m + “); else if (e.getActionCommand() = “MS“) m = Double.parseDouble(txt.getText();jlm.setText(“ M“);flag = true; else if (e.getActionCommand() = “M+“) m += Double.parseDouble(txt.getText();flag = true; else boolean isDoc = false;for (int i = 0; i 1) txt.setText(txt.getText(0, txt.getText().length() - 1); else txt.setText(“0“); catch (BadLocationException e1) txt.setText(“0“); else if (e.getKeyChar() = %) b = Double.parseDouble(txt.getText();result = doubleMul(doubleDiv(b, 100), a);txt.setText(result + “);flag = true; else if (e.getKeyChar() = 0x30 for (int i = 0; i txt.getText().length(); i+) if (txt.getText().charAt(i) = .) isDoc = true;if (!isDoc | e.getKeyChar() != .) if (flag) txt.setText(“);flag = false;if (txt.getText().equals(“0“) txt.setText(txt.getText() + e.getKeyChar();private class Controller extends KeyAdapter private Counter1 frame;public Controller(Counter1 frame) this.frame = frame;public void keyPressed(KeyEvent e) frame.keyAction(e);/ 提供相对精确的加法运算public static double doubleAdd(double d1, double d2) BigDecimal bd1 = new BigDecimal(Double.toString(d1);BigDecimal bd2 = new BigDecimal(Double.toString(d2);retur

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论