📄 huyuena.java
字号:
/*
* @(#)Huyuena.java 1.0 04/11/13
*
* You can modify the template of this file in the
* directory ..\JCreator\Templates\Template_1\Project_Name.java
*
* You can also create your own project template by making a new
* folder in the directory ..\JCreator\Template\. Use the other
* templates as examples.
*
*/
package myprojects.huyuena;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Huyuena implements ActionListener
{
public static void main(String args[])
{
new Huyuena();
}
Frame fr;
TextField textAnswer;
Panel panel,panel1,panel2;
Button button[],A,B,C,D,E,F,G,H,I,J;
DecimalFormat df;
String flg;
boolean ca;
double vard,answerd;
short key=-1,prekey=-1;
public Huyuena()
{
ca=true;
answerd=0;
fr=new Frame("简单的计算器");
df=new DecimalFormat("0.########");
textAnswer=new TextField(10);
panel=new Panel();
fr.add(panel);
panel1=new Panel();
panel2=new Panel();
panel.setLayout(new BorderLayout());
panel.add(textAnswer,BorderLayout.CENTER);
panel.add(panel1, BorderLayout.SOUTH);
panel1.setLayout(new BorderLayout());
panel1.add(panel2, BorderLayout.CENTER);
button=new Button[10];
for(int i=0; i<button.length;i++)
{
button[i]=new Button(Integer.toString(i));
}
A=new Button("+");
A.setForeground(new Color(0,0,255));
B=new Button("-");
B.setForeground(new Color(0,0,255));
C=new Button("*");
C.setForeground(new Color(0,0,255));
D=new Button("/");
D.setForeground(new Color(0,0,255));
E=new Button("%");
E.setForeground(new Color(0,0,255));
F=new Button("Clear");
F.setForeground(new Color(255,0,0));
G=new Button("sqrt");
G.setForeground(new Color(0,0,255));
H=new Button("1/x");
H.setForeground(new Color(0,0,255));
I=new Button("=");
I.setForeground(new Color(255,0,0));
J=new Button(".");
panel2.setLayout(new GridLayout(4, 5));
button[7].addActionListener(this);
button[8].addActionListener(this);
button[9].addActionListener(this);
A.addActionListener(this);
F.addActionListener(this);
button[4].addActionListener(this);
button[5].addActionListener(this);
button[6].addActionListener(this);
B.addActionListener(this);
G.addActionListener(this);
button[1].addActionListener(this);
button[2].addActionListener(this);
button[3].addActionListener(this);
C.addActionListener(this);
H.addActionListener(this);
button[0].addActionListener(this);
J.addActionListener(this);
E.addActionListener(this);
D.addActionListener(this);
I.addActionListener(this);
panel2.add(button[7]);
panel2.add(button[8]);
panel2.add(button[9]);
panel2.add(A);
panel2.add(F);
panel2.add(button[4]);
panel2.add(button[5]);
panel2.add(button[6]);
panel2.add(B);
panel2.add(G);
panel2.add(button[1]);
panel2.add(button[2]);
panel2.add(button[3]);
panel2.add(C);
panel2.add(H);
panel2.add(button[0]);
panel2.add(J);
panel2.add(E);
panel2.add(D);
panel2.add(I);
fr.pack();
fr.show();
}
public void actionPerformed(ActionEvent event)
{
boolean sign=false;
Object temp=event.getSource();
try {
for (int i=0;i<=9;i++)
if (temp==button[i]&&ca==true)
textAnswer.setText(textAnswer.getText()+Integer.toString(i));
if (temp==J&&ca==true)
{
boolean isJ=false;
if (textAnswer.getText().length()==0)
isJ=true;
for (int i=0; i<textAnswer.getText().length();i++)
if ('.'==textAnswer.getText().charAt(i))
{
isJ=true;
break;
}
if (isJ==false)
textAnswer.setText(textAnswer.getText()+".");
}
if ((temp==A||temp==B||temp==C||temp==D) && ca==true)
{
if(temp==A)
{
answerd=Double.parseDouble(textAnswer.getText());
textAnswer.setText("");
prekey=key=0;
}
if (temp==B)
{
answerd=Double.parseDouble(textAnswer.getText());
textAnswer.setText("");
prekey=key=1;
}
if (temp==C)
{
answerd=Double.parseDouble(textAnswer.getText());
textAnswer.setText("");
prekey=key=2;
}
if (temp==D)
{
answerd=Double.parseDouble(textAnswer.getText());
textAnswer.setText("");
prekey = key = 3;
}
}
if (temp==I&&ca==true)
{
if (prekey==5)
{
if (key==0)
{
answerd+=vard;
textAnswer.setText(df.format(answerd));
}
if (key==1)
{
answerd-=vard;
textAnswer.setText(df.format(answerd));
}
if (key==2)
{
answerd*=vard;
textAnswer.setText(df.format(answerd));
}
if (key==3)
{
answerd/=vard;
textAnswer.setText(df.format(answerd));
}
}
else
{
vard=Double.parseDouble(textAnswer.getText());
if (key==0)
{
prekey=-1;
answerd+=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if (key==1)
{
prekey=-1;
answerd-=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if (key==2)
{
prekey=-1;
answerd*=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if (key==3)
{
prekey=-1;
if (Double.parseDouble(textAnswer.getText())==0)
{
textAnswer.setText("除数不能为零");
ca=false;
}
else
{
answerd/=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
}
}
prekey=5;
}
if (temp==E && ca==true)
{
double dtemp=Double.parseDouble(textAnswer.getText());
dtemp=dtemp/100.0;
textAnswer.setText(Double.toString(dtemp));
}
if (temp==G&&ca==true)
{
String s=textAnswer.getText();
if (s.charAt(0)=='-')
{
textAnswer.setText("负数不能开根号");
ca=false;
}
else
textAnswer.setText(Double.toString(java.lang.Math.sqrt(Double.
parseDouble(textAnswer.getText()))));
}
if (temp==H&&ca==true)
{
String s = Double.toString(1/Double.parseDouble(textAnswer.getText()));
textAnswer.setText(s);
ca=true;
}
if (temp==F)
{
vard=answerd=0;
textAnswer.setText("");
ca=true;
}
}
catch (Exception e)
{
textAnswer.setText("操作非法");
ca=false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -