(没有括号和优先级,简易计算器)界面:
package javaprogram;
import java.awt.*;
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;
class Calculator extends JFrame{
private JLabel showJlabel = new JLabel(); //用来显示数值 //基础组件
private JPanel numJpanel = new JPanel(); //按钮面板 //容器类
private JPanel mainJpanel = new JPanel(); //主面板
private double result; //用来接收计算结果
private String symbol; //用来接收符号
private boolean start; //用来判断是否重新开始
public void init(){//初始化
this.setSize(300,220); //给窗体设置大小
this.setVisible(true); //让窗体可见
this.setLocationRelativeTo(null); //让窗体居中的方法
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //点击退出则程序关闭
this.setTitle("-----中华小妹牌计算器------"); //设置窗体标题
mainJpanel.setLayout(new BorderLayout()); //给主面板设为边框布局
mainJpanel.add(showJlabel,"North"); //让显示框位于主面板北方
mainJpanel.add(numJpanel);//在主面板中添加按钮面板
this.add(mainJpanel);
symbol ="=";//默认
showJlabel.setText("0"); //给显示框设置默认值0
showJlabel.setForeground(Color.blue);
start=true; //默认开始
numJpanel.setLayout(new GridLayout(5,4));//将界面划分成5行4列
Action1 action1 = new Action1();//添加数字和归零符号等处理 //准备监听对象
Action2 action2 = new Action2();//添加运算符号
this.addButton("C",action1);//归0
this.addButton("/",action2);
this.addButton("*",action2);
this.addButton("BACK",action1);//清除打错的字符
this.addButton("7",action1);
this.addButton("8",action1);
this.addButton("9",action1);
this.addButton("+",action2);
this.addButton("4",action1);
this.addButton("5",action1);
this.addButton("6",action1);
this.addButton("-",action2);
this.addButton("1",action1);
this.addButton("2",action1);
this.addButton("3",action1);
this.addButton("%",action2);
this.addButton(".",action1);
this.addButton("0",action1);
this.addButton("=",action2);
this.addButton("",action1);
}
//添加按钮并给按钮设置监听方法 监听按钮
public void addButton(String string,ActionListener a1){
JButton button =new JButton(string);//创建一个有标签文本、无图标的按钮
button.addActionListener(a1);
numJpanel.add(button);
}
//数字和基本操作处理
class Action1 implements ActionListener{ //接收操作事件的监听器接口
public void actionPerformed(ActionEvent e){
//同一个JFrame里可能有多个按钮的事件,为了避免冲突,给每个按钮设置不同的ActionCommand
String input = e.getActionCommand();//获取事件的标签内容
if(showJlabel.getText().equals("0")){
start = true;
}
if(start){ //如果start是true,就表示重新开始输入新的数
showJlabel.setText("");
start = false;
}
if(!input.equals("BACK")&&!input.equals("C")) {
//更新文本内容
showJlabel.setText(showJlabel.getText() + input);
}
if(input.equals("C")){//归0操作
showJlabel.setText("0");
}
if(input.equals("BACK")){ //回退操作
if(showJlabel.getText().length()>1){
showJlabel.setText(showJlabel.getText().substring(0, showJlabel.getText().length()-1)); //substring(0,3)string从0截取截到2
} else{
showJlabel.setText("0");
}
}
}
}
//进行运算
class Action2 implements ActionListener{
public void actionPerformed(ActionEvent e){
String input = e.getActionCommand();//获取事件的标签内容
if(start){
symbol = input; //符号
}else{
if(symbol.equals("+")){
result += Double.parseDouble(showJlabel.getText()); //将String类型的数字转换为double型的数字
} else if(symbol.equals("-")){
result -= Double.parseDouble(showJlabel.getText());
} else if(symbol.equals("*")){
result *= Double.parseDouble(showJlabel.getText());
} else if(symbol.equals("/")){
result /= Double.parseDouble(showJlabel.getText());
} else if(symbol.equals("%")){
result %= Double.parseDouble(showJlabel.getText());
}
else{//等于 “=” 直接显示输出的值
result = Double.parseDouble(showJlabel.getText()); }
showJlabel.setText(result+"");
symbol = input;
start = true;
}
}
}
public static void main(String[] args) {
Calculator calculator=new Calculator();
calculator.init();
}
}