import java.awt.*; //引入抽象窗体工具包
import javax.swing.*; //引入图形用户界面的包
import java.awt.event.*;
//面板布局类
class calculatorFrame extends JFrame implements ActionListener //自定义一个布局框架类calculatorFrame继承JFrame
{
Container cp;
JPanel pnl[] = new JPanel[3]; //定义一个主面板作为这个窗体的容器面板
JTextField text; //定义一个文本域
double num[]={0, 0}; //定义两个变量用于存储数值
int index=0; //用于指定访问num[0]或num[1]
char sign; //运算符
String s[]={ "toDegrees","toRadians","Backapace","Clear",
"7","8","9","/","sqrt",
"4","5","6","*","%",
"1","2","3","-","1/x",
"0","+/-",".","+","=",
"sin","cos","tan","cot","HEX"},
toolTip[]={ "角度->弧度","弧度->角度","删除字符","清空",
"除","开方","乘","求余数","减","求倒数","正负变换","加","等于",
"正弦","余弦","正切","余切","转化16进制"};
int tipSite[]={ 0,1,2,3,
7,8,12,13,17,18,20,22,23,
24,25,26,27,28};
JButton b[] = new JButton[29];
calculatorFrame()
{
setTitle("计算器-1042157148"); //设置窗体的标题
setSize(400, 220); //设置窗体的大小
setLocation(200, 200); //设置窗体的显示位置
cp=getContentPane();
//将pnl[i]添加到容器cp中,按"North","Center","South"布局
String str[]={"North","Center","South"};
for(int i=0; i<3; i++)
{ pnl[i] = new JPanel();
cp.add(pnl[i],str[i]);
}
pnl[0].setLayout(new GridLayout(1,6));
pnl[1].setLayout(new GridLayout(1,4));
pnl[2].setLayout(new GridLayout(5,5));
text = new JTextField(25);
text.setText("");
text.setHorizontalAlignment(JTextField.RIGHT); //文本右对齐
text.setEditable(false); //文本域不可编辑
text.setBackground(new Color(255, 255, 255)); //设置背景色
//向pnl[i]中添加按钮
pnl[0].add(text);
for(int i=0; i<29; i++)
{
b[i] = new JButton(s[i]);
if(i<=3)pnl[1].add(b[i]);
else pnl[2].add(b[i]);
b[i].addActionListener(this); //添加按钮事件监听
//设置以下按钮前景色为红色
if(i<=3 || i%5==2 || i==23)b[i].setForeground(new Color(255,0,0));
}
for(int i=0; i<toolTip.length; i++)
{
b[tipSite[i]].setToolTipText(toolTip[i]);//为按钮添加提示信息
}
setVisible(true); //显示窗体
setDefaultCloseOperation(EXIT_ON_CLOSE); //退出程序时关闭窗体
}
public void actionPerformed(ActionEvent e) //事件响应
{
Object tmp = e.getSource(); //获取事件源
String str = text.getText();
if(tmp==b[2] && str.length()>0) str=str.substring(0, str.length()-1);//退格键
else if(tmp==b[3]){ str=""; index=0; num[0]=0; num[1]=0;}//清空键
//字符输入
if(tmp==b[4] || tmp==b[5] || tmp==b[6] || tmp==b[9] || tmp==b[10] || tmp==b[11] ||
tmp==b[14] || tmp==b[15] || tmp==b[16] || tmp==b[19] || tmp==b[21])
{
if(tmp==b[4]) str += '7';
else if(tmp==b[5]) str += '8';
else if(tmp==b[6]) str += '9';
else if(tmp==b[9]) str += '4';
else if(tmp==b[10]) str += '5';
else if(tmp==b[11]) str += '6';
else if(tmp==b[14]) str += '1';
else if(tmp==b[15]) str += '2';
else if(tmp==b[16]) str += '3';
else if(tmp==b[19]) str += '0';
else if(tmp==b[21]) if(str.indexOf('.') == -1) str += '.'; //有一个小数点时不再添加
if(index==0) num[0] = Double.parseDouble(str); //将输入的字符串转化成数值并存储在num中
else num[1] = Double.parseDouble(str);
}
//运算符设置
else if(tmp==b[7] || tmp==b[12] || tmp==b[13] || tmp==b[17] || tmp==b[22])
{
if(tmp==b[7]) sign='/';
else if(tmp==b[12]) sign='*';
else if(tmp==b[13]) sign='%';
else if(tmp==b[17]) sign='-';
else if(tmp==b[22]) sign='+';
index=1;
str="";
}
else if(tmp==b[23]) //" == "输出运算结果
{
if(sign=='/' || sign=='*' || sign=='%' || sign=='-' || sign=='+')
{
if(sign=='/'){ num[0]/=num[1];}
else if(sign=='*'){ num[0]*=num[1];}
else if(sign=='%'){ num[0]%=num[1];}
else if(sign=='-'){ num[0]-=num[1];}
else if(sign=='+'){ num[0]+=num[1];}
index=0;
num[1]=0; //清除num[1]中的数值
str=Double.toString(num[index]);
}
}
//单目运算
if(tmp==b[0] || tmp==b[1] || tmp==b[8] || tmp==b[18] || tmp==b[20] || tmp==b[24] ||
tmp==b[25] || tmp==b[26] || tmp==b[27])
{
if(tmp==b[0]) num[index]=java.lang.Math.toDegrees(num[index]); //弧度->角度
if(tmp==b[1]) num[index]=java.lang.Math.toRadians(num[index]); //角度->弧度
if(tmp==b[8]) if(num[index]>=0) num[index]=java.lang.Math.sqrt(num[index]);
if(tmp==b[18]) num[index]=1/num[index]; //求倒数
if(tmp==b[20]) num[index]=-num[index]; //正负变换
if(tmp==b[24]) num[index]=java.lang.Math.sin(num[index]); //正弦
if(tmp==b[25]) num[index]=java.lang.Math.cos(num[index]);
if(tmp==b[26]) num[index]=java.lang.Math.tan(num[index]);
if(tmp==b[27]) num[index]=1/java.lang.Math.tan(num[index]);
str=Double.toString(num[index]); //更新str
}
if(tmp==b[28]) //将num[index]转化成16进制的字符串
{
int num1=(int)num[index], num2=0, sign=0;
char tmpC;
str="";
if(num1<0){ num1=-num1; sign=1;} //转换为正数
while(num1!=0)
{
num2=num1%16;
if(num2>9)tmpC=(char)('A'+num2-10);
else tmpC=(char)('0'+num2);
str=tmpC+str;
num1/=16;
}
if(sign==1)str='-'+str; //是负数时str添加负号
}
text.setText(str); //更新文本框中的显示内容
}
public static void main(String[] args)
{
new calculatorFrame(); //创建一个布局窗体
}
}
06-03