标题:本人不才,做本题做了5个小时都搞不定。求大神帮忙完成一下功能。 我压力大 ...
只看楼主
彭文雄
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-6-3
 问题点数:0 回复次数:4 
本人不才,做本题做了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);
        }

    }
           
}
搜索更多相关主题的帖子: private package public 
2012-06-03 23:29
oscarzhou
Rank: 1
等 级:新手上路
帖 子:5
专家分:5
注 册:2012-6-15
得分:0 
至少得贴出来,哪里有问题吧 ,这么乱  
  很难理清思路 ,还有注解....
2012-06-15 00:49
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
得分:0 
哪里有问题哦?给出运行错误啊
2012-06-15 10:15
ihih1234321
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-7-21
得分:0 
说不清道不明,看不懂。没有注解,不专业呀
2012-07-22 20:43
kongfwy
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-3-13
得分:0 
这个位置弄错了
程序代码:
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)赋给了长、宽,画图时对长、宽的转换为整数值,类型引用出错,无法实现预设功能。
2012-07-29 11:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-370748-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.074815 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved