问题与#34; Circle" Java中的类和构造函数?

时间:2017-09-10 03:00:39

标签: java methods constructor textfield numberformatexception

好的,这就是我正在进行的任务:

"创建一个带有图形用户界面的Java项目,允许用户选择他们希望使用的形状:方形,矩形,圆形,三角形

他们输入形状的尺寸,并可以选择计算形状的区域。

您将使用基类" Shape"然后有4个不同的类继承自Shape以计算Area。您对Area的计算应使用适当的class()方法"

我对GUI的想法是让用户通过同一组中的单选按钮选择形状,然后让它们在相应的文本字段中输入尺寸(高度和宽度)。在他们完成这两项工作后,他们按下一个按钮,就会计算出形状的区域。

我的问题是我已经创建了基础Shape类和四个继承类,并且我已经连接了#34;他们尽可能地使用GUI进入主类,但是当我尝试计算Circle区域时,我得到异常,因为程序希望用户在高度和宽度字段中输入数字,当我想要圆圈时通过宽度字段计算的区域(因为公式只需要一个维度)。我设置了多个构造函数来处理这个问题,但我显然做错了什么。所有其他Shapes都计算得很好。

以下是按下按钮时的主要(GUI)代码:

private void buttonAreaActionPerformed(java.awt.event.ActionEvent evt) {                                           

        int height = Integer.parseInt(tfHeight.getText());
        int width = Integer.parseInt(tfWidth.getText());

        try {
            Square mySquare = new Square(height,width);
            Circle myCircle = new Circle(width);
            Triangle myTriangle = new Triangle(height,width);
            Rectangle myRectangle = new Rectangle(height,width);

            double result = 0;

            if (rbSquare.isSelected()) {
                result = mySquare.area();
            }//end of if block
            else if (rbCircle.isSelected()){
                result = myCircle.area();
            }
            else if (rbTriangle.isSelected()){
                result = myTriangle.area();
            }
            else if (rbRectangle.isSelected()){
                result = myRectangle.area();
            }//end of else block

            JOptionPane.showMessageDialog(this, "Area is " + result);
        }//end of try block
        catch (NumberFormatException ex) {
            //Display new window with text upon catching exception
            JOptionPane.showMessageDialog(this, "Please enter the proper dimensions");
        }//end of catch block
    }                                          

这是基础Shape类代码:

public class Shape extends mainWindow {
    //create properties for class
    int width;
    int height;

    //default constructor to override method
    public Shape(){
        //"this" uses properties above
        width = 0;
        height = 0;
    }//end of constructor

    //create the constructor (same as the class name) and pass it arguments
    public Shape(int height, int width){
        //"this" uses properties above
        this.height = height;
        this.width = width;
    }//end of constructor

    public Shape(int width) {
        this.width = width;
    }

这是Circle类代码:

public class Circle extends Shape {
    //constructor
    Circle(int width){
        this.width = width;

    }

    public double area(){
        return Math.PI*(width*width);
    }
}

因此,要重申当用户选择Circle选项然后输入宽度尺寸时,他们会收到错误。他们必须在高度和宽度框中输入尺寸,这不是我想要的(它实际上没有取高度值并用它来计算,但它要求填充高度文本字段)

任何人都可以帮我解决这个问题或者告诉我哪里出错了吗?

这是异常堆栈跟踪:

线程中的异常" AWT-EventQueue-0" java.lang.NumberFormatException:对于输入字符串:""     at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)     在java.lang.Integer.parseInt(Integer.java:592)     在java.lang.Integer.parseInt(Integer.java:615)     在bism3800.mainWindow.buttonAreaActionPerformed(mainWindow.java:150)     在bism3800.mainWindow.access $ 000(mainWindow.java:14)     在bism3800.mainWindow $ 1.actionPerformed(mainWindow.java:71)     在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)     在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2348)     在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)     在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)     在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)     at java.awt.Component.processMouseEvent(Component.java:6533)     在javax.swing.JComponent.processMouseEvent(JComponent.java:3324)     at java.awt.Component.processEvent(Component.java:6298)     at java.awt.Container.processEvent(Container.java:2236)     at java.awt.Component.dispatchEventImpl(Component.java:4889)     at java.awt.Container.dispatchEventImpl(Container.java:2294)     at java.awt.Component.dispatchEvent(Component.java:4711)     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)     at java.awt.Container.dispatchEventImpl(Container.java:2280)     at java.awt.Window.dispatchEventImpl(Window.java:2746)     at java.awt.Component.dispatchEvent(Component.java:4711)     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)     at java.awt.EventQueue.access $ 500(EventQueue.java:97)     at java.awt.EventQueue $ 3.run(EventQueue.java:709)     at java.awt.EventQueue $ 3.run(EventQueue.java:703)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)     at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)     at java.awt.EventQueue $ 4.run(EventQueue.java:731)     at java.awt.EventQueue $ 4.run(EventQueue.java:729)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)     at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)     在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:3)

您的代码只执行您要告诉它的操作。在侦听器的开头,您从两个文本字段中提取文本,然后解析它们,无论选择哪个单选按钮,都会发生

private void buttonAreaActionPerformed(java.awt.event.ActionEvent evt) {

    // this code is **always** run, no matter what
    int height = Integer.parseInt(tfHeight.getText());
    int width = Integer.parseInt(tfWidth.getText());

显而易见的解决方案是不要这样做。仅在需要时解析所需内容,并在if语句中解析。

您的关键问题是您没有进行最重要的调试步骤:精心浏览您的代码,了解它在每一步所做的事情。现在,经常开始这样做。

根据How to Think Like a Computer Scientist文章:

  

为了编程,你需要有一个关于程序如何工作的心智模型。如果你编写的程序没有达到预期的效果,那么问题往往不在程序中;它在你的心理模型中。