日期:2014-05-20  浏览次数:20756 次

JAVA写的这个图形为什么不能显示
package org.paint;

import java.awt.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.*;
public class Paint extends JFrame implements ActionListener {
  private JRadioButton jradiobutton_round,jradiobutton_rectangle;
  private JLabel jlabel_width,jlabel_length,jlabel_cir,jlabel_area;
  private JTextField text_width,text_length,text_cir,text_area;
  private JButton button_paint;
  private JPanel jpanel1,jpanel2,jpanel3;
  private RoundPaint roundpaint;
  public Paint()
  {
  super("绘制平面图形并计算周长和面积");
  Dimension dim =getToolkit().getScreenSize(); //获得分辨率
  this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.setBackground(java.awt.Color.lightGray);
   
  jradiobutton_round =new JRadioButton("圆形");
  jradiobutton_rectangle = new JRadioButton("矩形");
  ButtonGroup bgp = new ButtonGroup();
  bgp.add(jradiobutton_round);
  bgp.add(jradiobutton_rectangle);
  jradiobutton_round.setSelected(true);
  jlabel_width = new JLabel("宽度");
  jlabel_length = new JLabel("长度");
  text_length = new JTextField(10);
  text_width = new JTextField(10);
  button_paint = new JButton("绘图");
  button_paint.addActionListener(this);
  jpanel1 = new JPanel();
  jpanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
  jpanel1.add(jradiobutton_rectangle);
  jpanel1.add(jradiobutton_round);
  jpanel1.add(jlabel_length);
  jpanel1.add(text_length);
  jpanel1.add(jlabel_width);
  jpanel1.add(text_width);
  jpanel1.add(button_paint);
   
  roundpaint = new RoundPaint();
  //jpanel2 = new JPanel();
  // jpanel2.add(roundpaint);
  // jpanel2.add(new JLabel("rrr"));
  this.getContentPane().add(roundpaint,BorderLayout.CENTER);
   
  jlabel_cir = new JLabel("周长");
  text_cir = new JTextField(10);
  jlabel_area = new JLabel("面积");
  text_area = new JTextField(10);
  jpanel3 = new JPanel();
  jpanel3.setLayout(new FlowLayout(FlowLayout.CENTER));
  jpanel3.add(jlabel_cir);
  jpanel3.add(text_cir);
  jpanel3.add(jlabel_area);
  jpanel3.add(text_area);
   
  //this.getContentPane().setLayout(new BorderLayout());
  this.getContentPane().add(jpanel1,BorderLayout.NORTH);
  this.getContentPane().add(jpanel3,BorderLayout.SOUTH);
  this.setVisible(true);
   
   
  }

public void actionPerformed(ActionEvent arg0) {
double a,b,cir,area;
try{
a = Double.parseDouble(text_length.getText());
b = Double.parseDouble(text_width.getText());

if(jradiobutton_rectangle.isSelected())
{
cir = 2*(a+b);
area = a*b;
}
else
{
cir = Math.PI*(1.5*(a+b)-Math.sqrt(a*b));
area =a*b*Math.PI;
}
text_cir.setText(""+cir);
text_area.setText(""+area);
roundpaint.repaint();
}
catch(Exception e)
{

}
finally