、使用JAVA语言编写一个简易的计算器

2020-09-21 科技 122阅读
我写了个整数可以使用的, 你可以研究一下, 添入小数部分
功能和window7自带的那个一样
import java.awt.BorderLayout;
import java.awt.GridLayout;
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.border.LineBorder;
public class Caculator extends JFrame {
private JLabel r1;
private JLabel r2;
private int tempResult;
boolean isResult;
private String lastOperator;
public Caculator() {
isResult = true;
tempResult = 0;
lastOperator = "+";
JPanel jp_main = new JPanel();
jp_main.setLayout(new BorderLayout());
JPanel jp_show = new JPanel();
jp_show.setBorder(new LineBorder(java.awt.Color.black));
jp_show.setLayout(new GridLayout(2, 1));
r1 = new JLabel("0", JLabel.RIGHT);
r2 = new JLabel("0", JLabel.RIGHT);
jp_show.add(r1);
jp_show.add(r2);
JPanel jp_button = new JPanel();
jp_button.setBorder(new LineBorder(java.awt.Color.black));
jp_button.setLayout(new GridLayout(4, 4));
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
JButton zero = new JButton("0");
JButton dot = new JButton(".");
JButton plus = new JButton("+");
JButton sub = new JButton("-");
JButton multi = new JButton("*");
JButton divide = new JButton("/");
JButton result = new JButton("=");
jp_button.add(one);
jp_button.add(two);
jp_button.add(three);
jp_button.add(sub);
jp_button.add(four);
jp_button.add(five);
jp_button.add(six);
jp_button.add(multi);
jp_button.add(seven);
jp_button.add(eight);
jp_button.add(nine);
jp_button.add(divide);
jp_button.add(zero);
jp_button.add(dot);
jp_button.add(plus);
jp_button.add(result);
JPanel reset = new JPanel();
reset.setBorder(new LineBorder(java.awt.Color.black));
JButton rb = new JButton("重置");
rb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
r1.setText("0");
r2.setText("0");
isResult = true;
tempResult = 0;
lastOperator = "+";
}
});
reset.add(rb);
jp_main.add(jp_show, BorderLayout.NORTH);
jp_main.add(jp_button, BorderLayout.CENTER);
jp_main.add(reset, BorderLayout.SOUTH);
this.add(jp_main);
one.addActionListener(new NumberButtonListener());
two.addActionListener(new NumberButtonListener());
three.addActionListener(new NumberButtonListener());
four.addActionListener(new NumberButtonListener());
five.addActionListener(new NumberButtonListener());
six.addActionListener(new NumberButtonListener());
seven.addActionListener(new NumberButtonListener());
eight.addActionListener(new NumberButtonListener());
nine.addActionListener(new NumberButtonListener());
zero.addActionListener(new NumberButtonListener());
// dot.addActionListener(new NumberButtonListener());
plus.addActionListener(new OperatorButtonListener());
sub.addActionListener(new OperatorButtonListener());
multi.addActionListener(new OperatorButtonListener());
divide.addActionListener(new OperatorButtonListener());
result.addActionListener(new ResultButtonListener());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(370, 280);
this.setResizable(false);
this.setLocationRelativeTo(null);
}
private class NumberButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JButton jb = (JButton) arg0.getSource();
String temp = r2.getText();
if (!isResult) {
if (temp.equals("0")) {
r2.setText(jb.getText());
} else {
r2.setText(temp + jb.getText());
}
} else {
r2.setText(jb.getText());
}
isResult = false;
}
}

private class ResultButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
r1.setText("0");
new OperatorButtonListener().caculate(new JButton("+"));
r2.setText(String.valueOf(tempResult));
isResult = true;
tempResult = 0;
lastOperator = "+";
}
}
private class OperatorButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JButton jb = (JButton) arg0.getSource();
String temp1 = r1.getText();
if (temp1.equals("0")) {
r1.setText(r2.getText() + " " + jb.getText());
caculate(jb);

} else {
r1.setText(temp1 + " " + r2.getText() + " " + jb.getText());
caculate(jb);
r2.setText(String.valueOf(tempResult));

}
isResult = true;
}

public void caculate(JButton jb){
if(lastOperator.equals("+")){
tempResult += Integer.valueOf(r2.getText());
}else if(lastOperator.equals("-")){
tempResult -= Integer.valueOf(r2.getText());
}else if(lastOperator.equals("*")){
tempResult *= Integer.valueOf(r2.getText());
}else if(lastOperator.equals("/")){
tempResult /= Integer.valueOf(r2.getText());
}
lastOperator = jb.getText();
}
}
}
下面是main函数
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Caculator c = new Caculator();
}
}
你运行就可以看到效果
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com