⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calcframe.java

📁 Java 语言实现的计算器
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package calcpackage;/** * * @author Yuyin Li */import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class CalcFrame implements ActionListener{ private CalcFunc Func=null; private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="10,7" private JPanel jContentPane = null; private JTextField jTextField = null; private JLabel jLabel = null; private JDesktopPane jDesktopPane = null; private JButton jButton_1 = null; private JButton jButton_2 = null; private JButton jButton_3 = null; private JButton jButton_4 = null; private JButton jButton_5 = null; private JButton jButton_6 = null; private JButton jButton_7 = null; private JButton jButton_8 = null; private JButton jButton_9 = null; private JButton jButton_0 = null; private JButton jButton_dot = null; private JButton jButton_Res = null; private JButton jButton_Add = null; private JButton jButton_Sub = null; private JButton jButton_Mul = null; private JButton jButton_Div = null; private JButton jButton_x2 = null; private JButton jButton_mod = null; private JButton jButton_1x = null; private JButton jButton_2x = null; private JButton jButton_sin = null; private JButton jButton_cos = null; private JButton jButton_tan = null; private JButton jButton_bp = null; private JButton jButton_ce = null; private JButton jButton_l = null; private JButton jButton_r = null; private JTextField jLeft = null; private JButton jButton_As = null;  public CalcFrame() {  getJFrame();  CalcMenu myMenu=new CalcMenu(jFrame);  myMenu.init();  Func=new CalcFunc(this.jLabel,this.jTextField,this.jLeft);  AddListener(); }  public JFrame getJFrame() {  if (jFrame == null) {   jFrame = new JFrame();   jFrame.setSize(new Dimension(444, 309));   jFrame.setTitle("计算器");   jFrame.setName("ClacFrame");   jFrame.setLocation(new Point(400, 200));   jFrame.setVisible(true);   jFrame.setResizable(false);   jFrame.setContentPane(getJContentPane());   jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  }  return jFrame; }  private JPanel getJContentPane() {  if (jContentPane == null) {   jLabel = new JLabel();   jLabel.setText("科学计算器");   jLabel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);   jLabel.setFont(new Font("Dialog", Font.BOLD, 12));   jLabel.setHorizontalAlignment(SwingConstants.CENTER);   jLabel.setName("DispText");   jContentPane = new JPanel();   jContentPane.setLayout(new BorderLayout());   jContentPane.add(getJTextField(), BorderLayout.NORTH);   jContentPane.add(jLabel, BorderLayout.SOUTH);   jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);     }  return jContentPane; }  private JTextField getJTextField() {  if (jTextField == null) {   jTextField = new JTextField();   jTextField.setName("Result");   jTextField.setFont(new Font("Dialog", Font.BOLD, 14));   jTextField.setHorizontalAlignment(JTextField.RIGHT);   jTextField.setText("0.0");   jTextField.setBackground(Color.white);   jTextField.setEditable(true);  }  return jTextField; }  private JDesktopPane getJDesktopPane() {  if (jDesktopPane == null) {   jDesktopPane = new JDesktopPane();   jDesktopPane.setBackground(new Color(238, 238, 238));   jDesktopPane.add(getJButton_1(), null);   jDesktopPane.add(getJButton_2(), null);   jDesktopPane.add(getJButton_3(), null);   jDesktopPane.add(getJButton_4(), null);   jDesktopPane.add(getJButton_5(), null);   jDesktopPane.add(getJButton_6(), null);   jDesktopPane.add(getJButton_7(), null);   jDesktopPane.add(getJButton_8(), null);   jDesktopPane.add(getJButton_9(), null);   jDesktopPane.add(getJButton_0(), null);   jDesktopPane.add(getJButton_dot(), null);   jDesktopPane.add(getJButton_Res(), null);   jDesktopPane.add(getJButton_Add(), null);   jDesktopPane.add(getJButton_Sub(), null);   jDesktopPane.add(getJButton_Mul(), null);   jDesktopPane.add(getJButton_Div(), null);   jDesktopPane.add(getJButton_x2(), null);   jDesktopPane.add(getjButton_mod(), null);   jDesktopPane.add(getJButton_1x(), null);   jDesktopPane.add(getJButton_2x(), null);   jDesktopPane.add(getJButton_sin(), null);   jDesktopPane.add(getJButton_cos(), null);   jDesktopPane.add(getJButton_tan(), null);   jDesktopPane.add(getJButton_bp(), null);   jDesktopPane.add(getJButton_ce(), null);   jDesktopPane.add(getJButton_l(), null);   jDesktopPane.add(getJButton_r(), null);   jDesktopPane.add(getJLeft(), null);   jDesktopPane.add(getJButton_As(), null);  }  return jDesktopPane; }  private JButton getJButton_1() {  if (jButton_1 == null) {   jButton_1 = new JButton();   jButton_1.setBounds(new Rectangle(15, 55, 46, 27));   jButton_1.setText("1");  }  return jButton_1; }  private JButton getJButton_2() {  if (jButton_2 == null) {   jButton_2 = new JButton();   jButton_2.setBounds(new Rectangle(75, 55, 46, 27));   jButton_2.setText("2");  }  return jButton_2; }  private JButton getJButton_3() {  if (jButton_3 == null) {   jButton_3 = new JButton();   jButton_3.setBounds(new Rectangle(135, 55, 46, 27));   jButton_3.setText("3");  }  return jButton_3; }  private JButton getJButton_4() {  if (jButton_4 == null) {   jButton_4 = new JButton();   jButton_4.setBounds(new Rectangle(15, 93, 46, 27));   jButton_4.setText("4");  }  return jButton_4; }  private JButton getJButton_5() {  if (jButton_5 == null) {   jButton_5 = new JButton();   jButton_5.setBounds(new Rectangle(75, 93, 46, 27));   jButton_5.setText("5");  }  return jButton_5; }  private JButton getJButton_6() {  if (jButton_6 == null) {   jButton_6 = new JButton();   jButton_6.setBounds(new Rectangle(135, 93, 46, 27));   jButton_6.setText("6");  }  return jButton_6; }  private JButton getJButton_7() {  if (jButton_7 == null) {   jButton_7 = new JButton();   jButton_7.setBounds(new Rectangle(15, 133, 46, 27));   jButton_7.setText("7");  }  return jButton_7; }  private JButton getJButton_8() {  if (jButton_8 == null) {   jButton_8 = new JButton();   jButton_8.setBounds(new Rectangle(75, 133, 46, 27));   jButton_8.setText("8");  }  return jButton_8; }  private JButton getJButton_9() {  if (jButton_9 == null) {   jButton_9 = new JButton();   jButton_9.setBounds(new Rectangle(135, 133, 46, 27));   jButton_9.setText("9");  }  return jButton_9; }  private JButton getJButton_0() {  if (jButton_0 == null) {   jButton_0 = new JButton();   jButton_0.setBounds(new Rectangle(15, 171, 46, 27));   jButton_0.setText("0");  }  return jButton_0; }  private JButton getJButton_dot() {  if (jButton_dot == null) {   jButton_dot = new JButton();   jButton_dot.setBounds(new Rectangle(75, 171, 46, 27));   jButton_dot.setText(".");  }  return jButton_dot; }  private JButton getJButton_Res() {  if (jButton_Res == null) {   jButton_Res = new JButton();   jButton_Res.setBounds(new Rectangle(135, 171, 46, 27));   jButton_Res.setText("=");  }  return jButton_Res; }  private JButton getJButton_Add() {  if (jButton_Add == null) {   jButton_Add = new JButton();   jButton_Add.setBounds(new Rectangle(195, 55, 46, 27));   jButton_Add.setText("+");  }  return jButton_Add; }  private JButton getJButton_Sub() {  if (jButton_Sub == null) {   jButton_Sub = new JButton();   jButton_Sub.setBounds(new Rectangle(195, 93, 46, 27));   jButton_Sub.setText("-");  }  return jButton_Sub; }  private JButton getJButton_Mul() {  if (jButton_Mul == null) {   jButton_Mul = new JButton();   jButton_Mul.setBounds(new Rectangle(195, 133, 46, 27));   jButton_Mul.setText("*");  }  return jButton_Mul; }  private JButton getJButton_Div() {  if (jButton_Div == null) {   jButton_Div = new JButton();   jButton_Div.setBounds(new Rectangle(195, 171, 46, 27));   jButton_Div.setText("/");  }  return jButton_Div; }  private JButton getJButton_x2() {  if (jButton_x2 == null) {   jButton_x2 = new JButton();   jButton_x2.setBounds(new Rectangle(286, 93, 60, 27));   jButton_x2.setText("x^2");  }  return jButton_x2; }  private JButton getjButton_mod() {  if (jButton_mod == null) {   jButton_mod = new JButton();   jButton_mod.setBounds(new Rectangle(358, 93, 60, 27));   jButton_mod.setText("sqrt");  }  return jButton_mod; }  private JButton getJButton_1x() {  if (jButton_1x == null) {   jButton_1x = new JButton();   jButton_1x.setBounds(new Rectangle(286, 133, 60, 27));   jButton_1x.setText("1/x");  }  return jButton_1x; }  private JButton getJButton_2x() {  if (jButton_2x == null) {   jButton_2x = new JButton();   jButton_2x.setText("sqrt");  }  return jButton_2x; }  private JButton getJButton_sin() {  if (jButton_sin == null) {   jButton_sin = new JButton();   jButton_sin.setText("sin");   jButton_sin.setBounds(new Rectangle(358, 171, 60, 27));  }  return jButton_sin; }  private JButton getJButton_cos() {  if (jButton_cos == null) {   jButton_cos = new JButton();   jButton_cos.setBounds(new Rectangle(358, 133, 60, 27));   jButton_cos.setText("cos");  }  return jButton_cos; }  private JButton getJButton_tan() {  if (jButton_tan == null) {   jButton_tan = new JButton();   jButton_tan.setBounds(new Rectangle(286, 171, 60, 27));   jButton_tan.setText("tan");  }  return jButton_tan; }  private JButton getJButton_bp() {  if (jButton_bp == null) {   jButton_bp = new JButton();   jButton_bp.setBounds(new Rectangle(15, 17,107, 27));   jButton_bp.setText("退出");  }  return jButton_bp; }  private JButton getJButton_ce() {  if (jButton_ce == null) {   jButton_ce = new JButton();   jButton_ce.setBounds(new Rectangle(285, 17, 134, 27));   jButton_ce.setText("清空");  }  return jButton_ce; }  private JButton getJButton_l() {  if (jButton_l == null) {   jButton_l = new JButton();   jButton_l.setBounds(new Rectangle(286, 55, 60, 27));   jButton_l.setText("(");  }  return jButton_l; }  private JButton getJButton_r() {  if (jButton_r == null) {   jButton_r = new JButton();   jButton_r.setBounds(new Rectangle(358, 55,  60, 27));   jButton_r.setText(")");  }  return jButton_r; } private void AddListener() {  jButton_1.addActionListener(this);  jButton_2.addActionListener(this);  jButton_3.addActionListener(this);  jButton_4.addActionListener(this);  jButton_5.addActionListener(this);  jButton_6.addActionListener(this);  jButton_7.addActionListener(this);  jButton_8.addActionListener(this);  jButton_9.addActionListener(this);  jButton_0.addActionListener(this);  jButton_Add.addActionListener(this);  jButton_Sub.addActionListener(this);  jButton_Mul.addActionListener(this);  jButton_Div.addActionListener(this);  jButton_dot.addActionListener(this);  jButton_Res.addActionListener(this);  jButton_sin.addActionListener(this);  jButton_cos.addActionListener(this);  jButton_tan.addActionListener(this);  jButton_x2.addActionListener(this);  jButton_mod.addActionListener(this);  jButton_1x.addActionListener(this);  jButton_2x.addActionListener(this);  jButton_bp.addActionListener(this);  jButton_ce.addActionListener(this);  jButton_l.addActionListener(this);  jButton_r.addActionListener(this);  jButton_As.addActionListener(this); }  private JTextField getJLeft() {  if (jLeft == null) {   jLeft = new JTextField();   jLeft.setBounds(new Rectangle(195, 17, 46, 27));   jLeft.setText("");   jLeft.setFont(new Font("Dialog", Font.BOLD, 14));   jLeft.setHorizontalAlignment(JTextField.CENTER);   jLeft.setEditable(false);  }  return jLeft; }  private JButton getJButton_As() {  if (jButton_As == null) {   jButton_As = new JButton();   jButton_As.setBounds(new Rectangle(135, 17, 46, 27));   jButton_As.setText("+-");  }  return jButton_As; } public void actionPerformed(ActionEvent e) {  String Command=e.getActionCommand();  this.jLabel.setText("输入...");  if(Command.equals("sin"))   Func.Sin();  else if(Command.equals("cos"))   Func.Cos();  else if(Command.equals("tan"))   Func.Tan();  else if(Command.equals("x^2"))   Func.X2();  else if(Command.equals("1/x"))   Func.X1();  else if(Command.equals("("))   Func.Left();  else if(Command.equals(")"))   Func.Right();  else if(Command.equals("sqrt"))   Func.Sqrt();  else if(Command.equals("清空"))   Func.Clear();  else if(Command.equals("退出"))   System.exit(0);  else if(Command.equals("="))   Func.Be();  else if(Command.equals("+-"))   Func.As();  else   Func.Input(Command); }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -