【问题标题】:Wanted advice and comments on A Java Calculator of making code better readable, Useable or coding tips what I could Change or keep [closed]想要关于使代码更好地可读、可用或编码提示的 Java 计算器的建议和评论我可以更改或保留的内容 [关闭]
【发布时间】:2011-08-27 07:29:43
【问题描述】:

希望获得有关 A Java Calculator 的建议和 cmets,以使代码更具可读性、可用性或编码提示我可以更改或保留的内容。

这是我的主要课程

package calculator;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

 /**
*
* @author abdimaden
*/
 public class Calculator extends JFrame implements ActionListener {

/**
 * @param args the command line arguments
 */
public static JTextField display;
JPanel displayPanel = new JPanel();
JButton numb;
JButton opButton;
boolean userIsInTheMiddleOfTyping;
CalculatorBrain brain = new CalculatorBrain();

@Override
public void setFont(Font font) {
    super.setFont(font);
}

public static void main(String[] args) {
    // TODO code application logic here

    Calculator calc = new Calculator();

}

public Calculator() {
    setLayout(new FlowLayout());
    setSize(500, 500);
    setTitle("Calculator: By Cabdifitaar Aden (CMProductions)");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    Layout();
    add(displayPanel);

    setVisible(true);
}

public void Layout() {

    displayPanel.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();

    //display

    display = new JTextField("0", SwingConstants.RIGHT);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 5;
    displayPanel.add(display, c);
    display.setFont(new Font("arial", Font.BOLD, 36));
    display.setEditable(false);
    display.setHorizontalAlignment(JTextField.RIGHT);
    display.setPreferredSize(new Dimension(500, 30));
    display.setBackground(Color.LIGHT_GRAY);
    //display.setHorizontalTextPosition(JLabel.RIGHT_ALIGNMENT);
    display.addActionListener(this);

    //operation buttons first row MC M+ M- MR
    opButton = new JButton("MC");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    opButton = new JButton("M+");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 1;
    c.gridy = 1;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    opButton = new JButton("M-");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 1;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    opButton = new JButton("MR");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 3;
    c.gridy = 1;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    //operation buttons second C +/- / *
    opButton = new JButton("C");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    opButton = new JButton("+/-");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 1;
    c.gridy = 2;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("specialOperation");

    opButton = new JButton("/");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 2;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("operation");

    opButton = new JButton("*");
    opButton.addActionListener(this);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 3;
    c.gridy = 2;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.setActionCommand("operation");

    // buttons third row 7 8 9 -

    numb = new JButton("7");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 3;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("8");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 1;
    c.gridy = 3;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("9");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 3;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    opButton = new JButton("-");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 3;
    c.gridy = 3;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    // buttons third row 4 5 6 +
    opButton.setActionCommand("operation");

    numb = new JButton("4");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 4;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("5");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 1;
    c.gridy = 4;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("6");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 4;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    opButton = new JButton("+");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 3;
    c.gridy = 4;
    c.gridwidth = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("operation");
    // buttons third row 1 2 3 =

    numb = new JButton("1");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 5;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("2");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 1;
    c.gridy = 5;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    numb = new JButton("3");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 5;
    c.gridwidth = 1;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    opButton = new JButton("=");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 90;
    c.gridx = 3;
    c.gridy = 5;
    c.gridwidth = 1;
    c.gridheight = 3;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.addActionListener(this);
    opButton.setActionCommand("operation");

    /// last row 0 . 
    numb = new JButton("0");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 90;
    c.ipady = 30;
    c.gridx = 0;
    c.gridy = 6;
    c.gridwidth = 2;
    displayPanel.add(numb, c);
    numb.setFont(new Font("arial", Font.BOLD, 20));
    numb.addActionListener(this);
    numb.setActionCommand("digit");

    opButton = new JButton(".");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipadx = 30;
    c.ipady = 30;
    c.gridx = 2;
    c.gridy = 6;
    c.gridwidth = 1;
    c.gridheight = 1;
    displayPanel.add(opButton, c);
    opButton.setFont(new Font("arial", Font.BOLD, 20));
    opButton.setActionCommand("operation");
    // numb.addActionListener(this);
    //opButton.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
    // add your event handling code here

    // only the digit buttons will trigger the fallow if statment 
    if ("digit".equals(e.getActionCommand())) {
        //see which button trigged action    
        JButton digit = (JButton) e.getSource();
        //get the text from the button
        String digitPressed = digit.getText();
        if (userIsInTheMiddleOfTyping) {
            //display.setText(digitPressed);                    
            display.setText(display.getText() + digitPressed);
        } else {
            display.setText(digitPressed);
            userIsInTheMiddleOfTyping = true;
        }

    } else if ("operation".equals(e.getActionCommand())) {
        JButton button = (JButton) e.getSource();

        if (userIsInTheMiddleOfTyping) {
            //gets tricked when an operation button is pressed
            brain.setOperand(Integer.parseInt(display.getText()));
            userIsInTheMiddleOfTyping = false;

        }
        String operation = button.getText();

        double result = brain.perforumOperation(operation);
        String newResult = Double.toString(result);
        brain.formatDisplay(newResult);


    } else if ("specialOperation".equals(e.getActionCommand())) {
        //see which button trigged action    
        JButton specialOperationButton = (JButton) e.getSource();
        //get the text from the button
        String specialOperation = specialOperationButton.getText();

        brain.performSpecialOperation(specialOperation);
        userIsInTheMiddleOfTyping = false;

    }

}
 }

