📄 calculatordemonstration.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Calculator extends JFrame // 主体功能实现类
{ Container container;
String[] str1={"MC","7","8","9","/","sqrt","MR","4","5","6","*","%","MS","1","2","3","-","1/x","M+","0","+/-",".","+","="};
String[] str2={"Backspace","CE","C"}; //使用字符串数组来实现程序的优化
JButton[] grd =new JButton[str1.length]; //JButton对象数组来管理所有键
JButton[] BCC =new JButton[str2.length];
JPanel pane1,pane2,pane12,pane123;
JTextField Msig,displayField;
String lastCommand, store="0"; //lastCommand存储上次的操作符,store主要是用来存储M操作符的计算结果的
static double result; //存储控制符算出的结果
boolean start,flag,lag; //三个标志符
Calculator()
{ super("计算器");
setSize(420,320);
container=getContentPane();
pane1 = new JPanel(); //下面24个键
pane2 = new JPanel(); //中间4个键
pane12 = new JPanel();
pane123 =new JPanel();
Msig =new JTextField(); //M显示区域
Msig.setHorizontalAlignment(JTextField.CENTER); //中间对齐
displayField =new JTextField(); //输入数字显示区
displayField.setHorizontalAlignment(JTextField.RIGHT); //右对齐
displayField.setBackground(Color.white); //底色为白色
Msig.setEditable(false); //使两个可编辑文本框不可通过鼠标和键盘编辑
displayField.setEditable(false);
pane1.setLayout(new GridLayout(4,6,5,5)); //为面板设置排版类型
pane2.setLayout(new GridLayout(1,4,5,5));
pane12.setLayout(new BorderLayout(5,5));
pane123.setLayout(new BorderLayout(5,5));
ActionListener insert = new InsertAction(); //三个事件监听类初始化
ActionListener commanda = new CommandaAction();
ActionListener commandb = new CommandbAction();
start=true; //为标志符设初值
flag=false;
lag=false;
result=0; //result和lastcommand设初值
lastCommand = "=";
for(int i=0;i<=str1.length-1;i++) //为pane1面板添加JButton,通过循环实现
{ grd[i]=new JButton(str1[i]);
if((i%6)==0)
grd[i].setBackground(Color.green); //M键涂成绿色
if((((i-4)%6)==0)||(i==str1.length-1)) //加减乘除等于号涂色和注册事件监听类
{ grd[i].setBackground(Color.yellow);
grd[i].addActionListener(commanda);
}
else if((i-5)%6==0&&(i!=str1.length-1)) grd[i].addActionListener(commandb); //sqrt,%,1/x注册
else grd[i].addActionListener(insert); //其他键注册事件监听
pane1.add(grd[i]); //添加JButton
}
pane2.add(Msig);
for(int i=0;i<=str2.length-1;i++) //为pane2面板添加JButton和注册事件监听类
{ BCC[i]=new JButton(str2[i]);
pane2.add(BCC[i]);
BCC[i].setBackground(Color.blue);
BCC[i].addActionListener(new InsertAction());
}
pane12.add(pane2,BorderLayout.NORTH); //按BorderLayout来添加pane1 ,2到pane12
pane12.add(pane1,BorderLayout.CENTER);
pane123.add(displayField,BorderLayout.NORTH);
pane123.add(pane12,BorderLayout.CENTER);
container.add(pane123);
setVisible(true);
}
class InsertAction implements ActionListener { //insetAction事件监听类
public void actionPerformed(ActionEvent event) {
String input=event.getActionCommand(); //设置input为按下的键的text区字符串
if(input.equals("+/-"))
displayField.setText("-"+displayField.getText()); //显示字符串
if (start) {
displayField.setText("");
start=false;
} //如果start=true,不保留原来的结果,重新开始计算,setText为空
if(!input.equals("+/-")) {
if(input.equals("Backspace")) { //如果按键是Backspace
String str=displayField.getText();
if(str.length()>0)
displayField.setText(str.substring(0,str.length()-1));
}else if(input.equals("CE")||input.equals("C")) { //如果按键是CE或C
displayField.setText("0");
flag=true;
lag=true;
result=0;
lastCommand = "=";
}else if(input.equals("MS")){ //如果是MS
store=displayField.getText();
Msig.setText("M");
}else if(input.equals("MR")){ //如果是MR
displayField.setText(store);
}else if(input.equals("MC")){ //如果是MC
store="0";
Msig.setText("");
}else if(input.equals("M+")){ //如果是M+
if(input!=null) { double y=Double.parseDouble(displayField.getText())+Double.parseDouble(store);
Double Y=new Double(y);
store=Y.toString();
} //如果有输入,将输入数和存储数相加存储
else { double y=Double.parseDouble(store)*2;
Double Y=new Double(y);
store=Y.toString();}
} //如果没有输入,将存储数的2倍存储
else { if(lag) { displayField.setText(input); lag=false;
} //如果文本框上的数是0,只输出输入的那个数字,去掉0
else
displayField.setText(displayField.getText()+input);
}
}
}
}
class CommandaAction implements ActionListener { //CommandaAction类
public void actionPerformed(ActionEvent evt) {
String command1=evt.getActionCommand();
if(start) {
lastCommand=command1;
}else {
calculate(Double.parseDouble(displayField.getText())); //先计算,后存command1
lastCommand=command1;
start=true;
displayField.setText(""+ result);
}
}
}
class CommandbAction implements ActionListener { //CommandbAction类
public void actionPerformed(ActionEvent ev) {
String command2=ev.getActionCommand();
if(!flag){ lastCommand=command2; //先存储command2后计算
calculate(result);
displayField.setText(""+ result);
}
else { //处理如果一开始就输入数在对其求方根或倒数的情况
calculate(Double.parseDouble(displayField.getText()));
lastCommand=command2;
calculate(result);
displayField.setText(""+ result);
flag=false;
}
}
}
public void calculate(double x) { //计算部分,通过lastCommand来实现
if (lastCommand.equals("+")) result+= x;
else if (lastCommand.equals("-")) result-=x;
else if (lastCommand.equals("*")) result*=x;
else if (lastCommand.equals("/")) result/=x;
else if (lastCommand.equals("=")) result=x;
else if (lastCommand.equals("sqrt")) result=Math.sqrt(x);
else if (lastCommand.equals("%")) result=x/100;
else if (lastCommand.equals("1/x")) result=1/x;
}
}
public class CalculatorDemonstration{ //显示函数
public static void main(String[] args) {
try{
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}catch(Exception e){}
Calculator test = new Calculator();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -