软帝学院教你用java编写计算器
Posted 王囧草
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软帝学院教你用java编写计算器相关的知识,希望对你有一定的参考价值。
软帝学院教你用java编写计算器(二)
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
JTextField result;
int calculate_type=0;
Calculator(){
JPanel jp;
JButton jb;
jp= new JPanel();
jp.setLayout(new BorderLayout());
result = new JTextField();
result.setEditable(false);
jp.add(result,BorderLayout.NORTH);//窗口中间添加数字按钮、清空、退格、小数点按钮
JPanel keyPanel = new JPanel();
keyPanel.setLayout(new GridLayout(5,3));
jb = new JButton("1");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("2");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("3");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("4");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("5");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("6");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("7");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("8");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("9");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("0");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("C");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("退格");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton(".");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("=");
keyPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("CE");
keyPanel.add(jb);
jb.addActionListener(this);
jp.add(keyPanel,BorderLayout.CENTER);
JPanel operatorpJPanel = new JPanel();
operatorpJPanel.setLayout(new GridLayout(4,1));
jb = new JButton("+");
operatorpJPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("-");
operatorpJPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("*");
operatorpJPanel.add(jb);
jb.addActionListener(this);
jb = new JButton("/");
operatorpJPanel.add(jb);
jb.addActionListener(this);
jp.add(operatorpJPanel,BorderLayout.EAST);
setContentPane(jp);
setTitle("计算器");
setSize(340,260);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
String cmd =e.getActionCommand();
String c =result.getText();
if (cmd.equals("C")) {
result.setText("");
}
else if (cmd.equals("退格")) {
if (c.length()>0) {
String slast = c.substring(c.length() -1);
if (slast.equals("+") || slast.equals("-") || slast.equals("*") || slast.equals("/")) {
calculate_type = 0;//如果删除了运算符,将计算类型恢复为未确定状态
}
result.setText(c.substring(0,c.length() -1));
}
else if (cmd.compareTo("0")>=0 && cmd.compareTo("9")<=0) {
result.setText(c+cmd);
}
else if (cmd.equals(".")) {
result.setText(c+cmd);
}
else if (cmd.equals("+") || cmd.equals("-") || cmd.equals("*") || cmd.equals("/") ) {
if (calculate_type ==0) {
result.setText(c+cmd);
judgeCalculateType(cmd);//判断计算类型
}
else {//自上次计算完成后,第二次点击运算符,触发计算
calculate();
result.setText(result.getText()+cmd);
judgeCalculateType(cmd);//判断计算类型
}
}
else if (cmd.equals("=")) {//点击=进行计算
try {
} catch (Exception exp) {
JOptionPane.showMessageDialog(this, "输入错误,请检查输出或重新启动程序");
}
}
}
}
//判断计算类型
private void judgeCalculateType(String cmd) {
if (cmd.equals("+"))
calculate_type =1;//记录运算类型
else if (cmd.equals("-"))
calculate_type =2;
else if(cmd.equals("*"))
calculate_type =3;
else
calculate_type =4;
}
//完成计算功能
private void calculate() {
double rt,op1,op2;
String c = result.getText();
int pos;
char ops[] ={‘+‘,‘-‘,‘*‘,‘/‘};//查找运算符位置
pos =c.indexOf(ops[calculate_type-1]);//以运算符位置为分界点,分离出两个待计算的数
op1 = Double.parseDouble(c.substring(0,pos));
op2 = Double.parseDouble(c.substring(pos+1));
switch (calculate_type) {
case 1:
rt = op1+op2;
break;
case 2:
rt = op1-op2;
break;
case 3:
rt = op1*op2;
break;
case 4:
rt = op1/op2;
break;
default:
rt =0;
}
result.setText("" +rt);//显示计算结果
calculate_type =0;//准备记录下次计算的类型
}
public static void main(String[] args) {
new Calculator();
}
}
以上是关于软帝学院教你用java编写计算器的主要内容,如果未能解决你的问题,请参考以下文章