huajia123456789 2013-12-23 03:21 采纳率: 50%
浏览 2393
已采纳

程序问什么运行不出来

import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//import java.Math.*;
public class cal extends Applet
{
//定义所有需要使用的全局变量
String str="0";//暂存显示屏数据
String sign="null";//暂存符号数据
double num=0;//暂存内部运算数据
boolean change=false;//暂存内部四则运算起用

GridLayout g0,g1;//定义AWT布局部件
JTextField number;
Button num_p,num_0,num_1,num_2,num_3,num_4,num_5,num_6,num_7,num_8,num_9;
Button act_add,act_mul,act_div,act_sub;
Button mat_sin,mat_cos,mat_tan;
Button do_ans,do_del;
Panel p0,p1;

public void init()//主程序部分
{
//定义各个控件的样式
number=new JTextField(20);//显示屏
number.setHorizontalAlignment(JTextField.RIGHT);
number.setEnabled(false);
number.setText("0");

num_p=new Button(".");//小数点的按钮
num_p.setForeground(Color.red);
num_p.addActionListener(new getbt());

num_0=new Button("0");//数字0的按钮
num_0.setForeground(Color.red);
num_0.addActionListener(new getbt());

num_1=new Button("1");//数字1的按钮
num_1.setForeground(Color.red);
num_1.addActionListener(new getbt());

num_2=new Button("2");//数字2的按钮
num_2.setForeground(Color.red);
num_2.addActionListener(new getbt());

num_3=new Button("3");//数字3的按钮
num_3.setForeground(Color.red);
num_3.addActionListener(new getbt());

num_4=new Button("4");//数字4的按钮
num_4.setForeground(Color.red);
num_4.addActionListener(new getbt());

num_5=new Button("5");//数字5的按钮
num_5.setForeground(Color.red);
num_5.addActionListener(new getbt());

num_6=new Button("6");//数字6的按钮
num_6.setForeground(Color.red);
num_6.addActionListener(new getbt());

num_7=new Button("7");//数字7的按钮
num_7.setForeground(Color.red);
num_7.addActionListener(new getbt());

num_8=new Button("8");//数字8的按钮
num_8.setForeground(Color.red);
num_8.addActionListener(new getbt());

num_9=new Button("9");//数字9的按钮
num_9.setForeground(Color.red);
num_9.addActionListener(new getbt());

act_add=new Button("+");//加法的按钮
act_add.setForeground(Color.red);
act_add.addActionListener(new getbt());

act_sub=new Button("-");//减法的按钮
act_sub.setForeground(Color.red);
act_sub.addActionListener(new getbt());

act_mul=new Button("*");//乘法的按钮
act_mul.setForeground(Color.red);
act_mul.addActionListener(new getbt());

act_div=new Button("/");//除法的按钮
act_div.setForeground(Color.red);
act_div.addActionListener(new getbt());

do_ans=new Button("=");//等于的按钮
do_ans.setForeground(Color.red);

do_ans.addActionListener(new getbt());

do_del=new Button("del");//删除的按钮
do_del.setForeground(Color.red);
do_del.addActionListener(new getbt());

mat_sin=new Button("sin");//sin的按钮
mat_sin.setForeground(Color.red);
mat_sin.addActionListener(new getbt());

mat_cos=new Button("cos");//cos的按钮
mat_cos.setForeground(Color.red);
mat_cos.addActionListener(new getbt());

mat_tan=new Button("tan");//tan的按钮
mat_tan.setForeground(Color.red);
mat_tan.addActionListener(new getbt());

//布局整个计算器
g0=new GridLayout(1,1,10,10);//布局输入输出大体位置
g1=new GridLayout(4,5,10,10);

p0=new Panel(); //显示屏的布局
p0.setLayout(g0);
p0.add(number);
p0.setBounds(0,0,100,50);

//按纽的布局
p1=new Panel();
p1.setLayout(g1);
p1.add(num_1);//第一排按纽
p1.add(num_2);
p1.add(num_3);
p1.add(act_add);
p1.add(act_sub);

p1.add(num_4);//第二排按纽
p1.add(num_5);
p1.add(num_6);
p1.add(act_mul);
p1.add(act_div);

p1.add(num_7);//第三排按纽
p1.add(num_8);
p1.add(num_9);
p1.add(mat_sin);
p1.add(mat_cos);

p1.add(num_0);//第四排按钮
p1.add(num_p);
p1.add(do_ans);
p1.add(do_del);
p1.add(mat_tan);
p1.setBounds(5,60,400,250);
p1.setBackground(Color.cyan);//设置按钮背景颜色

setLayout(null);
add(p0);
add(p1);

this.setBackground(Color.cyan);//设置背景整体颜色
}

class getbt implements ActionListener //监听作出反应的类
{
public void actionPerformed(ActionEvent e)//对每个每个按钮做出响应
{
if(e.getSource()==num_0)
press0();
else if(e.getSource()==num_1)
press1();
else if(e.getSource()==num_2)
press2();
else if(e.getSource()==num_3)
press3();
else if(e.getSource()==num_4)
press4();
else if(e.getSource()==num_5)
press5();
else if(e.getSource()==num_6)
press6();
else if(e.getSource()==num_7)
press7();
else if(e.getSource()==num_8)
press8();
else if(e.getSource()==num_9)
press9();
else if(e.getSource()==num_p)
pressp();
else if(e.getSource()==act_add)
pressadd();
else if(e.getSource()==act_mul)
pressmul();
else if(e.getSource()==act_div)
pressdiv();
else if(e.getSource()==act_sub)
presssub();
else if(e.getSource()==mat_sin)
presssin();
else if(e.getSource()==mat_tan)
presstan();
else if(e.getSource()==mat_cos)
presscos();
else if(e.getSource()==do_ans)
pressans();
else if (e.getSource()==do_del)
pressdel();
}

}
public void press0()//对按0作出反应
{
if(str.length()<16 && str!="0" && change==false)
{
str+="0";
}
else if(str=="0" || sign!="null")
{
str="0";
}
number.setText(str);
}

public void press1()//对按1作出反应
{
if(str.length()<16 && str!="0" && change==false)
{
str+="1";
}
else if(str=="0"|| sign!="null")
{
str="1";
change=false;
}
number.setText(str);
}

public void press2()//对按2作出反应
{
if(str.length()<16 && str!="0" && change==false)
{
str+="2";
}
else if(str=="0" || sign!="null")
{
str="2";
change=false;
}
number.setText(str);
}

public void press3()//对按3作出反应
{
if(str.length()<16 && str!="0" && change==false)
{
str+="3";
}
else if(str=="0" || sign!="null")
{
str="3";
change=false;
}
number.setText(str);
}
public void press4()//对按4作出反应
{
if(str.length()<16 && str!="0" && change==false)
{
str+="4";
}
else if(str=="0" || sign!="null")
{
str="4";
change=false;
}
number.setText(str);
}

public void press5()//对按5作出反应
{
if(str.length()<16 && str!="0" && change==false)
{
str+="5";
}
else if(str=="0" || sign!="null")
{
str="5";
change=false;
}
number.setText(str);
}

public void press6()//对按6作出反应
{
if(str.length()<16 && str!="0" && change==false)
{
str+="6";
}
else if(str=="0" || sign!="null")
{
str="6";
change=false;
}
number.setText(str);
}

public void press7()//对按7作出反应
{
if(str.length()<16 && str!="0" && change==false)
{
str+="7";
}
else if(str=="0" || sign!="null")
{
str="7";
change=false;
}
number.setText(str);
}

public void press8()//对按8作出反应
{
if(str.length()<16 && str!="0" && change==false)
{
str+="8";
}
else if(str=="0" || sign!="null")
{
str="8";
change=false;
}
number.setText(str);
}

public void press9()//对按9作出反应
{
if(str.length()<16 && str!="0" && change==false)
{
str+="9";
}
else if(str=="0" || sign!="null")
{
str="9";
change=false;
}
number.setText(str);
}

public void pressp()//对按.作出反应
{
boolean p=false;
for(int i=0;i<str.length();i++)
if(str.charAt(i)=='.')
p=true;
if(str.length()<16 && str!="0" && sign=="null" && p==false)
{
str+=".";
}
else if((str=="0" || sign!="null")&& p==false)
{
str="0.";
}
number.setText(str);
}

public void pressadd()//对按+作出反应
{
sign="add";
num=Double.parseDouble(str);
change=true;
}

public void presssub()//对按-作出反应
{
sign="sub";
num=Double.parseDouble(str);
change=true;
}
public void pressmul()//对按*作出反应
{
sign="mul";
num=Double.parseDouble(str);
change=true;
}
public void pressdiv()//对按/作出反应
{
sign="div";
num=Double.parseDouble(str);
change=true;
}
public void presssin()//对按sin作出反应
{
if(str!="0." && str!="-0" && str!="-0." && str!="0")
{
sign="null";
num=Double.parseDouble(str);
num=Math.sin(num);
str=Double.toString(num);
if(str.length()>16)
str=str.substring(0,16);
num=0;
}
if(str=="0")
{
sign="null";
num=0;
}
number.setText(str);
}
public void presstan()//对按tan作出反应
{
if(str!="0." && str!="-0" && str!="-0."&& str!="0")
{
sign="null";
num=Double.parseDouble(str);
num=Math.tan(num);
str=Double.toString(num);
if(str.length()>16)
str=str.substring(0,16);
num=0;
}
number.setText(str);
}
public void presscos()//对按cos作出反应
{
if(str!="0." && str!="-0" && str!="-0.")
{
sign="null";
num=Double.parseDouble(str);
num=Math.cos(num);
str=Double.toString(num);
if(str.length()>16)
str=str.substring(0,16);
num=0;
}
number.setText(str);
}
public void pressans()//对按=作出反应
{
if(sign=="null")
number.setText(str);
else if(sign=="add")
{
num+=Double.parseDouble(str);
str=Double.toString(num);
number.setText(str);
}
else if(sign=="sub")
{
num-=Double.parseDouble(str);
str=Double.toString(num);
number.setText(str);
}
else if(sign=="mul")
{
num*=Double.parseDouble(str);
str=Double.toString(num);
number.setText(str);
}
else if(sign=="div")
{
if(num!=0)
{
num/=Double.parseDouble(str);
str=Double.toString(num);
number.setText(str);
}
else
{
str="0";
number.setText(str);
}
}
sign="null";
}
public void pressdel()//对按del作出反应
{
if(str.length()>1)
{
str=str.substring(0,str.length()-1);
number.setText(str);
}
else if(str.length()==1)
{
str="0";
number.setText(str);
}
}
}

  • 写回答

3条回答

  • huajia123456789 2013-12-24 04:51
    关注

    我运行了,可以出来呀。一个计算机 不过后天有问题:告:不能读取 AppletViewer 的属性文件: C:\Users\Administrator.hotjava\properties 使用默认值。 erliangban 12-24 10 这句话是什么意思?我不懂,你能再解释一下,方便的话可以截屏看一下吗?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题