本人不才,做本题做了5个小时都搞不定。求大神帮忙完成一下功能。 我压力大!
package luo;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class huatu extends JFrame implements ActionListener{
private JTextField text_a,text_b,text_c,text_d;
private JRadioButton radio_ju,radio_tuoyuan;
JButton a;
private RoseCanvas rose;
public huatu()
{
super("绘制平面图形并计算周长和面积");
Dimension dim=getToolkit().getScreenSize();
this.setBounds(dim.width/4, dim.height/4,dim.width/2,dim.height/2);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jpanel=new JPanel();
ButtonGroup bgroup=new ButtonGroup();
radio_ju=new JRadioButton("矩形",true);
bgroup.add(radio_ju);
jpanel.add(radio_ju);
radio_tuoyuan=new JRadioButton("椭圆");
bgroup.add(radio_tuoyuan);
jpanel.add(radio_tuoyuan);
jpanel.add(new JLabel("长度"));
text_a=new JTextField(8);
jpanel.add(text_a);
jpanel.add(new JLabel("宽度"));
text_a=new JTextField(8);
jpanel.add(text_a);
a=new JButton("绘图");
jpanel.add(a);
this.getContentPane().add(jpanel,"North");
a.addActionListener(this);
rose=new RoseCanvas();
this.getContentPane().add(rose,"Center");
JPanel jpanela=new JPanel();
jpanela.add(new JLabel("周长"));
text_c=new JTextField(8);
jpanela.add(text_c);
jpanela.add(new JLabel("面积"));
text_d=new JTextField(8);
jpanela.add(text_d);
this.getContentPane().add(jpanela,"South");
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
final double pi=3.14;
if(e.getSource()==a)
{
double x=Double.parseDouble(text_a.getText());
double x1=Double.parseDouble(text_b.getText());
if(radio_ju.isSelected())
{
text_a.setText(String.valueOf(x*2+x1*2));
text_b.setText(String.valueOf(x*x1));
}
else
{
text_a.setText(String.valueOf(x1*2*pi+4*(x-x1)));
text_b.setText(String.valueOf((x/2)*(x1/2)));
}
rose.repaint();
}
}
public static void main(String[] args) {
new huatu();
}
class RoseCanvas extends Canvas
{
public void paint(Graphics g)
{
int y1=Integer.parseInt(text_a.getText());
int y2=Integer.parseInt(text_b.getText());
if(radio_ju.isSelected())
g.fillRect(this.getWidth()/2,this.getHeight()/2,y1/2,y2/2);
else
g.fillOval(this.getWidth()/2,this.getHeight()/2,y1/2,y2/2);
}
}
}