0

我知道我在这里的编码是一团糟,但我在编程方面相当陌生......无论如何,我希望我的程序从用户那里读取半径和高度并计算圆柱体的体积。我不知道如何处理我的最后两行。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Volume extends JFrame implements ActionListener{

    private double h, r, v;
    JFrame frame = new JFrame();
    JLabel labelRadius = new JLabel("Radius: ");
    JLabel labelHight = new JLabel("Hight: ");
    JLabel labelVolume = new JLabel("Volume: ");
    JLabel volume = new JLabel();
    JTextField textRadius = new JTextField(10);
    JTextField textHight = new JTextField(10);
    JPanel panel = new JPanel();

    public Volume(){
        setLayout(new GridLayout(2,2));
        add(panel);
        panel.add(labelHight);
        panel.add(textHight);
        textHight.addActionListener(this);
        panel.add(labelRadius);
        panel.add(textRadius);
        textRadius.addActionListener(this);
        panel.add(labelVolume);
        panel.add(volume);
        panel.setLayout(new GridLayout(3,2));
        panel.setSize(600, 600);
        pack();
        panel.setVisible(true);
        panel.setOpaque(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
        String hight = textHight.getText();
        h = Double.parseDouble(hight);

    }

    public void actionPermormed(ActionEvent e){
        String radius = textRadius.getText();
        r = Double.parseDouble(radius);
    }

    public static void main(String[] args) {
        Volume vlm = new Volume();
        vlm.setVisible(true);

    }

    v = Math.PI*r*r*h; // I DONT KNOW WHERE TO PUT -
    volym.setText(Double.toString(v)); // THESE TWO LINES (IF MY CODING IS RIGHT)
}
4

1 回答 1

1

您必须创建一个方法,例如:

public double ZylinderVolumen (double r, double h) {
   return Math.PI*r*r*h;
}
于 2013-04-10T16:37:34.593 回答