java实现绘制矩形和椭圆并计算面积和周长
/*本文件由开伟同学亲情提供请将以下代码复制到编译器中,此文档完全免费*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PCJFrame extends JFrame implements ActionListener
{
private JRadioButton radio_Rec,radio_Ell;
private JButton button_paint;
private JTextField text_perimeter,text_area;
public JTextField text_length,text_width;
public int l,w; private RectangleCanvas Rectangle;
private EllipseCanvas Ellipse;
public PCJFrame()
{
super("绘制平面图形并计算周长和面积");
this.setSize(600,300);
this.setLocation(200,100);
this.setBackground(Color.white);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel1=new JPanel(new FlowLayout(1));
this.getContentPane().add(panel1,"North");
ButtonGroup bgroup=new ButtonGroup();
radio_Rec=new JRadioButton("矩形",true);
panel1.add(radio_Rec);
bgroup.add(radio_Rec);
radio_Ell=new JRadioButton("椭圆",true);
panel1.add(radio_Ell);
bgroup.add(radio_Ell);
panel1.add(new Label("长度/长轴"));
text_length=new JTextField(10);
panel1.add(text_length);
panel1.add(new Label("宽度/短轴"));
text_width=new JTextField(10);
panel1.add(text_width);
button_paint=new JButton("绘图");
button_paint.addActionListener(this);
panel1.add(button_paint);
JPanel panel2=new JPanel(new FlowLayout(1));
this.getContentPane().add(panel2,"South");
text_perimeter=new JTextField(20);
panel2.add(new Label("周长"));
panel2.add(text_perimeter);
text_area=new JTextField(20);
panel2.add(new Label("面积"));
panel2.add(text_area);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
double length=Double.parseDouble(text_length.getText());
double width=Double.parseDouble(text_width.getText());
l=(int)length;
w=(int)width;
if (Rectangle != null)
{
getContentPane().remove(Rectangle);
}
if (Ellipse != null)
{
getContentPane().remove(Ellipse);
}
Rectangle=new RectangleCanvas(l,w);