当前位置: 代码迷 >> Java相关 >> 本人不才,做本题做了5个小时都搞不定。求大神帮忙完成一下功能。 我压力大 ...
  详细解决方案

本人不才,做本题做了5个小时都搞不定。求大神帮忙完成一下功能。 我压力大 ...

热度:152   发布时间:2012-06-03 23:29:52.0
本人不才,做本题做了5个小时都搞不定。求大神帮忙完成一下功能。 我压力大!
package luo;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.Component;
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);
        }

    }
           
}
搜索更多相关的解决方案: private  package  public  

----------------解决方案--------------------------------------------------------
至少得贴出来,哪里有问题吧 ,这么乱  
  很难理清思路 ,还有注解....
----------------解决方案--------------------------------------------------------
哪里有问题哦?给出运行错误啊
----------------解决方案--------------------------------------------------------
说不清道不明,看不懂。没有注解,不专业呀
----------------解决方案--------------------------------------------------------
这个位置弄错了
程序代码:
jpanel.add(new JLabel("长度"));
        text_a=new JTextField("0",8); //x
        jpanel.add(text_a);      
        jpanel.add(new JLabel("宽度"));
        text_b=new JTextField("0",8);  //x
        jpanel.add(text_b);

还有这里:
程序代码:
  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_c.setText(String.valueOf(x*2+x1*2));//这里
                    text_d.setText(String.valueOf(x*x1));//x
                }   
                else
                {
                    text_c.setText(String.valueOf(x1*2*pi+4*(x-x1)));//x
                    text_d.setText(String.valueOf((x/2)*(x1/2)));//x
                }   
                rose.repaint();
        }
    }

第一个错误,没有给一个初始化值,导致在实例化rose时,text_a.getText()出错。
第二处错误,将周长、面积值(double)赋给了长、宽,画图时对长、宽的转换为整数值,类型引用出错,无法实现预设功能。

----------------解决方案--------------------------------------------------------
  相关解决方案