Java设计计算器

么么哒狂人 2021-09-18 16:36 233 次浏览 赞 135

最新问答

  • 济南别墅装修

    ****************************************************************

    写了一天,终于写完了。

    ****************************************************************

    说明:

    类Calc.java出了界面符合你的要求外,还有以下优点:

    1. 对输入内容进行校验,如果是非法字符(如字母),不响应键盘,而且单个文本框只能输入一个小数点;

    2. 使用Windows界面风格;

    3. 采用大数计算,更精准;

    4. 其它……

    ****************************************************************

    代如下:

    ****************************************************************

    import java.awt.BorderLayout;

    import java.awt.Dimension;

    import java.awt.FlowLayout;

    import java.awt.Toolkit;

    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.JButton;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JMenu;

    import javax.swing.JMenuBar;

    import javax.swing.JMenuItem;

    import javax.swing.JOptionPane;

    import javax.swing.JPanel;

    import javax.swing.JTextField;

    import javax.swing.UIManager;

    /**

    * @author Godwin

    * @version 2010-05-16

    */

    public class Calc extends JFrame implements ActionListener {

    JTextField number1Text;

    JTextField number2Text;

    JTextField resultText;

    public Calc() {

    this.setTitle("计算器");

    /*

    * 菜单栏

    */

    // Operation菜单

    JMenuItem addMenuItem = new JMenuItem("Add");

    addMenuItem.setMnemonic('a');

    JMenuItem subtractMenuItem = new JMenuItem("Subtract");

    subtractMenuItem.setMnemonic('s');

    JMenuItem multiplyMenuItem = new JMenuItem("Multiply");

    multiplyMenuItem.setMnemonic('m');

    JMenuItem divideMenuItem = new JMenuItem("Divide");

    divideMenuItem.setMnemonic('d');

    JMenu operationFile = new JMenu("Operation");

    operationFile.setMnemonic('o');

    operationFile.add(addMenuItem);

    operationFile.add(subtractMenuItem);

    operationFile.add(multiplyMenuItem);

    operationFile.add(divideMenuItem);

    // Exit菜单

    JMenu exitMenu = new JMenu("Exit");

    exitMenu.setMnemonic('x');

    JMenuItem exitItem = new JMenuItem("Exit");

    exitItem.setMnemonic('x');

    exitItem.addActionListener(new ExitActionListener());

    exitMenu.add(exitItem);

    // Operation和Exit菜单

    JMenuBar menubar = new JMenuBar();

    menubar.add(operationFile);

    menubar.add(exitMenu);

    /*

    * 作数和结果

    */

    JLabel number1Label = new JLabel("Number 1");

    number1Text = new JTextField(6);

    JLabel number2Label = new JLabel("Number 2");

    number2Text = new JTextField(6);

    JLabel resultLabel = new JLabel("Result");

    resultText = new JTextField(10);

    resultText.setEditable(false);

    JPanel operandPanel = new JPanel(new FlowLayout());

    operandPanel.add(number1Label);

    operandPanel.add(number1Text);

    operandPanel.add(number2Label);

    operandPanel.add(number2Text);

    operandPanel.add(resultLabel);

    operandPanel.add(resultText);

    /*

    * 作符

    */

    JButton addButton = new JButton("Add");

    JButton subtractButton = new JButton("Subtract");

    JButton multiplyButton = new JButton("Multiply");

    JButton divideButton = new JButton("Divide");

    JPanel operatorPanel = new JPanel(new FlowLayout());

    operatorPanel.add(addButton);

    operatorPanel.add(subtractButton);

    operatorPanel.add(multiplyButton);

    operatorPanel.add(divideButton);

    /*

    * 所有面板

    */

    this.add(menubar, BorderLayout.NORTH);

    this.add(operandPanel, BorderLayout.CENTER);

    this.add(operatorPanel, BorderLayout.SOUTH);

    this.pack();

    this.setResizable(false);

    Toolkit tool = Toolkit.getDefaultToolkit();

    Dimension screen = tool.getScreenSize();

    this.setLocation(screen.width / 2 - this.getWidth() / 2, screen.height

    / 2 - this.getHeight() / 2);

    this.setVisible(true);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    /*

    *

    */

    // 菜单

    addMenuItem.addActionListener(this);

    subtractMenuItem.addActionListener(this);

    multiplyMenuItem.addActionListener(this);

    divideMenuItem.addActionListener(this);

    // 文本框

    number1Text.addKeyListener(new VerifyInputKeyAdapter());

    number2Text.addKeyListener(new VerifyInputKeyAdapter());

    // 按钮

    addButton.addActionListener(this);

    subtractButton.addActionListener(this);

    multiplyButton.addActionListener(this);

    divideButton.addActionListener(this);

    }

    class VerifyInputKeyAdapter extends KeyAdapter {

    public void keyTyped(KeyEvent e) {

    char c = e.getKeyChar();

    System.out.println(c);

    if (!(('0' <= c) && (c <= '9') || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_BACK_SPACE))) {

    if (!((!((JTextField) (e.getSource())).getText().contains(".")) && (c == '.'))) {

    e.consume();

    }

    }

    }

    }

    class ExitActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

    System.exit(0);

    }

    }

    public void actionPerformed(ActionEvent e) {

    String operand1 = number1Text.getText();

    BigDecimal big1 = BigDecimal.valueOf(Double.valueOf(operand1));

    String operand2 = number2Text.getText();

    BigDecimal big2 = BigDecimal.valueOf(Double.valueOf(operand2));

    if ((!(operand1.equals(""))) && (operand1 != null)

    && (!(operand2.equals(""))) && (operand2 != null)) {

    String name = e.getSource().getClass().getSimpleName();

    String operator = "";

    if (name.equals("JButton")) {

    JButton b = (JButton) (e.getSource());

    operator = b.getText();

    } else if (name.equals("JMenuItem")) {

    JMenuItem m = (JMenuItem) e.getSource();

    operator = m.getText();

    }

    if (operator.equals("Add")) {

    resultText.setText(String.valueOf(big1.add(big2)));

    } else if (operator.equals("Subtract")) {

    resultText.setText(String.valueOf(big1.subtract(big2)));

    } else if (operator.equals("Multiply")) {

    resultText.setText(String.valueOf(big1.multiply(big2)));

    } else if (operator.equals("Divide")) {

    resultText.setText(String.valueOf(big1.divide(big2)));

    } else {

    JOptionPane.showMessageDialog(this, "作有误!", "错误提示",

    JOptionPane.WARNING_MESSAGE);

    }

    } else {

    JOptionPane.showMessageDialog(this, "请输入作数!", "错误提示",

    JOptionPane.WARNING_MESSAGE);

    number1Text.requestFocus();

    }

    }

    public static void main(String[] args) throws Exception {

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    new Calc();

    }

    }

    ****************************************************************

    运行结果如下:

    浏览 239赞 73时间 2023-07-01
  • 游客小孩儿

    我晕,谁会为你写这么一堆,而且很没意义的代?
    ……

    浏览 215赞 112时间 2023-04-10
  • 潘潘吃吃吃啊

    先等着吧,写好了再给你
    有邮箱没??写好了发给你

    浏览 263赞 130时间 2022-06-11
  • wangxinrose

    就这么点要求都100分啊,太简单了

    浏览 210赞 55时间 2022-03-30

Java设计计算器