package
yz;
//引入类
import
java.awt.*;
import
javax.swing.*;
import
java.awt.event.*;
import
java.math.BigDecimal;
public
class
Yz
extends
JFrame
implements
ActionListener {
JPanel jsq;
JTextField txt;
JButton[] Nums;
int
f=
0
;
Double g=
0.0
,h=
0.0
,j=
0.0
;
String fh=
""
;
public
static
void
main(String[] args) {
Yz y=
new
Yz();
}
public
Yz(){
jsq=
new
JPanel();
jsq.setLayout(
new
GridLayout(
5
,
4
,
4
,
4
));
Nums=
new
JButton[
20
];
String []a={
"←"
,
"CE"
,
"C"
,
"%"
,
"7"
,
"8"
,
"9"
,
"/"
,
"4"
,
"5"
,
"6"
,
"*"
,
"1"
,
"2"
,
"3"
,
"-"
,
"."
,
"0"
,
"="
,
"+"
};
for
(
int
i=
0
;i<Nums.length;i++){
Nums[i]=
new
JButton(a[i]);
jsq.add(Nums[i]);
Nums[i].addActionListener(
this
);
//注册监听
}
txt=
new
JTextField(
10
);
txt.setText(
""
);
txt.setEditable(
false
);
txt.setRequestFocusEnabled(
false
);
txt.setHorizontalAlignment(JTextField.RIGHT);
this
.add(txt,BorderLayout.NORTH);
this
.add(jsq);
this
.setVisible(
true
);
//显示窗体
this
.setSize(
300
,
300
);
//窗体大小
this
.setLocation(
550
,
300
);
//出现位置
this
.setTitle(
"计算器"
);
//窗体标题
this
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//关闭进程
}
//监听
public
void
actionPerformed(ActionEvent e){
String a=e.getActionCommand();
int
b=
"0123456789"
.indexOf(a);
int
c=
"+-*/%"
.indexOf(a);
if
(b!=-
1
){
//如果按下的是数字
if
(f==
0
){
txt.setText(a);
f=
1
;
}
else
{
String d = txt.getText();
txt.setText(d+a);
}
}
else
if
(c!=-
1
)
//如果按下的是运算符
{
if
(c==
1
&&f==
0
){
g=
0.0
;
txt.setText(a);
f=
1
;
}
else
if
(txt.getText().equals(
""
)){
JOptionPane.showMessageDialog(
null
,
"错误"
,
"不能为零请重新输入"
, JOptionPane.ERROR_MESSAGE);
}
else
{
g=Double.valueOf(txt.getText());
fh=a;
txt.setText(
""
);
f=
0
;
}
}
else
if
(
"="
==a){
//如果按下的是等号
if
(txt.getText().equals(
""
)){
h=
0.0
;
}
else
h=Double.valueOf(txt.getText());
if
(fh==
"+"
){
//如果按下的是加号
String d1 = String.valueOf(g+h).substring(
0
, String.valueOf(g+h).length());
double
i;
i=Double.valueOf(d1);
int
r=(
int
)i;
if
(r==Double.valueOf(d1)){
d1=d1.substring(
0
, d1.length()-
2
);
txt.setText(d1);
}
else
txt.setText(d1);
}
if
(fh==
"-"
){
//如果按下的是减号
String d2 = String.valueOf(g-h).substring(
0
, String.valueOf(g-h).length());
double
i;
i=Double.valueOf(d2);
if
(i>=
0
)
{
float
r=(
float
)i;
if
(r==Double.valueOf(d2)){
d2=d2.substring(
0
, d2.length()-
2
);
txt.setText(d2);
}
else
{
txt.setText(d2);
}
}
else
{
txt.setText(
new
BigDecimal(String.valueOf(g-h).substring(
0
, String.valueOf(g-h).length())).stripTrailingZeros().toString());
}
}
if
(fh==
"*"
){
//如果按下的是乘号
String d3 = String.valueOf(g*h).substring(
0
, String.valueOf(g*h).length());
double
i;
i=Double.valueOf(d3);
int
r=(
int
)i;
if
(r==i){
d3=d3.substring(
0
, d3.length()-
2
);
txt.setText(d3);
}
else
{
txt.setText(d3);
}
}
if
(fh==
"/"
){
//如果按下的是除号
if
(h==
0.0
){
JOptionPane.showMessageDialog(
null
,
"错误"
,
"除数不能为零请重新输入"
, JOptionPane.ERROR_MESSAGE);
txt.setText(
""
);
}
else
{
String d4 = String.valueOf(g/h).substring(
0
, String.valueOf(g/h).length());
double
i;
i=Double.valueOf(d4);
int
r=(
int
)i;
if
(r==Double.valueOf(d4)){
d4=d4.substring(
0
, d4.length()-
2
);
txt.setText(d4);
}
else
{
txt.setText(d4);
}
}
}
if
(fh==
"%"
){
String n = txt.getText();
txt.setText(String.valueOf(g/
100
));
}
}
else
if
(
"CE"
==a){
h=
0.0
;
txt.setText(
""
);
}
else
if
(
"C"
==a){
g=
0.0
;
h=
0.0
;
f=
0
;
txt.setText(
""
);
}
else
if
(
"."
==a){
String n = txt.getText();
if
(txt.getText().trim().indexOf(
"."
)!=-
1
) ;
else
txt.setText(n+
"."
);
}
else
if
(
"←"
==a){
String xs=txt.getText();
if
(xs.length()>=
1
)
xs=xs.substring(
0
, xs.length()-
1
);
txt.setText(xs);
}
}
}
|