import java.awt.BorderLayout;import java.awt.Container;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JPanel;import javax.swing.JTextField;public class Calculator extends JFrame implements ActionListener { boolean init = true; boolean isMath = false; boolean clear = true; boolean clickable = true; double qian; String fuhao; int all = 0; JTextField text = new JTextField(25); JButton bM = new JButton(); JButton bMC = new JButton("MC"); JButton bMR = new JButton("MR"); JButton bMS = new JButton("MS"); JButton bMADD = new JButton("M+"); JButton b0 = new JButton("0"); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); JButton b5 = new JButton("5"); JButton b6 = new JButton("6"); JButton b7 = new JButton("7"); JButton b8 = new JButton("8"); JButton b9 = new JButton("9"); JButton bNOP = new JButton("+/-"); JButton bDot = new JButton("."); JButton bDiv = new JButton("/"); JButton bMul = new JButton("*"); JButton bSub = new JButton("-"); JButton bAdd = new JButton("+"); JButton bSprt = new JButton("sprt"); JButton bMod = new JButton("%"); JButton bDao = new JButton("1/x"); JButton bEqual = new JButton("="); JButton bBackspace = new JButton("Backspace"); JButton bCE = new JButton("CE"); JButton bC = new JButton("C");
public Calculator() { this.setTitle("计算器"); JMenuBar mainMenu = new JMenuBar(); setJMenuBar(mainMenu); JMenu editMenu = new JMenu("编辑"); JMenu viewMenu = new JMenu("查看"); JMenu helpMenu = new JMenu("帮助"); mainMenu.add(editMenu); mainMenu.add(viewMenu); mainMenu.add(helpMenu);
JPanel jpDisplay = new JPanel(); JPanel jpInput = new JPanel(); JPanel jpLeft = new JPanel(); JPanel jpRight = new JPanel();
text.setText("0."); text.setHorizontalAlignment(JTextField.RIGHT); jpDisplay.add(text);
bM.addActionListener(this); bMC.addActionListener(this); bMS.addActionListener(this); bMR.addActionListener(this); bMADD.addActionListener(this);
jpLeft.setLayout(new GridLayout(5, 1)); jpLeft.add(bM); jpLeft.add(bMC); jpLeft.add(bMR); jpLeft.add(bMS); jpLeft.add(bMADD);
JPanel jpInnerN = new JPanel(); JPanel jpInnerS = new JPanel();
bBackspace.addActionListener(this); bCE.addActionListener(this); bC.addActionListener(this);
jpInnerN.setLayout(new GridLayout(1, 3)); jpInnerN.add(bBackspace); jpInnerN.add(bCE); jpInnerN.add(bC);
b0.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); bNOP.addActionListener(this); bDot.addActionListener(this); bDiv.addActionListener(this); bMul.addActionListener(this); bSub.addActionListener(this); bAdd.addActionListener(this); bSprt.addActionListener(this); bMod.addActionListener(this); bDao.addActionListener(this); bEqual.addActionListener(this);
public Calculator() { this.setTitle("计算器"); JMenuBar mainMenu = new JMenuBar(); setJMenuBar(mainMenu); JMenu editMenu = new JMenu("编辑"); JMenu viewMenu = new JMenu("查看"); JMenu helpMenu = new JMenu("帮助"); mainMenu.add(editMenu); mainMenu.add(viewMenu); mainMenu.add(helpMenu);
JPanel jpDisplay = new JPanel(); JPanel jpInput = new JPanel(); JPanel jpLeft = new JPanel(); JPanel jpRight = new JPanel();
text.setText("0."); text.setHorizontalAlignment(JTextField.RIGHT); jpDisplay.add(text);
bM.addActionListener(this); bMC.addActionListener(this); bMS.addActionListener(this); bMR.addActionListener(this); bMADD.addActionListener(this);
jpLeft.setLayout(new GridLayout(5, 1)); jpLeft.add(bM); jpLeft.add(bMC); jpLeft.add(bMR); jpLeft.add(bMS); jpLeft.add(bMADD);
JPanel jpInnerN = new JPanel(); JPanel jpInnerS = new JPanel();
bBackspace.addActionListener(this); bCE.addActionListener(this); bC.addActionListener(this);
jpInnerN.setLayout(new GridLayout(1, 3)); jpInnerN.add(bBackspace); jpInnerN.add(bCE); jpInnerN.add(bC);
b0.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); bNOP.addActionListener(this); bDot.addActionListener(this); bDiv.addActionListener(this); bMul.addActionListener(this); bSub.addActionListener(this); bAdd.addActionListener(this); bSprt.addActionListener(this); bMod.addActionListener(this); bDao.addActionListener(this); bEqual.addActionListener(this);