Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有人能识别出什么';我的代码怎么了?_Java_Swing - Fatal编程技术网

Java 有人能识别出什么';我的代码怎么了?

Java 有人能识别出什么';我的代码怎么了?,java,swing,Java,Swing,我试图创建一个GUI,当用户输入半径时,它可以计算圆的体积。到目前为止,我得到的是: import javax.swing.JFrame; public class Volume { //Creates a JFrame that calculates the Volume of a circle given the radius public static void main(String args[]) { JFrame frame = new JFrame

我试图创建一个GUI,当用户输入半径时,它可以计算圆的体积。到目前为止,我得到的是:

import javax.swing.JFrame;

public class Volume
{
    //Creates a JFrame that calculates the Volume of a circle given the radius
    public static void main(String args[])
    {
    JFrame frame = new JFrame("Volume Calc");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    VolumePanel panel = new VolumePanel();

    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
    }
}
对于我的Jframe和:

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

public class VolumePanel extends JPanel
{
private JLabel radiusLabel, volumeLabel, resultLabel;
private JTextField inputField;

//Sets up the Panel
public VolumePanel()
{
    JLabel radiusLabel = new JLabel("Please enter the radius: ");
    JLabel volumeLabel = new JLabel("The volume of the circle is: ");
    JLabel resultLabel = new JLabel("");

    JTextField inputField = new JTextField(5);
    inputField.addActionListener (new VolumeCalc());

    add (radiusLabel);
    add (inputField);
    add (volumeLabel);
    add (resultLabel);

    setPreferredSize(new Dimension(250,75));
    setBackground(Color.WHITE);
}

//Calculates the volume based on the input
private class VolumeCalc implements ActionListener
{
    public void actionPerformed(ActionEvent stuff)
    {
        double radius, volume;
        String input = inputField.getText();

        radius = Double.parseDouble(input);
        volume = (Math.pow(radius, 2) * Math.PI);

        resultLabel.setText (Double.toString(volume));
    }
}
}

为我的小组。它可以很好地编译和运行,但当我按enter键时,它会出错。

在您的操作中,请尝试:

String input = inputField.getText(); // guessing your input is the size of the circle
int result = Math.PI * Integer.valueOf(input);
resultLabel.setText(result);
因为圆是平的,所以它没有体积


编辑:在代码布局中,当尝试在volumePanel中使用inputField和resultLabel时,应该会出现错误。您有两个
inputField
声明,它们声明了单独的变量。这个:

private JTextField inputField;
JTextField inputField = new JTextField(5);
是实例的成员字段,但它从未初始化,因此它总是
null
。这个:

private JTextField inputField;
JTextField inputField = new JTextField(5);
是构造函数中的局部变量。它仅在构造函数内部可见。构造函数完成后,它就会消失

如果希望构造函数修改字段,请将第二行更改为

inputField = new JTextField(5);

这使它成为一个赋值语句,而不是一个新变量的声明。

“它搞砸了”实际发生了什么?你能澄清一下吗?我们需要更多关于到底发生了什么和你预期的情况的信息。所以我预期会发生的是,体积会被计算并显示在resultLabel中,但是发生的是命令提示符再次打开并显示了很多错误,而不是像这样说“它显示了很多错误”,只要告诉我们错误是什么。它们通常对问题是什么非常有用。顺便说一句,“体积”是指三维物体,如球体。你在这里计算的是“面积”。这是完全错误的。你只是更改了“面积”的正确公式“改成了一个错误的公式,你把他的代码改成了一些不可编译的代码,因为你不能乘
字符串
,也不能在
int
上使用
setText