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)
上一篇 2025年12月10日 上午11:22
下一篇 2025年12月10日 下午12:01


相关推荐

  • 分布式锁—-数据库和redis实现分布式锁

    分布式锁—-数据库和redis实现分布式锁

    2021年8月3日
    76
  • 鸿蒙系统手机电脑互传文件,华为手机怎么与电脑互传文件(Huawei Share教程

    鸿蒙系统手机电脑互传文件,华为手机怎么与电脑互传文件(Huawei Share教程未经允许 如果我想把手机的相册传到电脑上 9 接着按照电脑的提示 就必须通过 HuaweiShare 进行互传 如果对您有所帮助 END 注意事项本经验由作者 梦想快男 原创 5 在此电脑中的搜索框内输入网络 6 点击带有 HONOR 类似的前缀的图标 或是说电脑需要接收华为手机传送的文件 2 打开手机状态栏 通过手机可以知道用户名和密码 即可通过华为手机和电脑互传文件了 然后点击复制 需

    2025年11月5日
    6
  • ubuntu18.04安装pycharm专业版

    ubuntu18.04安装pycharm专业版安装的 pycharm 版本 2017 4 操作系统 ubuntu18 04LTS 步骤 1 在官网下载安装包 https www jetbrains com pycharm download section linux 下载文件位于 Downloads 下选择对应版本步骤 2 解压到文件夹 opt 该文件夹内安装的程序都是 allinone 便于删除程

    2026年3月17日
    2
  • 史上最全ThreadLocal 详解(一)

    史上最全ThreadLocal 详解(一)史上最全 ThreadLocal 详解目录史上最全 ThreadLocal 详解一 ThreadLocal 简介二 ThreadLocal 的作用三 ThreadLocal 与 Synchronized 的区别四 ThreadLocal 的简单使用五 ThreadLocal 的原理 5 1 ThreadLocal 与 Thread ThreadLocalM 之间的关系六 ThreadLocal 常见使用场景一 ThreadLocal 简介 ThreadLocal 叫做线程变量

    2026年3月19日
    2
  • atop用法_atop 使用详情

    atop用法_atop 使用详情atop 是一个功能非常强大的 linux 服务器监控工具 它的数据采集主要包括 CPU 内存 磁盘 网络 进程等 并且内容非常的详细 特别是当那一部分存在压力它会以特殊的颜色进行展示 如果颜色是红色那么说明已经非常严重了 ATOP 列 该列显示了主机名 信息采样日期和时间点 PRC 列 该列显示进程整体运行情况 sys usr 字段分别指示进程在内核态和用户态的运行时间 proc 字段指示进程总数 zombie 字

    2026年3月19日
    3
  • SQL 语句练习

    实验名称SQL语句练习实验地点实验楼502实验日期3.21 一、实验目的及要求 1.加深对表间关系的理解 2.理解数据库中数据的查询方法和应用 3.掌握各种查询的异同及相互之间的转换方法 4.掌握各种查询要求的实现 二、实验环境 Sql…

    2022年4月8日
    50

发表回复

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

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