计算器的大脑在这里进行所有操作和计算

package calculator;

/**
*
* @author CMP
*/
import static javax.swing.JOptionPane.*;

public class CalculatorBrain {

//    Calculator calc = new Calculator();
double operAnd;
String waitingOperation;
double waitingOperand;
String plus = "+";
String specialOperation;
double memory = 0;

/// method to set the operAnd
public void setOperand(double anOperAnd) {
    operAnd = anOperAnd;

}

public void performWaitingOperation() {

    if (plus.equals(waitingOperation)) {

        operAnd = waitingOperand + operAnd;

    } else if ("-".equals(waitingOperation)) {

        operAnd = waitingOperand - operAnd;

    } else if ("/".equals(waitingOperation)) {
        String testFor0 = Double.toString(operAnd);
        if (testFor0.equals("0.0")) {
            showMessageDialog(null, "You can not divide by 0");
        } else {
            operAnd = waitingOperand / operAnd;
        }
    } else if ("*".equals(waitingOperation)) {

        operAnd = waitingOperand * operAnd;
    } else if ("C".equals(waitingOperation)) {
        operAnd = 0;
    }
}

public double perforumOperation(String operation) {
    if (operation.equals("sqrt")) {
    } else {
        performWaitingOperation();
        waitingOperation = operation;
        waitingOperand = operAnd;

    }

    return operAnd;

}

public void performSpecialOperation(String specialOp) {
    //specialOperation include C MC M+ M- MR +/-
    specialOperation = specialOp;

    //this if statement rests the screen and operAnd value to 0
    if ("C".equals(specialOperation)) {

        operAnd = 0;
        Calculator.display.setText("0");

    } else if ("+/-".equals(specialOperation)) {
        // this if stament checks whether the number is negative or positive

        if (operAnd < 0) {

            operAnd = Math.abs(operAnd);

        } else if (operAnd > 0) {

            operAnd = -operAnd;

        }

        String DisplayValue = Double.toString(operAnd);
        formatDisplay(DisplayValue);

    } else if ("MR".equals(specialOperation)) {

        String memoryValue = Double.toString(memory);

        formatDisplay(memoryValue);
    } else if ("M+".equals(specialOperation)) {

        memory = memory + Double.parseDouble(Calculator.display.getText());
        formatDisplay(Double.toString(memory));

    } else if ("MC".equals(specialOperation)) {

        memory = 0;
        formatDisplay(Double.toString(memory));

    } else if ("M-".equals(specialOperation)) {

        memory = memory - Double.parseDouble(Calculator.display.getText());
        formatDisplay(Double.toString(memory));

    }


}

public void formatDisplay(String value) {
    String newValue = value;
    if (value.endsWith(".0")) {
        newValue = value.substring(0, value.length() - 2);
        Calculator.display.setText("" + newValue);
    } else {
        Calculator.display.setText("" + newValue);
    }

}
}

如果您发现此帮助完整,请评论为什么 非常感谢您正在寻找成为更好的程序员并获得正确风格的方法

【问题讨论】:

  • 我认为没有人会为你调试你的计算器,但没有人知道,也许我错了和某人
  • “operAnd”中强制的驼峰格是怎么回事?
  • 尝试在这里提问:codereview.stackexchange.com(请不要大写!)
  • 这个问题似乎是题外话,因为它是关于改进代码的请求,这已经成为 SO 题外话。

标签: java swing user-interface calculator


【解决方案1】:

我不会为此使用 StackOverflow(尽管我很感兴趣它是否会起作用;)

我宁愿使用一些插件来使您的代码更好地可读、可用、健壮、高性能、正确……建议路线图:

  • 安装 checkstyle 以熟悉编码约定和样式。一旦你熟悉了它,宁可软化规则。
  • 使用 Findbugs 和 PMD 查找代码中的错误、风险或性能关键部分。
  • 开始使用指标,例如关于依赖项(例如 jdepend)、圈复杂度(例如 JavaNCSS)和测试覆盖率(例如 eclemma),
  • 哦,是的,并且为您的代码编写大量测试用例(预先),例如看看它的可用性。

从这里,你可以去

  • 持续集成,例如将 Jenkins 与声纳一起使用。
  • 进一步改进敏捷实践,尤其是使用代码审查和结对编程
  • 进入干净的代码开发(请参阅Java tool to improve my Clean Code Development 以获得支持)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 2021-04-06
    相关资源
    最近更新 更多