1、 import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea;import javax.swing.JTextField; public class jsq implements ActionListener /导入动作监听接口 /设计面板中的单位 JFrame frame;/窗口 JTextField text
2、Answer;/文本 JPanel panel, panel1, panel2, panel3;/面版 JMenuBar mainMenu;/菜单 JTextField textMemory; JLabel labelMemSpace; /labelMemSpace单纯做摆设,控制面板的形状 JButton buttonBk, buttonCe, buttonC; JButton button;/按钮数组 JButton buttonMC, buttonMR, buttonMS, buttonMAdd;/清除,使用,储存,添加 JButton buttonDot, buttonAddAndSu
3、b, buttonAdd, buttonSub, buttonMul, buttonDiv, buttonMod; JButton buttonSqrt, buttonDao, buttonEqual; JMenu editMenu, viewMenu, helpMenu; JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp, aboutCal; DecimalFormat df; /设置数据输出精度 boolean clickable; /控制当前能否按键 double memoryd; /使用内存中存储的数字
4、int memoryi; double vard, answerd; /用来保存double型数据的中间值(vard)和最后结果(answerd) short key = -1, prekey = -1; /key用来保存当前进行何种运算,prekey用来保存前次进行何种运算 String copy; /做复制用 JTextArea help; /帮助 JScrollPane scrollHelp; /构造函数 public jsq() clickable = true; answerd = 0; frame = new JFrame(计算器); df = new DecimalFormat(
5、0.# /设置数据输出精度(对于double型值) textAnswer = new JTextField(15); textAnswer.setText( textAnswer.setEditable(false); textAnswer.setBackground(new Color(255, 255, 255); panel = new JPanel(); frame.getContentPane().add(panel); panel1 = new JPanel(); panel2 = new JPanel(); panel.setLayout(new BorderLayout();
6、/设计整个面板 mainMenu = new JMenuBar(); editMenu =new JMenu(编辑(E) editMenu .setMnemonic(KeyEvent.VK_E); viewMenu =new JMenu(查看(V) viewMenu .setMnemonic(KeyEvent.VK_V); helpMenu =new JMenu(帮助(H) helpMenu .setMnemonic(KeyEvent.VK_H); mainMenu .add(editMenu ); mainMenu .add(viewMenu ); mainMenu .add(helpMen
7、u ); panel.add(mainMenu, BorderLayout.NORTH); panel.add(textAnswer, BorderLayout.CENTER); panel.add(panel1, BorderLayout.SOUTH); panel1.setLayout(new BorderLayout(); textMemory = new JTextField(3); textMemory.setEditable(false); textMemory.setBackground(new Color(217, 217, 217); labelMemSpace = new
8、JLabel( buttonBk = new JButton(Backspace buttonBk.setForeground(new Color(255, 0, 0); buttonCe = new JButton(CE buttonCe.setForeground(new Color(255, 0, 0); buttonC = new JButton(C buttonC.setForeground(new Color(255, 0, 0); buttonBk.addActionListener(this); buttonCe.addActionListener(this); buttonC
9、.addActionListener(this); panel1.add(panel2, BorderLayout.NORTH); panel2.setLayout(new FlowLayout(FlowLayout.RIGHT); panel2.add(textMemory); panel2.add(labelMemSpace); panel2.add(buttonBk); panel2.add(buttonCe); panel2.add(buttonC); panel3 = new JPanel(); panel1.add(panel3, BorderLayout.CENTER); but
10、ton = new JButton10; for (int i = 0; i button.length; i+) buttoni = new JButton(Integer.toString(i); buttoni.setForeground(new Color(0, 0, 255); /构造按钮 buttonMC = new JButton(MC buttonMC.setForeground(new Color(255, 0, 0); buttonMR = new JButton(MR buttonMR.setForeground(new Color(255, 0, 0); buttonM
11、S = new JButton(MS buttonMS.setForeground(new Color(255, 0, 0); buttonMAdd = new JButton(M+ buttonMAdd.setForeground(new Color(255, 0, 0); buttonDot = new JButton(. buttonDot.setForeground(new Color(0, 0, 255); buttonAddAndSub = new JButton(+/- buttonAddAndSub.setForeground(new Color(0, 0, 255); but
12、tonAdd = new JButton(+ buttonAdd.setForeground(new Color(255, 0, 0); buttonSub = new JButton(- buttonSub.setForeground(new Color(255, 0, 0); buttonMul = new JButton(* buttonMul.setForeground(new Color(255, 0, 0); buttonDiv = new JButton(/ buttonDiv.setForeground(new Color(255, 0, 0); buttonMod = new
13、 JButton(% buttonMod.setForeground(new Color(0, 0, 255); buttonSqrt = new JButton(sqrt buttonSqrt.setForeground(new Color(0, 0, 255); buttonDao = new JButton(1/x buttonDao.setForeground(new Color(0, 0, 255); buttonEqual = new JButton(= buttonEqual.setForeground(new Color(255, 0, 0); /将所有行为与监听绑定 pane
14、l3.setLayout(new GridLayout(4, 6); panel3.add(buttonMC); buttonMC.addActionListener(this); panel3.add(button7); button7.addActionListener(this); panel3.add(button8); button8.addActionListener(this); panel3.add(button9); button9.addActionListener(this); panel3.add(buttonDiv); buttonDiv.addActionListe
15、ner(this); panel3.add(buttonSqrt); buttonSqrt.addActionListener(this); panel3.add(buttonMR); buttonMR.addActionListener(this); panel3.add(button4); button4.addActionListener(this); panel3.add(button5); button5.addActionListener(this); panel3.add(button6); button6.addActionListener(this); panel3.add(
16、buttonMul); buttonMul.addActionListener(this); panel3.add(buttonMod); buttonMod.addActionListener(this); panel3.add(buttonMS); buttonMS.addActionListener(this); panel3.add(button1); button1.addActionListener(this); panel3.add(button2); button2.addActionListener(this); panel3.add(button3); button3.ad
17、dActionListener(this); panel3.add(buttonSub); buttonSub.addActionListener(this); panel3.add(buttonDao); buttonDao.addActionListener(this); panel3.add(buttonMAdd); buttonMAdd.addActionListener(this); panel3.add(button0); button0.addActionListener(this); panel3.add(buttonAddAndSub); buttonAddAndSub.ad
18、dActionListener(this); panel3.add(buttonDot); buttonDot.addActionListener(this); panel3.add(buttonAdd); buttonAdd.addActionListener(this); panel3.add(buttonEqual); buttonEqual.addActionListener(this); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.show(); /设置各个按钮行为 public
19、void actionPerformed(ActionEvent event) boolean sign = false; /判断是否是double型数参与运算,是为true,不是为false Object temp = event.getSource(); try /如果按下数据按钮,将按下的按钮代表的数据插入的当前文本框字符串之后= 9; i+) if (temp = buttoni & clickable = true) textAnswer.setText(textAnswer.getText() + Integer.toString(i); /按下按钮时,判断当前文本框内字符串中含不
20、含,如果已含,则不允许再插入 if (temp = buttonDot & clickable = true) boolean isDot = false; if (textAnswer.getText().length() = 0) isDot = true; textAnswer.getText().length(); if (. = textAnswer.getText().charAt(i) break; if (isDot = false) textAnswer.setText(textAnswer.getText() + if (temp = buttonAdd | temp =
21、buttonSub | temp = buttonMul | temp = buttonDiv) & /操作 if (temp = buttonAdd) switch (prekey) case 0: answerd += Double.parseDouble(textAnswer.getText(); case 1: answerd -= Double.parseDouble(textAnswer.getText(); case 2: answerd *= Double.parseDouble(textAnswer.getText(); case 3: if (Double.parseDou
22、ble(textAnswer.getText() = 0) /textAnswer=aa;除数不能为零 clickable = false; else answerd /= Double.parseDouble(textAnswer.getText(); default: answerd = Double.parseDouble(textAnswer.getText(); prekey = key = 0; if (temp = buttonSub) prekey = key = 1; if (temp = buttonMul) /textAnser = prekey = key = 2; if (temp = buttonDiv) prekey = key = 3;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1