java编写球体体积和面积_球体有哪些物品

java编写球体体积和面积_球体有哪些物品importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassVolumeextendsJFrameimplementsActionListener,ItemListener{JPanelp1,p2,p3;JRadioButtonrb1,rb2,rb3;ButtonGroupbg;JButton…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Volume extends JFrame implements ActionListener,ItemListener{

JPanel p1,p2,p3;

JRadioButton rb1,rb2,rb3;

ButtonGroup bg;

JButton b1,b2;

JLabel l1,l2,l3,l4;

JTextField tf1,tf2,tf3,tf4;

public Volume(){

init();

this.add(p1,BorderLayout.NORTH);

this.add(p2,BorderLayout.CENTER);

this.add(p3,BorderLayout.SOUTH);

this.setTitle(“Volume Calculator”);

this.pack();

this.setLocationRelativeTo(null);//窗口居中

this.setVisible(true);

this.setResizable(false);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void init(){

rb1 = new JRadioButton(“正方体”,true);

rb2 = new JRadioButton(“球  体”);

rb3 = new JRadioButton(“圆柱体”);

rb1.addItemListener(this);

rb2.addItemListener(this);

rb3.addItemListener(this);

rb1.addActionListener(this);

rb2.addActionListener(this);

rb3.addActionListener(this);

bg = new ButtonGroup();

bg.add(rb1);

bg.add(rb2);

bg.add(rb3);

p1 = new JPanel();

p1.setBorder(BorderFactory.createTitledBorder(“选择对象:”));

p1.add(rb1);

p1.add(rb2);

p1.add(rb3);

l1 = new JLabel(“边长:”);

l2 = new JLabel(“半径:”);

l3 = new JLabel(“高:”);

tf1 = new JTextField(4);

tf2 = new JTextField(4);

tf3 = new JTextField(4);

tf1.setEditable(true);

tf2.setEditable(false);

tf3.setEditable(false);

p2 = new JPanel();

p2.setBorder(BorderFactory.createTitledBorder(“输入参数:”));

p2.add(l1);

p2.add(tf1);

p2.add(l2);

p2.add(tf2);

p2.add(l3);

p2.add(tf3);

l4 = new JLabel(“体积:”);

tf4 = new JTextField(12);

tf4.setEditable(false);

b1 = new JButton(“GO”);

b1.addActionListener(this);

b2 = new JButton(“Help”);

b2.addActionListener(this);

p3 = new JPanel();

p3.setBorder(BorderFactory.createTitledBorder(“计算结果:”));

p3.add(b1);

p3.add(l4);

p3.add(tf4);

p3.add(b2);

}

public static void main(String[] args){

JFrame.setDefaultLookAndFeelDecorated(true);

JDialog.setDefaultLookAndFeelDecorated(true);

new Volume();

}

public void clear(){

tf1.setText(“”);

tf2.setText(“”);

tf3.setText(“”);

tf4.setText(“”);

}

public String getText(JTextField tf){

return tf.getText().trim();

}

public void shouDialog(){

JOptionPane.showMessageDialog(this, “参数只能为正数,请不要输入非法字符!”, “警告”, JOptionPane.WARNING_MESSAGE);

clear();

}

public  void itemStateChanged(ItemEvent ie){

if(ie.getItem()==rb1){

if(ie.getStateChange()==ItemEvent.SELECTED){

clear();

tf1.setEditable(true);

tf2.setEditable(false);

tf3.setEditable(false);

}

}else if(ie.getItem()==rb2){

if(ie.getStateChange()==ItemEvent.SELECTED){

clear();

tf2.setEditable(true);

tf1.setEditable(false);

tf3.setEditable(false);

}

}else if(ie.getItem()==rb3){

if(ie.getStateChange()==ItemEvent.SELECTED){

clear();

tf2.setEditable(true);

tf3.setEditable(true);

tf1.setEditable(false);

}

}

}

public void actionPerformed(ActionEvent ae){

if(ae.getSource()==b2){//”Help”

new Dialog(this);

}else if(ae.getSource()==b1){//”GO”

double volume = 0;

try{

if(rb1.isSelected()){//正方体

if(!getText(tf1).equals(“”)){

double size = Double.parseDouble(getText(tf1));

if(size>0){

volume = size*size*size;

tf4.setText((double)((int)Math.round(volume*1000)/1000.000)+””);

}else{

shouDialog();

}

}

}else if(rb2.isSelected()){//球体

if(!getText(tf2).equals(“”)){

double radius = Double.parseDouble(getText(tf2));

if(radius>0){

volume = 4*Math.PI* radius*radius*radius/3;

tf4.setText((double)((int)Math.round(volume*1000)/1000.000)+””);

}else{

shouDialog();

}

}

}else if(rb3.isSelected()){//圆柱体

if(!getText(tf2).equals(“”)&&!getText(tf3).equals(“”)){

double radius = Double.parseDouble(getText(tf2));

double height = Double.parseDouble(getText(tf3));

if(radius>0&&height>0){

volume = Math.PI*radius*radius*height;

tf4.setText((double)((int)Math.round(volume*1000)/1000.000)+””);

}else{

shouDialog();

}

}

}

}catch(Exception e){

shouDialog();

}

}

}

}

class Dialog extends JDialog{

JLabel[] l;

JButton b;

JPanel p1,p2,p3;

public Dialog(Frame f){

super(f,true);

p1 = new JPanel(new FlowLayout());

p1.setBorder(BorderFactory.createTitledBorder(“参数描述:”));

p2 = new JPanel(new GridLayout(0,1));

p2.setBorder(BorderFactory.createTitledBorder(“体积公式:”));

p3 = new JPanel(new FlowLayout());

p3.setBorder(BorderFactory.createEtchedBorder());

String[] s = {“a: 边长, r: 半径, h: 高, π: 圆周率(3.14159265..)”,

”    正方体: a*a*a”,

”    球  体: (4/3)*π*r*r*r”,

”    圆柱体: π*r*r*h”,

“Made by Kingpo in Wuhan QQ:281623469”};

l = new JLabel[s.length];

for(int i=1;i

l[i] = new JLabel(s[i]);

p2.add(l[i]);

}

l[0] = new JLabel(s[0]);

p1.add(l[0]);

l[4] = new JLabel(s[4]);

l[4].setFont(new Font(“”,Font.ITALIC,12));

l[4].setForeground(Color.GRAY);

b = new JButton(“OK”);

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

setVisible(false);

}

});

p3.add(l[4]);

p3.add(b);

this.add(p1,BorderLayout.NORTH);

this.add(p2,BorderLayout.CENTER);

this.add(p3,BorderLayout.SOUTH);

this.setTitle(“帮助…”);

this.setSize(f.getSize());

this.setResizable(false);

this.setLocationRelativeTo(f);

this.setVisible(true);

}

}

