窗口的实现要用到JFrame,计算的实现要通过ActionListener
1.窗口的实现
public class calculatorLBW522
{
public void showUI()
{
CalucOfaction ual = new CalucOfaction();
JFrame jf = new JFrame();
jf.setTitle("calculator");
jf.setSize(210,240);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLocation(500,300);
FlowLayout flow = new FlowLayout();
jf.setLayout(flow);
JTextField cOUT = new JTextField(30);
JButton btnAC = new JButton("A");
JButton btnsp = new JButton("=");
JButton btnadd = new JButton("+");
JButton btnleft = new JButton("(");
JButton btnright = new JButton(")");
JButton btn0 = new JButton("0");
JButton btnp = new JButton(".");
JButton btnCOM = new JButton("%");
JButton[] btnNUM = new JButton[12];
String[] labelNUMSTR = {"1","2","3","-","4","5","6","*","7","8","9","\\"};
for(int i =0;i<btnNUM.length;i++)
{
btnNUM[i] = new JButton(labelNUMSTR[i]);
}
jf.add(cOUT);
jf.add(btnAC);
jf.add(btnsp);
jf.add(btnCOM);
jf.add(btnadd);
cOUT.addActionListener(ual);
btnAC.addActionListener(ual);
btnsp.addActionListener(ual);
btnCOM.addActionListener(ual);
btnadd.addActionListener(ual);
for(int i = 0; i< labelNUMSTR.length; i++)
{
jf.add(btnNUM[i]);
btnNUM[i].addActionListener(ual);
}
jf.add(btn0);
jf.add(btnp);//"."
jf.add(btnleft);
jf.add(btnright);
btn0.addActionListener(ual);
btnp.addActionListener(ual);
btnleft.addActionListener(ual);
btnright.addActionListener(ual);
jf.setVisible(true);
jf.setResizable(false);
}
public static void main(String[] args)
{
calculatorLBW522 cal = new calculatorLBW522();
cal.showUI();
}
}
2.具体功能的实现
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Date;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class CalucOfaction implements ActionListener
{
ArrayList<String> strcmputeArr = new ArrayList<>();
int[] num = new int[100];
public void actionPerformed(ActionEvent e)
{
strcmputeArr.add(e.getActionCommand());
System.out.println(strcmputeArr);
if(e.getActionCommand().equals("A"))
{
strcmputeArr.clear();
System.out.println("已清除,请重新计算");
}
else if (e.getActionCommand().equals("="))
{
String[] strcmputeStr = strcmputeArr.toArray(new String[0]);
int result = cmpute(strcmputeStr);
System.out.println(result);
}
}
int BaseCompute(int num1,int num2,char a)
{
switch (a)
{
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
case '%':
return num1 % num2;
default:
return 0;
}
}
int cmpute(String[] str)
{
int[] num = new int[100]; //存放的数字
int[] temp = new int[100];//中间结果
int numi = 0;
int numii = 0;
int tempi = 0;
int tempii = 0;
int i = 0;//i代表字符串的位置
char c0;
if(str[i].equals("("))
{
i++;//移动到“(”后的位置
num[numi++] = Integer.parseInt(str[i++]);//将“(”后的数字传递给num
char c1 = str[i].charAt(0);
i++;//移动到符合后的位置
num[numi++] = Integer.parseInt(str[i++]);
i++;//跳到“)”后的位置
temp[tempi++] = BaseCompute(num[numi - 2], num[numi - 1],c1);
numi -=2;
}
c0 = str[i].charAt(0);
i++;
if(str[i].equals("("))
{
i++;//移动到“(”后的位置
num[numi++] = Integer.parseInt(str[i++]);//将“(”后的数字传递给num
char c1 = str[i].charAt(0);
i++;//移动到符合后的位置
num[numi++] = Integer.parseInt(str[i++]);//将数字传递给num后,后移
i++;//跳到“)”后的位置
temp[tempi++] = BaseCompute(num[numi - 2], num[numi - 1],c1);
numi -=2;
}
temp[tempi++] = BaseCompute(temp[tempi-3],temp[tempi-2],c0);
return temp[tempi-1];
}
}
注:初学者能力有限,只能实现最最*100基本的功能,欢迎大佬指正。
标签:实现,numi,初步,++,int,num,计算器,new,JButton From: https://www.cnblogs.com/lbwxxc/p/18227372