import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyWindow extends JFrame implements ActionListener{
JButton button1,button2;
static JTextField text1,text2;
MyWindow(String s){
super(s);
Container con=this.getContentPane();
con.setLayout(new GridLayout(2,2));
button1=new JButton("求圆面积");
button2=new JButton("求三角形面积");
button1.addActionListener(this);
button2.addActionListener(this);
text1=new JTextField(20);
text2=new JTextField(20);
con.add(button1);con.add(text1);
con.add(button2);con.add(text2);
this.setVisible(true);
this.pack();
}
public static void returnCircleArea(String s){
text1.setText("圆的面积是:"+s);
}
public static void returnTriangleArea(String s){
text2.setText("三角形的面积是:"+s);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button1){
CircleDlg dlg;
dlg=new CircleDlg(this,"圆");
dlg.setVisible(true);
}
else if(e.getSource()==button2){
TriangleDlg dlg;
dlg=new TriangleDlg(this,"三角形");
dlg.setVisible(true);
}
}
}
class CircleDlg extends JDialog implements ActionListener{
JLabel label;
JTextField text;
JButton Cal;
CircleDlg(JFrame F,String s){
super(F,s,true);
Container con=this.getContentPane();
label=new JLabel("请输入半径:");
text=new JTextField(10);text.setEditable(true);
con.setLayout(new FlowLayout());
setModal(true);
Cal=new JButton("计算");
Cal.addActionListener(this);
con.add(label);
con.add(text);
con.add(Cal);
con.setVisible(true);this.pack();
}
public void actionPerformed(ActionEvent e){
double r,s;
r=Double.parseDouble(text.getText());
s=Math.PI*r*r;
MyWindow.returnCircleArea(s+"");
this.setVisible(false);
this.dispose();
}
}
class TriangleDlg extends JDialog implements ActionListener{
JLabel labela,labelb,labelc;
JTextField texta,textb,textc;
JButton cal;
TriangleDlg(JFrame F,String s){
super(F,s,true);
Container con=this.getContentPane();
con.setLayout(new GridLayout(4,2));
labela=new JLabel("请输入边长a:");
labelb=new JLabel("请输入边长b:");
labelc=new JLabel("请输入边长c:");
texta=new JTextField(10);texta.setEditable(true);
textb=new JTextField(10);textb.setEditable(true);
textc=new JTextField(10);textc.setEditable(true);
cal=new JButton("计算");
cal.addActionListener(this);
con.add(labela);con.add(texta);
con.add(labelb);con.add(textb);
con.add(labelc);con.add(textc);
con.add(cal);
con.setVisible(true);this.pack();
}
public void actionPerformed(ActionEvent e){
double a,b,c,l,s;
a=Double.parseDouble(texta.getText());
b=Double.parseDouble(textb.getText());
c=Double.parseDouble(textc.getText());
l=(a+b+c)/2;
s=Math.sqrt(l*(l-a)*(l-b)*(l-c));
MyWindow.returnTriangleArea(s+"");
this.setVisible(false);
this.dispose();
}
}
public class ex0607 extends Applet{
MyWindow window;
public void init(){
window=new MyWindow("练习0607");
}
}