2b7ed385e552d8e5d5bfdcd795e528a5.png

4a5191050a9fa40fed22450c0fc87155.png

12d8348c17f79a6063cbb56cd12b66f8.png

上面是实现了图形用户界面,功能全部实现了,还可以扩展。例如多加几个容器(圆锥体等)对象,多加几个公式(表面积等)。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/192683.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • TextField的高宽autosize[通俗易懂]

    TextField的高宽autosize[通俗易懂]vart_name:TextField=newTextField;trace(t_name.height);trace(t_name.width);这样打印出来的高度都是100PX,是系统默认的。如果不设置可能会让父容器宽高变大,遮挡住别的窗口的鼠标事件。可以简单地使用tf.autoSize=TextFieldAutoSize.LEFT;来解决这样宽高就会根据文本内容大小来…

    2022年5月15日
    39
  • mysql 练习题

    mysql 练习题一、上机内容使用SQL语句创建数据库studentsdb。、Createdatabasestudentdb;2.使用SQL语句选择studentsdb为当前使用数据库。Usestudentdb;3.使用SQL语句在studentsdb数据库创建数据表student_info、curriculum、grade,三个表的数据结构如表1-表3所示。createtablestude…

    2022年9月18日
    3
  • mysql左连接丢失null值的问题

    mysql左连接丢失null值的问题一、前言      如题所示,我们有的时候直接使用左连接查询,当右表不存在该数据的时候,是可以查出带有null的列。可是当在where条件中有右表相关的筛选条件时,我们惊奇的发现查询的结果不带null值了,换句话说就是查出来的结果比预期的少。      博主之前遇到过这个问题…

    2022年5月6日
    37
  • OCX控件数字签名图文教程[通俗易懂]

    OCX控件数字签名图文教程[通俗易懂]这段时间做了一个B/S下套打的控件(过几天整理一下放到博客上来),控件测试完成,但是因为没有数字证书,IE如果不设置信任区域和等级的话,会直接被阻止下载安装(我不期望客户能熟练地改IE设置),但是数字证书价格昂贵,暂不考虑,看到cnblogs上一篇文章后深受启发,照着搞了一个测试用的数字签名,但是这篇文章上的OS貌似是win2000,IE5,我有必要在win7下给大家做个演示:首先,下载

    2022年7月13日
    14
  • js数组转对象的方法_js json对象转换成数组

    js数组转对象的方法_js json对象转换成数组记录将数组转成对象方法letarray=[1,2,3,4,5];letobj={};obj=(Object.assign({},array)console.log(obj);//{1,2,3,4,5}

    2025年10月24日
    3
  • 【转载】C# sleep 和wait的区别

    【转载】C# sleep 和wait的区别

    2021年11月20日
    46

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号