import java.awt.*;
import java.awt.event.*;
import java.applet.Applet; public class jsq extends Applet implements ActionListener
{
int flag=0;
double x;
String s=new String("");
Panel p1,p2,p3;
Label label;
TextField text1;
Button bclear,bpoint,beq,badd,bsbb,bmult,bdiv;
Button[] b=new Button[10];
public void init()
{
p1=new Panel();p2=new Panel();p3=new Panel();
setLayout(new FlowLayout());
p1.setLayout(new FlowLayout());
p2.setLayout(new GridLayout(4,3));
p3.setLayout(new GridLayout(4,1));
label=new Label("¼òÒ×¼ÆËãÆ÷");
text1=new TextField(12);
bclear=new Button("Clear");
add(label); //°ÑÎı¾¿òºÍÇå¿Õ°´Å¥¼ÓÔÚµÚÒ»¸ö°åÉÏ
p1.add(text1);p1.add(bclear);
bclear.addActionListener(this);
for(int i=0;i<10;i++)
{
b[i]=new Button(Integer.toString(i));
} bpoint=new Button(".");
beq=new Button("="); //°ÑËùÓÐÊý×Ö°´Å¥ºÍ"."¼°"="¼ÓÔÚµÚ¶þ¸ö°åÉÏ
for(int i=0;i<10;i++)
{
p2.add(b[i]);
b[i].addActionListener(this);
}
p2.add(bpoint);p2.add(beq);
bpoint.addActionListener(this);
beq.addActionListener(this); //°ÑÔËËã·ûºÅ°´Å¥¼ÓÔÚµÚÈý¸ö°åÉÏ
badd=new Button("+");
bsbb=new Button("-");
bmult=new Button("*");
bdiv=new Button("/");
p3.add(badd);
p3.add(bsbb);
p3.add(bmult);
p3.add(bdiv); badd.addActionListener(this);
bsbb.addActionListener(this);
bmult.addActionListener(this);
bdiv.addActionListener(this); add(p1);add(p2);add(p3);add(new Label(" ")); }
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<10;i++){
if(e.getSource()==b[i]||e.getSource()==bpoint){
s=s+e.getActionCommand();
text1.setText(s);
break;
}
} //ÅжÏÔËËã·ûºÅ£¬²¢×÷Éϱê¼Ç
if(e.getSource()==badd){
x=Double.parseDouble(s);
flag=1;
text1.setText("");
s="";
}
if(e.getSource()==bsbb){
x=Double.parseDouble(s);
flag=2;
text1.setText("");
s="";
}
if(e.getSource()==bmult){
x=Double.parseDouble(s);
flag=3;
text1.setText("");
s="";
}
if(e.getSource()==bdiv){
x=Double.parseDouble(s);
flag=4;
text1.setText("");
s="";
} //Çå¿Õ²¢±ê־Ϊ0
if(e.getSource()==bclear){
text1.setText("");
s="";
flag=0;
} //ÔËËã
if(e.getSource()==beq){
switch(flag){
case 1:
{
x=Double.parseDouble(s)+x;
String s=String.valueOf(x);
text1.setText(s);break;
} case 2:
{
x=x-Double.parseDouble(s);
String s=String.valueOf(x);
text1.setText(s);break;
}
case 3:
{
x=Double.parseDouble(s)*x;
String s=String.valueOf(x);
text1.setText(s);break;
}
case 4:
{
if(Double.parseDouble(s)==0){text1.setText("³ýÊý²»ÄÜΪ0!");break;}
x=x/Double.parseDouble(s);
String s=String.valueOf(x);
text1.setText(s);break;
}
}
}
}
}
import java.awt.event.*;
import java.applet.Applet; public class jsq extends Applet implements ActionListener
{
int flag=0;
double x;
String s=new String("");
Panel p1,p2,p3;
Label label;
TextField text1;
Button bclear,bpoint,beq,badd,bsbb,bmult,bdiv;
Button[] b=new Button[10];
public void init()
{
p1=new Panel();p2=new Panel();p3=new Panel();
setLayout(new FlowLayout());
p1.setLayout(new FlowLayout());
p2.setLayout(new GridLayout(4,3));
p3.setLayout(new GridLayout(4,1));
label=new Label("¼òÒ×¼ÆËãÆ÷");
text1=new TextField(12);
bclear=new Button("Clear");
add(label); //°ÑÎı¾¿òºÍÇå¿Õ°´Å¥¼ÓÔÚµÚÒ»¸ö°åÉÏ
p1.add(text1);p1.add(bclear);
bclear.addActionListener(this);
for(int i=0;i<10;i++)
{
b[i]=new Button(Integer.toString(i));
} bpoint=new Button(".");
beq=new Button("="); //°ÑËùÓÐÊý×Ö°´Å¥ºÍ"."¼°"="¼ÓÔÚµÚ¶þ¸ö°åÉÏ
for(int i=0;i<10;i++)
{
p2.add(b[i]);
b[i].addActionListener(this);
}
p2.add(bpoint);p2.add(beq);
bpoint.addActionListener(this);
beq.addActionListener(this); //°ÑÔËËã·ûºÅ°´Å¥¼ÓÔÚµÚÈý¸ö°åÉÏ
badd=new Button("+");
bsbb=new Button("-");
bmult=new Button("*");
bdiv=new Button("/");
p3.add(badd);
p3.add(bsbb);
p3.add(bmult);
p3.add(bdiv); badd.addActionListener(this);
bsbb.addActionListener(this);
bmult.addActionListener(this);
bdiv.addActionListener(this); add(p1);add(p2);add(p3);add(new Label(" ")); }
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<10;i++){
if(e.getSource()==b[i]||e.getSource()==bpoint){
s=s+e.getActionCommand();
text1.setText(s);
break;
}
} //ÅжÏÔËËã·ûºÅ£¬²¢×÷Éϱê¼Ç
if(e.getSource()==badd){
x=Double.parseDouble(s);
flag=1;
text1.setText("");
s="";
}
if(e.getSource()==bsbb){
x=Double.parseDouble(s);
flag=2;
text1.setText("");
s="";
}
if(e.getSource()==bmult){
x=Double.parseDouble(s);
flag=3;
text1.setText("");
s="";
}
if(e.getSource()==bdiv){
x=Double.parseDouble(s);
flag=4;
text1.setText("");
s="";
} //Çå¿Õ²¢±ê־Ϊ0
if(e.getSource()==bclear){
text1.setText("");
s="";
flag=0;
} //ÔËËã
if(e.getSource()==beq){
switch(flag){
case 1:
{
x=Double.parseDouble(s)+x;
String s=String.valueOf(x);
text1.setText(s);break;
} case 2:
{
x=x-Double.parseDouble(s);
String s=String.valueOf(x);
text1.setText(s);break;
}
case 3:
{
x=Double.parseDouble(s)*x;
String s=String.valueOf(x);
text1.setText(s);break;
}
case 4:
{
if(Double.parseDouble(s)==0){text1.setText("³ýÊý²»ÄÜΪ0!");break;}
x=x/Double.parseDouble(s);
String s=String.valueOf(x);
text1.setText(s);break;
}
}
}
}
}
public HelpDialog(Frame f){
super(f);
this.setLabel("Help");
this.setFilename("xxx.txt");
this.setTitle("帮助");
}
}Computer.javaimport java.awt.*;
import java.awt.event.*;public class Computer implements ActionListener{
private Frame f = new Frame("计算器");
private Panel p = new Panel();
private Panel p1 = new Panel();
private Panel p2 = new Panel();
private Panel p3 = new Panel();
private AboutDialog d ;
private HelpDialog d1 ;
private Button b[] = new Button[21];
private Button b1[] = new Button[4];
private Button b2[] = new Button[5];
private TextField tf = new TextField();
private TextField tf1 = new TextField(1);
private String s,oper,s2;
private boolean flag = true;
private double result, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
public void run(){
s = "";
oper = "";
s2 = "";
//tf.setEditable(false);
tf1.setEditable(false);
MenuBar mb = new MenuBar();
f.setLayout(new BorderLayout(5,5));
f.setBackground(Color.gray);
f.setLocation(300,200);
p.setLayout(new BorderLayout(3,3));
p.setBackground(Color.gray);
p1.setLayout(new GridLayout(4,5));
p2.setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
p3.setLayout(new GridLayout(4,1));
f.setMenuBar(mb);
Menu m1 = new Menu("编辑");
Menu m2 = new Menu("关于");
Menu m3 = new Menu("帮助");
mb.add(m1);
mb.add(m2);
mb.setHelpMenu(m3);
MenuItem mi1 = new MenuItem("清除");
MenuItem mi2 = new MenuItem("退出");
MenuItem mi3 = new MenuItem("关于计算器");
MenuItem mi4 = new MenuItem("帮助主题");
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
m2.add(mi3);
m3.add(mi4);
mi1.addActionListener(this);
mi2.addActionListener(this);
//mi3.addActionListener(this);
//mi4.addActionListener(this);
//--------------------------------------------------------------
mi3.addActionListener(new MyHandler());
mi4.addActionListener(new MyHandler());
//--------------------------------------------------------------
for(int i=1;i<4;i++){
b1[i] = new Button();
b1[i].setFont(new Font("仿宋",0,16));
}
b1[1].setLabel(" 退格 ");
b1[2].setLabel("二进制转换");
b1[3].setLabel(" CE ");
p2.add(tf1);
for(int i=1;i<4;i++){
p2.add(b1[i]);
b1[i].addActionListener(this);
}
for(int i=1;i<5;i++){
b2[i] = new Button();
b2[i].setFont(new Font("仿宋",0,16));
}
b2[1].setLabel("MC");
b2[2].setLabel("MR");
b2[3].setLabel("MS");
b2[4].setLabel("M+");
for(int i=1;i<5;i++){
p3.add(b2[i]);
b2[i].addActionListener(this);
}
for(int i=1;i<21;i++){
b[i] = new Button();
b[i].setFont(new Font("仿宋",0,16));
}
b[1].setLabel("7");
b[2].setLabel("8");
b[3].setLabel("9");
b[4].setLabel("/");
b[5].setLabel("squrt");
b[6].setLabel("4");
b[7].setLabel("5");
b[8].setLabel("6");
b[9].setLabel("*");
b[10].setLabel("%");
b[11].setLabel("1");
b[12].setLabel("2");
b[13].setLabel("3");
b[14].setLabel("-");
b[15].setLabel("1/x");
b[16].setLabel("0");
b[17].setLabel("+/-");
b[18].setLabel(".");
b[19].setLabel("+");
b[20].setLabel("=");
for(int i=1;i<21;i++){
p1.add(b[i]);
b[i].addActionListener(this);
b[i].setBackground(new Color(38,99,224));
b[i].setForeground(Color.yellow);
}
for(int i=4;i<15;i=i+5){
b[i].setBackground(new Color(120,180,170));
b[i].setForeground(Color.red);
}
for(int i=17;i<21;i++){
b[i].setBackground(new Color(120,180,170));
b[i].setForeground(Color.red);
}
for(int i=1;i<4;i++){
b[i*5].setBackground(new Color(20,130,180));
b[i*5].setForeground(Color.pink);
}
f.add(tf,"North");
p.add(p1,"Center");
p.add(p2,"North");
p.add(p3,"West");
f.add(p,"Center");
f.pack();
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
}
public void actionPerformed(ActionEvent e){
s = e.getActionCommand();
String s1 = tf.getText(); if(s=="0"|s=="1"|s=="2"|s=="3"|s=="4"|s=="5"|s=="6"|s=="7"|s=="8"|s=="9"){
if(flag){
tf.setText(s1 + s);
}else{
tf.setText(s);
flag = true;
}
}else if(s=="+"|s=="-"|s=="*"|s=="/"){
result = Double.parseDouble(s1);
flag = false;
oper = s;
}else if(s=="="){
tmp1 = Double.parseDouble(tf.getText());
if(oper == "+") result += tmp1;
if(oper == "-") result -= tmp1;
if(oper == "*") result *= tmp1;
if(oper == "/") result /= tmp1;
tf.setText(Double.toString(result));
oper = "";
flag = false;
}else if(s=="+/-"){
tmp2 = -1*Double.parseDouble(tf.getText());
tf.setText(Double.toString(tmp2));
}else if(s=="squrt"){
tmp3 = Double.parseDouble(tf.getText());
if(tmp3>0){
tmp3 = Math.sqrt(tmp3);
tf.setText(Double.toString(tmp3));
flag = false;
}else{}
}else if(s=="1/x"){
tmp4 = 1/Double.parseDouble(tf.getText());
tf.setText(Double.toString(tmp4));
flag = false;
}else if(s=="%"){
tmp5 = Double.parseDouble(tf.getText())/100;
if(oper == "*"){
result = result*(result*tmp5);
tf.setText(Double.toString(result));
}else if(oper == "+"){
result = result+result*tmp5;
tf.setText(Double.toString(result));
}else if(oper == "-"){
result = result-result*tmp5;
tf.setText(Double.toString(result));
}else if(oper == "/"){
result = result/(result*tmp5);
tf.setText(Double.toString(result));
}else{
tf.setText("0.0");
}
flag = false;
oper = "";
}else if(s==" 退格 "){
String after = s1.substring(0,(s1.length()-1));
tf.setText(after);
}else if(s=="."){
if(s1.indexOf('.')==-1){
tf.setText(s1+s);
}else{}
}else if(s==" CE "|s=="清除"){
tf.setText("");
flag = false;
}else if(s=="退出"){
System.exit(1);
}
/*----------------------------------------------------------------
else if(s=="关于计算器"){
d = new AboutDialog(f);
d.display();
}else if(s=="帮助主题"){
d1 = new HelpDialog(f);
d1.display();
}
-------------------------------------------------------------------*/
else if(s=="二进制转换"){
String after="";
long data;
tmp6 = Double.parseDouble(tf.getText());
data = Math.round(tmp6);
int b = 1<<31;
for(int i=0;i<32;i++){
if((data&b)!=0){
after=after+"1";
}else{
after=after+"0";
}
b=b>>>1;
}
after = after.substring(after.indexOf("1"));
tf.setText(after);
flag = false;
}else if(s=="MS"){
String before = tf.getText();
tmp7 = Double.parseDouble(before);
s2 = Double.toString(tmp7);
tf1.setText(" M");
flag = false;
}else if(s=="MR"){
tf.setText(s2);
flag = false;
}else if(s=="MC"){
s2 = "";
tf1.setText("");
}else if(s=="M+"){
tmp7 = tmp7 + Double.parseDouble(tf.getText());
s2 = Double.toString(tmp7);
flag = false;
}
}
public static void main(String args[]){
Computer c = new Computer();
c.run();
}
//-----------------------------------------------------------------
private class MyHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if(s=="关于计算器"){
d = new AboutDialog(f);
d.display();
}else if(s=="帮助主题"){
d1 = new HelpDialog(f);
d1.display();
}
}
}
//-----------------------------------------------------------------
}
import java.awt.*;
import java.awt.event.*;public class AboutDialog extends Dialog implements ActionListener{
private Label l;
private Button b;
private TextArea ta;
private Panel p1;
private Panel p2;
private Panel p3;
private Canvas c1, c2, c3, c4;
private String filename = "xxx.txt";
private String title = "关于计算器";
private String content = "";
public AboutDialog(Frame f){
super(f,true);
this.setTitle(title);
l = new Label("About Calculator V 1.00");
l.setForeground(Color.blue);
l.setFont(new Font("Arial",Font.PLAIN,24));
b = new Button(" 确定 ");
b.addActionListener(this); //(new ButtonHandler());
ta = new TextArea("",30,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
ta.setBackground(Color.lightGray);
ta.setEditable(false);
c1 = new Canvas();
c2 = new Canvas();
c3 = new Canvas();
c4 = new Canvas();
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p2.setLayout(new BorderLayout(5,5));
p1.setBackground(Color.lightGray);
p2.setBackground(new Color(130,254,240,200));
p3.setBackground(Color.lightGray);
p1.add(l);
p2.add(ta);
p2.add(c1,"North");
p2.add(c2,"South");
p2.add(c3,"West");
p2.add(c4,"East");
p3.add(b);
this.add(p1,"North");
this.add(p2,"Center");
this.add(p3,"South");
this.setLocation(f.getX() + f.getWidth(), f.getY());
this.addWindowListener(new WindowHandler());
}
public void display(){
this.setSize(350,350);
this.readfromFile(filename);
this.setTextArea();
this.setResizable(false);
this.setVisible(true);
}
public void readfromFile(String s){
try{
BufferedReader br = new BufferedReader(new FileReader(s));
String s1 = br.readLine();
String s2 = "";
while(s1 != null){
s2 = s2 + s1 + "\n";
s1 = br.readLine();
}
br.close();
content = s2;
}catch(IOException e){
e.printStackTrace();
}
}
public void setFilename(String s){
filename = s;
}
public void setLabel(String s){
l.setText(s);
}
public void setTextArea(){
ta.setText(content);
}
// private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
dispose();
}
// }
private class WindowHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
dispose();
//System.exit(1);
}
}
}