要求:
1、参照Windows计算器功能,实现加、减、乘、除运算;
2、声明为按钮数组;
3、对0~9数字操作,采用相同的算法;
4、解决除数为零的问题;
5、具有清除一个数字、全部清除、开平方和平方功能。
6、计算结果保留4位小数。
7、界面缩放时,所有组件同步缩放,保持界面整体风格不变。
calculator函数:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class calculator extends JPanel implements ActionListener {
//声明按钮
private JButton numJButton[][] = new JButton[5][4];
//为初始化按钮做准备
String name[][] = {{"√x","x²","←","+"},
{"7","8","9","-"},
{"4","5","6","×"},
{"1","2","3","÷"},
{".","0","del","="}};
private JTextField text = new JTextField();//声明文本框用于显示数据
private JPanel temp[] = new JPanel[5];
private String num = "",num2 = "";//两个待运算的数
private int flag = 0;//全局标志变量
public calculator() {
this.setLayout(new GridLayout(6, 1));//先设置成六行一列的网格布局
this.add(text); //将文本框至于第一整行
text.setEditable(false); //设置文本框为不可编辑
for(int i = 0;i < 5;i++) //剩下的五行每一行四列,整体看就是一行文本框和五行按钮
temp[i] = new JPanel(new GridLayout(1,4));
//按钮初始化
for(int i=0;i<5;i++) {
for(int j=0;j<4;j++) {
numJButton[i][j] = new JButton(name[i][j]);
numJButton[i][j].addActionListener(this);
temp[i].add(numJButton[i][j]);
}
}
//添加面板
for(int i = 0;i < 5;i++)
this.add(temp[i]);
this.setVisible(true);
}
//设置监听器
@Override
public void actionPerformed(ActionEvent e) {
// 触发数字按钮
//1~9的按钮
for (int i = 1; i <=3 ; i++) {
for (int j = 0; j <=2; j++) {
if(e.getSource() == numJButton[i][j]) {
if(flag==0) {
/*
*e.getActionCommand()用来获得按钮显示的字符串
*numJButton[i][j].getLabel()也可以
*/
num = num + e.getActionCommand();
text.setText(num);
}
else if(flag==-1){
flag = 0;
num = num + e.getActionCommand();
text.setText(num);
}
else{
num2 = num2+e.getActionCommand();
text.setText(num2);
}
}
}
}
//0按钮,因为存储位置没有和其他数字的连在一起,所以单拿出来
if(e.getSource() == numJButton[4][1])
{
num = num + e.getActionCommand();
text.setText(num);
}
//触发加减乘除按钮,按钮顺序就是加减乘除
for(int i=0;i<=3;i++) {
if(e.getSource()==numJButton[i][3])
{
flag = i+1;
text.setText(e.getActionCommand());
}
}
//触发小数点按钮
if(e.getSource()==numJButton[4][0])
{
if(flag==0 ||flag==-1) {
if(num.indexOf('.')==-1) {
flag = 0;
num = num +".";
text.setText(num);
}
}
else
{
if(num2.indexOf('.')==-1) {
num2 = num2 +".";
text.setText(num2);
}
}
}
//清空当前所有字符
if(e.getSource()==numJButton[4][2])
{
if(flag==0 || flag == -1) {
num = "";
flag = -1;
text.setText(num);
}
else
{
num2 = "";
text.setText(num2);
}
}
//根据标记的不同每次分情况清空一个字符
if(e.getSource()==numJButton[0][2])
{
if((flag==0 || flag==-1)&&num.length()>0) {
flag = 0;
num = num.substring(0, num.length()-1);
if(num=="")
num = "0";
text.setText(num);
}
if(flag!=0 &&flag!=-1&&num2.length()>0) {
num2 = num2.substring(0, num2.length()-1);
if(num2=="")
num2 = "0";
text.setText(num2);
}
}
//平方运算
if(e.getSource()==numJButton[0][1])
{
double rlt;
if(flag == 0 ||flag == -1)
{
if(flag==-1) flag = 0;
rlt = Double.parseDouble(num);
num = String.format("%.4f", rlt*rlt);
}
else
rlt = Double.parseDouble(num2);
rlt = rlt*rlt;
text.setText(String.format("%.4f", rlt));
}
//开方运算
if(e.getSource()==numJButton[0][0])
{
double rlt=0;
if(flag == 0 ||flag == -1)
{
if(flag==-1) flag = 0;
rlt = Math.sqrt(Double.parseDouble(num));
num = String.format("%.4f", rlt);
}
else
{
rlt = Math.sqrt(Double.parseDouble(num2));
num2 = String.format("%.4f", rlt);
}
text.setText(String.format("%.4f", rlt));
}
int mark = 0;
//触发了等号 进行求值运算
if(e.getSource()==numJButton[4][3])
{
if(flag!=-1 && flag!=0) {
double n1 = Double.parseDouble(num);
double n2 = Double.parseDouble(num2);
double result = 0;
if(flag==1)
result = n1+n2;
else if(flag==2)
result = n1-n2;
else if(flag==3)
result = n1*n2;
else if(flag==4) {
try {//处理除数为0的操作
if(n2==0)
throw new Exception();
else
result = n1/n2;
}
catch (Exception e1) {
text.setText("0不能做除数");
mark = 1;
}
}
//除数不为0,或者进行的是其他操作时进行的操作
flag = -1;
num2 = "";
if(mark!=1) {//除数不为0
num = String.format("%.4f", result);
text.setText(num);
}
else//除数为0
{
flag = 0;
num = "0";
}
}
}
}
}
test:
import javax.swing.JFrame;
public class test extends JFrame {
public test(calculator c)
{
super("计算器");
this.setBounds(300,400,400,400);
this.getContentPane().add(c);
this.setVisible(true);
}
public static void main(String[] args) {
new test(new calculator());
}
}
运行是这个样子