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

2022-09-30 17:39:39 浏览数 (2)

大家好,又见面了,我是你们的朋友全栈君。

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);

}

}

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

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

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

0 人点赞