我无法弄清楚如何修复此代码?

时间:2013-04-10 16:25:54

标签: java actionlistener

我知道我的编码在这里很乱,但我在编程方面很新......无论如何,我希望我的程序从用户读取半径和高度并计算圆柱体积。我不知道如何处理我的最后两行。

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

1 个答案:

答案 0 :(得分:1)

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

public double ZylinderVolumen (double r, double h) {
   return Math.PI*r*r*h;
}