正文

Java基础计算2007-12-16 21:45:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/tld5yj/31465.html

分享到:

一个小程序,自行编写一个HTML文件嵌入运行就行了。

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Add extends Applet implements ActionListener
{Label ask,result;TextField answer;Button submit;int a=6,b=9;
 public static void main(String args[])
 {Add a=new Add();
  a.init();
 }
 public void init()
 {Panel p=new Panel();
  setLayout(new BorderLayout());
  p.setLayout(null);
  ask=new Label(Integer.toString(a)+"+"+Integer.toString(b));
  ask.setFont(new Font("宋体",Font.BOLD,16));
  answer=new TextField(5);
  result=new Label();
  submit=new Button("OK");
  submit.addActionListener(this);
  p.add(ask);p.add(answer);p.add(result);p.add(submit);
  ask.setBounds(50,20,70,20);answer.setBounds(160,20,70,20);
  result.setBounds(50,60,90,20);submit.setBounds(160,60,70,20);
  add("Center",p);p.setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {try
  {if(Integer.valueOf(answer.getText())==a+b)result.setText("正确");
   else result.setText("不正确");
  }
  catch(Exception ee)
  {result.setText("请输入正确格式");
  }
 }
}

阅读(3199) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册