问答题

本题程序的功能是计算圆和三角形的面积。通过菜单“选择”可以分别进行圆和三角形面积的计算。单击菜单项“圆面积计算”,窗口中就会显示两个文本框和一个“确定”按钮,在第一个文本框中输入圆的半径,单击“确定”按钮后就可以在第二个文本框中显示圆的面积。单击菜单项“三角形面积计算”,窗口中就会显示4个文本框和一个“确定”按钮,在前三个文本框中分别输入三角形三个边的长度,单击“确定”按钮后,如果三个边的长度不能组成三角形,结果文本框中会给出提示信息,否则显示三角形的面积;如果输入的值不是数值,则会给出提示信息。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
import java.awt.*;
import java.awt.event.*;
class circle extends Panel implements AetionListener
{
double r,area;
TextField radius = null,result = null;
Button b = null;
______;
{
radius = new TextField(10);
result = new TextField(10);
b = new Button("确定");
add (new Label ("输入半径"));
add (radius);
add(new Label("面积是"));
add(result);
add(b);
b.addActionListener (this);
result,setEnabled (false);
}
public void actionPerformed(ActionEvent e)
{
try
{
r = Double.parseDouble (radius.getText ());
area =Math.PI*r*r;
result,setText (""+area);
}
catch (Exception ee)
{
radius.setText ("请输入数字字符");
}
}
}
class triangle extends Panel implements ActionListener
{
double a = 0,b = 0,c = 0,area;
TextField border a = new TextField(6) ;
TextField border b = new TextField(6) ;
TextField border c = new TextField(6) ;
Result = new TextField(24);
Button button = new Button("确定");
triangle ()
{
add(new Label("输入三边的长度"));
add (border_a);
add (border_b);
add (border_c);
add(new Label("面积是:"));
add (result);
add (button);
button,addActionListener (this);
result.setEnabled(false);
}
public void actionPerformed(ActionEvent e)
{
try
{
a = Double.parseDouble{border_a.getText());
b = Double.parseDouble(border_b.getText());
c = Double.parseDouble(border_c.getText());
if(a+b>c&&a+c>b&&c+b>a)
{
double p = (a+b+c)/2;
area = Math.sqrt(p*(p-a)*(p-b)*(p-c));
result.setText(""+ area);
}
else
{
result.setText ("您输入的数字不能形成三角形");
}
}
catch(Exception ee)
{
result.setText ("请输入数字字符");
}
}
}
class Win ______ implements ActionListener
{
MenuBar bar = null;
Menu menu = null;
MenuItem item1,item2;
circle circle;
triangle trangle;
Win()
{
bar = new MenuBar(); menu = new Menu("选择");
setSize(300,200);
item1 = new MenuItem("圆面积计算");
item2 = new MenuItem("三角形面积计算");
menu.add(item1);
menu.add(item2);
bar.add(menu);
setMenuBar(bar);
circle = new circle();
trangle = new triangle();
item1.addActionListener(this);
item2.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == item1)
{
removeAll();
add(circle,"Center");
validate();
}
else
if(e.getSource() == item2)
{
removeAll ();
add (trangle,"Center");
validate ();
}
}
public class advance
{
public static void main (String args[])
{
Win win = new Win();
win.setTitle ("advance ");
win.setBounds (100,100,700,300);
win.setVisible (true);
win.addWindowListener (______)
{
public void windowClosing(WindowEvent e)
{
System.exit (0);
}
});
}
}

【参考答案】

circle()。
extends Frame。
new WindowAdapter()。
热门 试题

问答题
请完成以下程序,首先由一个类Example2_3实现Serializable接口,并有三个成员变量,分别为int型、double型和String型,可以用toString的方法显示这三个成员变量。在main方法中创建这个Example2_3的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为TheSerial.data的文件,并显示成员变量。最后从文件TheSerial.data中读出三个成员变量并显示出来。注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。import java.io.*;class TheSerial implements Serializable{private int intValue;private double doubleValue;private String string;TheSerial(){intValue = 123;doubleValue = 12.34;string = Serialize Test ;}public void setDouble(double d){doubleValue = d;}public void setInt(int i){intValue = i;}public void setString(String s){string = s;}public String toString(){return( int= +intValue+ double= +doubleValue+ string= +string);}}public class Example2_3{public static void main(String argv[]){TheSerial e1 = new TheSerial();TheSerial e2;try{e1.setInt(Integer.parseInt(argv[0]));e1.setDouble(Double.parseDouble(argv[1]));e1.setString[argv[2]);}catch(Exception e){e1.setString(e.getMessage{));}System.out.println(e1);try{FileOutputStream oS = new FileOutputStream( TheSerial.data );ObjectOutputStream oIS = new ObjectOutputStream(oS);______;}catch(IOException ioException){System.out.println(ioException.getMessage());}try{FileInputStream iS = new FileInputStream( TheSerial. data );ObjectInputStream oIS = new ObjectInputStream(iS);______System.out.println(e2);}catch(IOException ioException){System.out.println(ioException.getMessage());}catch(ClassNotFoundException cnfException){System.out.println(cnfException.getMessage());}}}