主题:关于计算器的程序
import java.awt.*;
import java.math.*;
import java.text.*;
import java.awt.event.*;
public class Cal implements ActionListener
{
Frame f;
TextField tf1,tf2;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,plus,minus,mult,divide,equal,percent,C,sqrt,sign,opp,dot,BP,CE,MR,MS,MC,M;
double n1,n2,n,save;
int flag[]={0,0,0,0,0,0,0,0,0},state=0;
String tmp1="",tmp2="",tmp="";
public void display()
{
f=new Frame("计算器");
f.setLayout(new FlowLayout());
f.setBackground(Color.LIGHT_GRAY);
f.setBounds(32,32,250,210);
f.setResizable(false);
//Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Panel p4=new Panel();
f.add(p2);
f.add(p3); f.add(p4);
p2.setSize(240,8);
p3.setSize(240,30);
p4.setSize(260,120);
p2.setLayout(new GridLayout(1,1));
p3.setLayout(new FlowLayout());
p4.setLayout(new GridLayout(4,6));
p2.setBackground(Color.LIGHT_GRAY);
p3.setBackground(Color.LIGHT_GRAY);
p4.setBackground(Color.LIGHT_GRAY);
p2.setForeground(Color.red);
p3.setForeground(Color.red);
p4.setForeground(Color.red);
/* MenuBar myMenu=new MenuBar();
Menu edit=new Menu("编辑(E)");
Menu search=new Menu("查看(S)");
Menu help=new Menu("帮助(H)");
MenuItem copy=new MenuItem("复制(C)");
MenuItem paste=new MenuItem("粘贴(P)");
MenuItem standard=new MenuItem("标准型");
MenuItem scientice=new MenuItem("科学型");
MenuItem help1=new MenuItem("帮助主题");
MenuItem help2=new MenuItem("关于计算器");
help2.addActionListener(this);
edit.add(copy); edit.add(paste);
search.add(standard); search.add(scientice);
help.add(help1); help.add(help2);
myMenu.add(edit);myMenu.add(search);myMenu.add(help);
f.setMenuBar(myMenu);*/
tf1=new TextField(28);
tf1.setSize(28,6);
tf2=new TextField(" ");
tf1.setEditable(false);
tf1.setBackground(Color.white);
tf2.setEditable(false);
p2.add(tf1);
BP=new Button(" Backspace "); CE=new Button(" CE ");C=new Button(" C ");
p3.add(tf2);p3.add(BP);p3.add(CE);p3.add(C);
MC=new Button("MC");b7=new Button("7"); b8=new Button("8");b9=new Button("9"); divide=new Button("/");sqrt=new Button("sqrt");
MR=new Button("MR");b4=new Button("4"); b5=new Button("5");b6=new Button("6"); mult=new Button("*"); percent=new Button("%");
MS=new Button("MS");b1=new Button("1"); b2=new Button("2");b3=new Button("3"); minus=new Button("-"); opp=new Button("1/x");
M=new Button("M+");b0=new Button("0"); sign=new Button("+/-");dot=new Button("."); plus=new Button("+");equal=new Button("=");
p4.add(MC); p4.add(b7); p4.add(b8); p4.add(b9); p4.add(divide); p4.add(sqrt);
p4.add(MR); p4.add(b4); p4.add(b5); p4.add(b6); p4.add(mult); p4.add(percent);
p4.add(MS); p4.add(b1); p4.add(b2); p4.add(b3); p4.add(minus); p4.add(opp);
p4.add(M); p4.add(b0); p4.add(sign);p4.add(dot); p4.add(plus);p4.add(equal);
MC.addActionListener(this); MR.addActionListener(this); MS.addActionListener(this); M.addActionListener(this);
b0.addActionListener(this); sign.addActionListener(this); dot.addActionListener(this);
b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this);
b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this);
b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this);
plus.addActionListener(this); minus.addActionListener(this); mult.addActionListener(this);
divide.addActionListener(this); percent.addActionListener(this); BP.addActionListener(this);
CE.addActionListener(this); C.addActionListener(this); equal.addActionListener(this);
sqrt.addActionListener(this); opp.addActionListener(this);
f.addWindowListener(new WinClose());
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
try
{
if (e.getSource()==C) tf1.setText("");
else if(e.getSource()==BP){}
else if(e.getSource()==CE){}
else if(e.getSource()==MC)
{
tf2.setText("");
}
else if(e.getSource()==MR)
{
}
else if(e.getSource()==MS){}
else if(e.getSource()==M)
{
save=Double.parseDouble(tf1.getText());
tf2.setForeground(Color.black);
tf2.setText(" M ");
}
else if(e.getSource()==plus||e.getSource()==minus||e.getSource()==mult||
e.getSource()==divide||e.getSource()==percent||e.getSource()==opp||
e.getSource()==sqrt||e.getSource()==equal||e.getSource()==sign)
{
state=1;
if(e.getSource()==plus)
{
n1=n2;
flag[0]++;
}
else if(e.getSource()==minus)
{
n1=n2;
flag[1]++;
}
else if(e.getSource()==mult)
{
n1=n2;
flag[2]++;
}
else if(e.getSource()==divide)
{
n1=n2;
flag[3]++;
}
else if(e.getSource()==sqrt)
{
n1=n2;
flag[4]++;
}
else if(e.getSource()==opp)
{
n1=n2;
flag[5]++;
}
else if(e.getSource()==equal)
{
if(flag[0]==1) {n=n1+n2;flag[0]=0;}
if(flag[1]==1) {n=n1-n2;flag[1]=0;}
if(flag[2]==1) {n=n1*n2;flag[2]=0;}
if(flag[3]==1) {n=n1/n2;flag[3]=0;}
if(flag[4]==1) {n=Math.sqrt(n1);flag[4]=0;}
if(flag[5]==1) {n=1/n2;flag[5]=0;}
if(flag[6]==1) {n=n1+n2;flag[6]=0;}
if(flag[7]==1) {n=n1+n2;flag[7]=0;}
tmp=String.valueOf(n);
tf1.setText(tmp);
}
else{}
}
else
{
if(state==1)
{
tf1.setText("");
state=0;
}
tf1.setText(tf1.getText()+e.getActionCommand());
n2=Double.parseDouble(tf1.getText());
}
}
catch(Exception e1)
{}
}
public static void main(String args[])
{
(new Cal()).display();
}
class WinClose implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e){};
public void windowIconified(WindowEvent e){};
public void windowDeiconified(WindowEvent e){};
public void windowClosed(WindowEvent e){};
public void windowActivated(WindowEvent e){};
public void windowDeactivated(WindowEvent e){};
}
}
不知道是哪里的问题 除法 平方根 不对
import java.math.*;
import java.text.*;
import java.awt.event.*;
public class Cal implements ActionListener
{
Frame f;
TextField tf1,tf2;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,plus,minus,mult,divide,equal,percent,C,sqrt,sign,opp,dot,BP,CE,MR,MS,MC,M;
double n1,n2,n,save;
int flag[]={0,0,0,0,0,0,0,0,0},state=0;
String tmp1="",tmp2="",tmp="";
public void display()
{
f=new Frame("计算器");
f.setLayout(new FlowLayout());
f.setBackground(Color.LIGHT_GRAY);
f.setBounds(32,32,250,210);
f.setResizable(false);
//Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Panel p4=new Panel();
f.add(p2);
f.add(p3); f.add(p4);
p2.setSize(240,8);
p3.setSize(240,30);
p4.setSize(260,120);
p2.setLayout(new GridLayout(1,1));
p3.setLayout(new FlowLayout());
p4.setLayout(new GridLayout(4,6));
p2.setBackground(Color.LIGHT_GRAY);
p3.setBackground(Color.LIGHT_GRAY);
p4.setBackground(Color.LIGHT_GRAY);
p2.setForeground(Color.red);
p3.setForeground(Color.red);
p4.setForeground(Color.red);
/* MenuBar myMenu=new MenuBar();
Menu edit=new Menu("编辑(E)");
Menu search=new Menu("查看(S)");
Menu help=new Menu("帮助(H)");
MenuItem copy=new MenuItem("复制(C)");
MenuItem paste=new MenuItem("粘贴(P)");
MenuItem standard=new MenuItem("标准型");
MenuItem scientice=new MenuItem("科学型");
MenuItem help1=new MenuItem("帮助主题");
MenuItem help2=new MenuItem("关于计算器");
help2.addActionListener(this);
edit.add(copy); edit.add(paste);
search.add(standard); search.add(scientice);
help.add(help1); help.add(help2);
myMenu.add(edit);myMenu.add(search);myMenu.add(help);
f.setMenuBar(myMenu);*/
tf1=new TextField(28);
tf1.setSize(28,6);
tf2=new TextField(" ");
tf1.setEditable(false);
tf1.setBackground(Color.white);
tf2.setEditable(false);
p2.add(tf1);
BP=new Button(" Backspace "); CE=new Button(" CE ");C=new Button(" C ");
p3.add(tf2);p3.add(BP);p3.add(CE);p3.add(C);
MC=new Button("MC");b7=new Button("7"); b8=new Button("8");b9=new Button("9"); divide=new Button("/");sqrt=new Button("sqrt");
MR=new Button("MR");b4=new Button("4"); b5=new Button("5");b6=new Button("6"); mult=new Button("*"); percent=new Button("%");
MS=new Button("MS");b1=new Button("1"); b2=new Button("2");b3=new Button("3"); minus=new Button("-"); opp=new Button("1/x");
M=new Button("M+");b0=new Button("0"); sign=new Button("+/-");dot=new Button("."); plus=new Button("+");equal=new Button("=");
p4.add(MC); p4.add(b7); p4.add(b8); p4.add(b9); p4.add(divide); p4.add(sqrt);
p4.add(MR); p4.add(b4); p4.add(b5); p4.add(b6); p4.add(mult); p4.add(percent);
p4.add(MS); p4.add(b1); p4.add(b2); p4.add(b3); p4.add(minus); p4.add(opp);
p4.add(M); p4.add(b0); p4.add(sign);p4.add(dot); p4.add(plus);p4.add(equal);
MC.addActionListener(this); MR.addActionListener(this); MS.addActionListener(this); M.addActionListener(this);
b0.addActionListener(this); sign.addActionListener(this); dot.addActionListener(this);
b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this);
b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this);
b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this);
plus.addActionListener(this); minus.addActionListener(this); mult.addActionListener(this);
divide.addActionListener(this); percent.addActionListener(this); BP.addActionListener(this);
CE.addActionListener(this); C.addActionListener(this); equal.addActionListener(this);
sqrt.addActionListener(this); opp.addActionListener(this);
f.addWindowListener(new WinClose());
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
try
{
if (e.getSource()==C) tf1.setText("");
else if(e.getSource()==BP){}
else if(e.getSource()==CE){}
else if(e.getSource()==MC)
{
tf2.setText("");
}
else if(e.getSource()==MR)
{
}
else if(e.getSource()==MS){}
else if(e.getSource()==M)
{
save=Double.parseDouble(tf1.getText());
tf2.setForeground(Color.black);
tf2.setText(" M ");
}
else if(e.getSource()==plus||e.getSource()==minus||e.getSource()==mult||
e.getSource()==divide||e.getSource()==percent||e.getSource()==opp||
e.getSource()==sqrt||e.getSource()==equal||e.getSource()==sign)
{
state=1;
if(e.getSource()==plus)
{
n1=n2;
flag[0]++;
}
else if(e.getSource()==minus)
{
n1=n2;
flag[1]++;
}
else if(e.getSource()==mult)
{
n1=n2;
flag[2]++;
}
else if(e.getSource()==divide)
{
n1=n2;
flag[3]++;
}
else if(e.getSource()==sqrt)
{
n1=n2;
flag[4]++;
}
else if(e.getSource()==opp)
{
n1=n2;
flag[5]++;
}
else if(e.getSource()==equal)
{
if(flag[0]==1) {n=n1+n2;flag[0]=0;}
if(flag[1]==1) {n=n1-n2;flag[1]=0;}
if(flag[2]==1) {n=n1*n2;flag[2]=0;}
if(flag[3]==1) {n=n1/n2;flag[3]=0;}
if(flag[4]==1) {n=Math.sqrt(n1);flag[4]=0;}
if(flag[5]==1) {n=1/n2;flag[5]=0;}
if(flag[6]==1) {n=n1+n2;flag[6]=0;}
if(flag[7]==1) {n=n1+n2;flag[7]=0;}
tmp=String.valueOf(n);
tf1.setText(tmp);
}
else{}
}
else
{
if(state==1)
{
tf1.setText("");
state=0;
}
tf1.setText(tf1.getText()+e.getActionCommand());
n2=Double.parseDouble(tf1.getText());
}
}
catch(Exception e1)
{}
}
public static void main(String args[])
{
(new Cal()).display();
}
class WinClose implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e){};
public void windowIconified(WindowEvent e){};
public void windowDeiconified(WindowEvent e){};
public void windowClosed(WindowEvent e){};
public void windowActivated(WindowEvent e){};
public void windowDeactivated(WindowEvent e){};
}
}
不知道是哪里的问题 除法 平方根 不对