第16章课后答案

1. import java.applet.*;

import java.awt.*;

import java.awt.event.*; 

import javax.swing.*;

public class Xiti2 extendsApplet implements ActionListener

{  TextField text1,text2;

   Label label;

   public void init()

   { text1=new TextField(10);

      text2=new TextField(20);

      Box box1=Box.createHorizontalBox();

      Box box2=Box.createHorizontalBox();

      Box boxV=Box.createVerticalBox();

      box1.add(new Label("输入一个数回车确定:"));

      box1.add(text1);

      label=new Label("数的平方:");

      box2.add(label);

      box2.add(text2);

      boxV.add(box1);

      boxV.add(box2);

      add(boxV);

      text2.setEditable(false);

      text1.addActionListener(this);

   }

   public void actionPerformed(ActionEvent e)

   { String number=e.getActionCommand();

      try{ double n=Double.parseDouble(number);

           double m=n*n;

           label.setText(n+"的平方:");

           text2.setText(""+m);

           text1.setText("");

           validate();

         }

      catch(NumberFormatException exp)

         { text2.setText(""+exp);

         }

   }

}

2. import java.applet.*;

import java.awt.*;

public class Rect extendsApplet {

   int w,h;

   public void init() {

      Strings1=getParameter("width");  //从html得到"width"的值。

      Strings2=getParameter("height");  //从html得到"height"的值。

      w=Integer.parseInt(s1);

      h=Integer.parseInt(s2);

   }

   public void paint(Graphics g) {

      g.drawRect(10,10,w,h);

   }

}

/*<applet code= Rect.classwidth=280 height=500>

     <Param name="width"  value ="150">

     <Param name="height"  value ="175">

</applet>*/

猜你喜欢

转载自blog.csdn.net/qq_41045071/article/details/80958376