一个小程序,自行编写一个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("请输入正确格式"); } }}

评论