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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mybatis框架–学习笔记(下)

    mybatis框架–学习笔记(下)

    2021年9月26日
    40
  • Android 开发者,你真的会用textview(maxEms和maxLength)的属性吗?

    Android 开发者,你真的会用textview(maxEms和maxLength)的属性吗?这里我们不说那些复杂的属性,光说我们通常用的比较多的,android:maxlength官网API对其的解释为:第一句,也就是说,他是个inputfilter(输入过滤器)他的作用是通过specifiednumber(你指定的数字)来限制textlength(文本长度),这里特指的是文本长度,而无论我们输入什么内容,英文,符号,数字,汉字………………这些都属于文本范围,所以ma

    2022年6月14日
    127
  • 【雕爷学编程】Arduino动手做(59)—RS232转TTL串口模块

    37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞不掂的问题,希望能够抛砖引玉。【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)实验…

    2022年4月6日
    151
  • python中的文本操作

    python中的文本操作

    2022年3月13日
    93
  • 线程池参数调优_rtt线程池

    线程池参数调优_rtt线程池在TiKV中,线程池主要由gRPC、Scheduler、UnifyReadPool、Raftstore、StoreWriter、Apply、RocksDB以及其它一些占用CPU不多的定时任务与检测组件组成,这里主要介绍几个占用CPU比较多且会对用户读写请求的性能产生影响的线程池。gRPC线程池负责处理所有网络请求,它会把不同任务类型的请求转发给不同的线程池。Scheduler线程池配置项的值为0,Raftstore线程将日志写入到磁盘;如果该值不为0RocksDB。…

    2022年9月23日
    0
  • 机器学习——下采样(under-sampling)「建议收藏」

    下采样(under-sampling)什么是下采样?当原始数据的分类极不均衡时,如下图我们要想用这样的数据去建模显然是存在问题的。尤其是在我们更关心少数类的问题的时候数据分类不均衡会更加的突出,例如,信用卡诈骗、病例分析等。在这样的数据分布的情况下,运用机器学习算法的预测模型可能会无法做出准确的预测,最后的模型显然是趋向于预测多数集的,少数集可能会被当做噪点或被忽视,相比多数集,少数集被…

    2022年4月4日
    147

发表回复

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

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