当前位置: 代码迷 >> Java相关 >> 谁能帮我解决这个问题 这个图形总是不能显示在画布上 我纠结啊 我做了很 ...
  详细解决方案

谁能帮我解决这个问题 这个图形总是不能显示在画布上 我纠结啊 我做了很 ...

热度:400   发布时间:2012-06-03 23:17:17.0
谁能帮我解决这个问题 这个图形总是不能显示在画布上 我纠结啊 我做了很久没有做出来。大家帮帮忙,你们可以到Eclipse运行,我描述问题能力有限 谢谢大
package Test;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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 jisuan {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
                new pwx();
    }

}
class pwx extends JFrame implements ActionListener{
    Canvas c;
    //public final double PI=3.14f;
    JRadioButton tuoyuan,juxing;
    JTextField t1,t2,t3,t4;
    JButton bt;
    JLabel l1,l2,l3,l4;
    int lt1,lt2;
    public pwx(){
        super("图形计算");
        Dimension di=getToolkit().getScreenSize();
        this.setBounds(di.width/4, di.height/4, di.width/2, di.height/2);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel jp=new JPanel();
        this.getContentPane().add(jp,"West");
        JPanel dx=new JPanel(new GridLayout(1,2));
        jp.add(dx);
        ButtonGroup bg=new ButtonGroup();
        juxing=new JRadioButton("矩形",true);
        bg.add(juxing);
        dx.add(juxing);
        tuoyuan=new JRadioButton("椭圆");
        bg.add(tuoyuan);
        dx.add(tuoyuan);
        
        
        l1=new JLabel("长度");
        t1=new JTextField(10);
        jp.add(l1);
        jp.add(t1);
        l2=new JLabel("宽度");
        t2=new JTextField(10);
        jp.add(l2);
        jp.add(t2);
        bt=new JButton("绘图");
        jp.add(bt);
        
        
        JPanel jp1=new JPanel();
        this.getContentPane().add(jp1,"South");
        l3=new JLabel("周长");
        t3=new JTextField(10);
        jp1.add(l3);
        jp1.add(t3);
        l4=new JLabel("面积");
        t4=new JTextField(10);
        jp1.add(l4);
        jp1.add(t4);
        
        //c=new Canvas(Color.red);
        //this.getContentPane().add(c,"Center");
        //jp.add(c);
        bt.addActionListener(this);
        juxing.addActionListener(this);
        tuoyuan.addActionListener(this);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent a) {
        // TODO Auto-generated method stub
        if(a.getSource()==bt||a.getSource()==juxing){
            
            //try{
            double cd=Double.parseDouble(t1.getText());
            double kd=Double.parseDouble(t2.getText());
            t3.setText(String.valueOf(cd*2+kd*2));
            t4.setText(String.valueOf(cd*kd));
            }
            //lt1=(int)cd;
            //lt2=(int)kd;
            //c=new Canvas(lt1,lt2);
        //    }
            //catch(Exception e){}
        if(a.getSource()==bt&a.getSource()==tuoyuan)
        {

            double cd1=Double.parseDouble(t1.getText());
            double kd1=Double.parseDouble(t2.getText());
            t3.setText(String.valueOf(2*3.14f*(kd1+cd1)));
            t4.setText(String.valueOf(3.14f*(kd1+cd1)));
            
        }
    }
   
    class pw extends Canvas{
        Color co;
        public pw(Color co){
            //this.setColor(co);
        }
    public void paint(Graphics g){
            g.setColor(Color.black);
            g.fillRect(this.getWidth()/2,this.getHeight()/2, 100, 300);
            g.setColor(Color.black);
            g.fillOval(this.getWidth()/2,this.getHeight()/2, 100, 300);
            
        }
    }
   
}

搜索更多相关的解决方案: 图形  package  public  import  

----------------解决方案--------------------------------------------------------
代码有点乱  希望各位帮帮忙
----------------解决方案--------------------------------------------------------
  相关解决方案