发布网友 发布时间:2024-10-24 03:44
共3个回答
热心网友 时间:2024-10-30 14:53
你的10分太少了!
这个最少也要200!
不然自己 网上搜,自己研究吧!
热心网友 时间:2024-10-30 14:48
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener
{
Frame f;
MenuBar mb1;
Menu mf1,me1,mh1,md1;
Panel p1,p2,p3;
TextField tf1,tf2;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27;
int Ms=0;//用于标识M+,Ms
double m=0;//用于实现连续运算
double k=0;//用于实现负号
double cur=0;//用于保存记忆的值
double k1;//用于实现Backspace
public Calculator()
{
f=new Frame("计算器");
f.setSize(260,200);
f.setLocation(320,320);
f.setLayout(new BorderLayout());
f.setBackground(Color.lightGray);
tf1=new TextField(30);
tf1.setEditable(false);
tf1.setBackground(Color.WHITE);
f.add(tf1,BorderLayout.CENTER);
p1=new Panel(new BorderLayout());
f.add(p1,BorderLayout.SOUTH);
tf2=new TextField(2);
tf2.setEditable(false);
b1=new Button("BackSpace");
b1.setForeground(Color.RED);
b2=new Button(" CE ");
b2.setForeground(Color.RED);
b3=new Button(" C ");
b3.setForeground(Color.RED);
b2.addActionListener(this);
b3.addActionListener(this);
b1.addActionListener(this);
p2=new Panel(new FlowLayout(FlowLayout.RIGHT));
p2.add(tf2);
p2.add(b1);
p2.add(b2);
p2.add(b3);
b24=new Button("MC");
b24.setForeground(Color.RED);
b24.addActionListener(this);
b25=new Button("MR");
b25.setForeground(Color.RED);
b25.addActionListener(this);
b26=new Button("MS");
b26.setForeground(Color.RED);
b26.addActionListener(this);
b27=new Button("M+");
b27.setForeground(Color.RED);
b27.addActionListener(this);
b19=new Button("/");
b19.setForeground(Color.RED);
b19.addActionListener(this);
b18=new Button("*");
b18.setForeground(Color.RED);
b18.addActionListener(this);
b17=new Button("-");
b17.setForeground(Color.RED);
b17.addActionListener(this);
b16=new Button("+");
b16.setForeground(Color.RED);
b16.addActionListener(this);
b14=new Button("+/-");
b14.addActionListener(this);
b15=new Button(".");
b15.addActionListener(this);
b20=new Button("sqrt");
b20.addActionListener(this);
b23=new Button("%");
b23.addActionListener(this);
b21=new Button("1/x");
b21.addActionListener(this);
b22=new Button("=");
b22.setForeground(Color.RED);
b22.addActionListener(this);
b4=new Button("7");
b5=new Button("8");
b6=new Button("9");
b7=new Button("4");
b8=new Button("5");
b9=new Button("6");
b10=new Button("1");
b11=new Button("2");
b12=new Button("3");
b13=new Button("0");
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
p3=new Panel(new GridLayout(4,6,2,2));
p3.add(b24);
p3.add(b4);
p3.add(b5);
p3.add(b6);
p3.add(b21);
p3.add(b19);
p3.add(b20);
p3.add(b25);
p3.add(b7);
p3.add(b8);
p3.add(b9);
p3.add(b18);
p3.add(b23);
p3.add(b26);
p3.add(b10);
p3.add(b11);
p3.add(b12);
p3.add(b17);
p3.add(b21);
p3.add(b27);
p3.add(b13);
p3.add(b14);
p3.add(b15);
p3.add(b16);
p3.add(b22);
p1.add(p2,BorderLayout.NORTH);
p1.add(p3,BorderLayout.CENTER);
f.add(p1,BorderLayout.SOUTH);
f.addWindowListener(new WinClose());
f.setVisible(true);
tf1.setText("");
addmyMenu();
}
public void addmyMenu()
{
mb1 = new MenuBar(); //生成一个菜单栏
f.setMenuBar(mb1); //框架f上添加菜单栏
mf1 = new Menu("编辑(E)"); //生成一个菜单
me1 = new Menu("查看(V)");
mh1 = new Menu("帮助(H)");
mb1.add(mf1); //菜单栏中加入菜单
mb1.add(me1);
mb1.add(mh1);
mf1.add(new MenuItem("复制")); //生成菜单项并加入到菜单
mf1.add(new MenuItem("粘贴"));
mf1.addActionListener(this); //为菜单注册事件监听程序
me1.add(new MenuItem("标准型"));
me1.add(new MenuItem("科学型"));
me1.addSeparator(); //加分隔线
me1.add(new MenuItem("数字分组"));
me1.addActionListener(this);
mh1.add(new MenuItem("帮助主题"));
mh1.add(new MenuItem("关于计算器"));
mh1.addActionListener(this);
}
int flag=0;//运算符有效标识
int num=0;//用于实现连续运算
double dot=0;//用于标识小数点
double first=0,second=0,result=0;//用于保存第一个数,第二个数和计算结果
double push;//用于标识是否有数字键按下
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b3)
{tf1.setText("0.");
num=0;
dot=0;
push=0;
flag=0;
first=0;}
else
if(e.getSource()==b4)
{
if(push==0.)
tf1.setText(e.getActionCommand());
else
tf1.setText(tf1.getText()+e.getActionCommand());
push=1;}
if(e.getSource()==b5)
{
if(push==0.)
tf1.setText(e.getActionCommand());
else
tf1.setText(tf1.getText()+e.getActionCommand());
push=1;}
if(e.getSource()==b6)
{
if(push==0.)
tf1.setText(e.getActionCommand());
else
tf1.setText(tf1.getText()+e.getActionCommand());
push=1;}
if(e.getSource()==b7)
{
if(push==0.)
tf1.setText(e.getActionCommand());
else
tf1.setText(tf1.getText()+e.getActionCommand());
push=1;}
if(e.getSource()==b8)
{
if(push==0.)
tf1.setText(e.getActionCommand());
else
tf1.setText(tf1.getText()+e.getActionCommand());
push=1;}
if(e.getSource()==b9)
{
if(push==0.)
tf1.setText(e.getActionCommand());
else
tf1.setText(tf1.getText()+e.getActionCommand());
push=1;}
if(e.getSource()==b10)
{
if(push==0.)
tf1.setText(e.getActionCommand());
else
tf1.setText(tf1.getText()+e.getActionCommand());
push=1;}
if(e.getSource()==b11)
{
if(push==0.)
tf1.setText(e.getActionCommand());
else
tf1.setText(tf1.getText()+e.getActionCommand());
push=1;}
if(e.getSource()==b12)
{
if(push==0.)
tf1.setText(e.getActionCommand());
else
tf1.setText(tf1.getText()+e.getActionCommand());
push=1;}
if(e.getSource()==b13)
{
if(push==0.)
tf1.setText(e.getActionCommand());
else
tf1.setText(tf1.getText()+e.getActionCommand());}
if(e.getSource()==b26||e.getSource()==b27)
{
tf2.setText("M");
cur=Double.parseDouble(tf1.getText());
Ms=1;
}
if(e.getSource()==b25)
{
if(Ms==1)
tf1.setText(Double.toString(cur));
else
tf1.setText("0.");
}
if(e.getSource()==b24)
{
tf2.setText(" ");
Ms=0;
}
if(e.getSource()==b14)
{
k=Double.parseDouble(tf1.getText());
k=-k;
tf1.setText(Double.toString(k));
}
if(e.getSource()==b15)
{
push=1;
if(dot==0)
tf1.setText(tf1.getText()+e.getActionCommand());
else
tf1.setText(tf1.getText());
dot=1;}
if(e.getSource()==b16)
{
flag=1;
first=Double.parseDouble(tf1.getText());
dot=0;
push=0;
}
if(e.getSource()==b17)
{
flag=2;
first=Double.parseDouble(tf1.getText());
dot=0;
push=0;
}
if(e.getSource()==b18)
{
flag=3;
first=Double.parseDouble(tf1.getText());
dot=0;
push=0;
}
if(e.getSource()==b19)
{
flag=4;
first=Double.parseDouble(tf1.getText());
dot=0;
push=0;
}
if(e.getSource()==b21)
{
flag=5;
first=Double.parseDouble(tf1.getText());
dot=0;
push=0;
}
if(e.getSource()==b23)
{
flag=6;
first=Double.parseDouble(tf1.getText());
dot=0;
push=0;
}
if(e.getSource()==b20)
{
flag=7;
first=Double.parseDouble(tf1.getText());
dot=0;
push=0;
}
if(e.getSource()==b22)
{
second=Double.parseDouble(tf1.getText());
if(num==0)
m=second;
num=1;
switch(flag)
{
case 0: break;
case 1: result=first+second;
first=m;
tf1.setText(String.valueOf(result));
break;
case 2:second=m;
result=first-second;
first=result;
tf1.setText(String.valueOf(result));
break;
case 3:result=first*second;
first=m;
tf1.setText(String.valueOf(result));
break;
case 4:if(second==0)
tf1.setText("除数不能为0");
else
{second=m;
result=first/second;
first=result;
tf1.setText(String.valueOf(result));}
break;
case 5:if(first==0)
tf1.setText("0没有倒数");
else
{result=1/first;
first=result;
tf1.setText(String.valueOf(result));}
break;
case 6:result=first/100;
first=result;
tf1.setText(String.valueOf(result));
break;
case 7:if(first<0)
tf1.setText("负数不能开方");
else{result=Math.sqrt(first);
first=result;
tf1.setText(String.valueOf(result));}
break;
}
dot=0;
push=0;
}
if(e.getSource()==b2)
{
tf1.setText("0.");
}
if(e.getSource()==b1)
{
k1=Double.parseDouble(tf1.getText());
if(k1==0)
tf1.setText(Double.toString(k1));
String s=tf1.getText();
int i;
i=s.length();
s=s.substring(0,i-1);
i=i-1;
if(i==0)
tf1.setText("0.");
else
tf1.setText(s);
push=0;
dot=0;
}
}
public static void main(String arg[])
{
new Calculator();
}
}
class WinClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
热心网友 时间:2024-10-30 14:46
新手吧、自己研究去、
啥都来百度百、自己先研究去 不准抄袭作业