首页 > 编程语言 > 详细

Java编写圆的相关计算

时间:2015-11-26 21:09:22      阅读:290      评论:0      收藏:0      [点我收藏+]

希望大神可以帮我完善这个小程序,感激不尽 

 

 

 

技术分享

package 练习;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test  implements ActionListener{
    String[] str = {"1","2","3",
            "4","5","6",
            "7","8","9",
            "0",".","DEL",
            "圆","计算","球"};
    private TextArea tf;
    private int flag=0;

     private String str1 ="请输入半径: ";
     private String str2="";
     private String str3="请选择计算的种类";
    public Test(){
        
            JFrame f = new JFrame("圆的相关计算");
            JLabel lb = new JLabel("显示:");
            lb.setBounds(90,100,30,15);
            JPanel p = new JPanel();
             tf = new TextArea(3,50);
            tf.setText(str3);
            p.add(lb);
            p.add(tf,BorderLayout.SOUTH);
            

            
        
            GridLayout  gl = new GridLayout(5,3,10,10);
            JPanel p2 = new JPanel();
            p2.setLayout(gl);
            for (int i= 0; i<str.length;i++){
                JButton b = new JButton(str[i]);
                p2.add(b);
                b.addActionListener(this);
                b.setFont(new Font("宋体", Font.BOLD, 25));
                b.setBackground(Color.YELLOW);
            }
            
            f.add(p,BorderLayout.NORTH);
            f.add(p2,BorderLayout.SOUTH  );
            f.setSize(400, 400);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    
    public static void main(String[] args){
        new Test();
    }


    public void actionPerformed(ActionEvent e) {
        
        JButton button = (JButton)e.getSource();
        if(button.getText().equals("圆"))
        {
            flag = 1;
            tf.setText(str1);
            return ;
            
        }
        
    
    if(button.getText().equals("球"))
    {
        flag = 2;
        tf.setText(str1);
        return ;
    }
    
    if(button.getText().equals("计算"))
    {
        Double bo = new Double(str2);
        switch(flag){
            case 1:
                tf.setText("圆的面积是:"+bo*bo*Math.PI+"   "+ "周长是:"+(bo+bo)*Math.PI+"");
                break;
            case 2:
                tf.setText("球的体积是:"+bo*bo*bo*(4/3)*Math.PI+"    "+ "表面积是:"+4*bo*bo*Math.PI+"");
                break;
            
            }
        str2 ="";
        return ;
    }
    
    if(button.getText().equals("DEL"))
    {
    tf.setText(str3);
    
    return ;
    
    }
    
    
    else{
            str2 =str2+ button.getText();
            tf.setText(str2);
    }
            
        }
        
        
    }

 

Java编写圆的相关计算

原文:http://www.cnblogs.com/hprluck/p/Java.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!