如何从gui获取值并应用合适的构造函数或方法

时间:2013-03-04 16:45:06

标签: java swing oop inheritance dynamic-programming

我是新手程序员,学习使用面向对象的编程概念(继承和重载构造函数方法)

现在我创建了(大部分是复制的)代码,该代码将从用户获取5个输入并显示输出。现在使用方法重载它也可以采用3个输入并显示相同的输出。

现在如何在Swing GUI中实现它。 GUI就是这个

GUI Image

用户可以提供所有值,也可以只提供宽度,高度和深度的值。

主要代码是

`

class box
{
    private double width;
    private double height;
    private double depth;
    box(box ob)
    {
        width =ob.width;
        height=ob.height;
        depth =ob.depth;
    }
    box(double w,double h,double d)

    {
        width=w;
        height=h;
        depth=d;
    }
    box()
    {
        width=-1;
        height=-1;
        depth=-1;
    }
    box(double len)
    {
        width=height=depth=len;
    }
    double volume()
    {
        return width*depth*height;
    }

 }

class boxweight extends box
{
    private double weight;
    boxweight(boxweight ob)
    {
        super(ob);
        weight= ob.weight;
    }
    boxweight(double w,double h,double d,double m)
    {
        super(w,h,d);
        weight=m;
    }

    boxweight(double len,double m)
    {
        super(len);
        weight=m;

    }

    boxweight() 
    {
        super();
        weight=-1;
    }

    double weightcal()
    {
        return (volume()*weight);
    }


}

class shipment extends boxweight
{
    private double cost;

    shipment(shipment ob) 
    {
        super(ob);
        cost=ob.cost;

    }

    shipment(double w,double h,double d,double m,double c) 
    {
        super(w,h,d,m);
        cost=c;
    }
    shipment (double len,double m,double c)
    {
        super(len,m);
        cost=c;

    }

    shipment() 
    {
        super();
        cost=-1;
    }
    double costcal()
    {
        return(weightcal()*cost);
    }


}


public class Inheritanceallhere {

    public static void main(String[] args) {

        shipment o1=new shipment(10,20,30,40,50);
        shipment o3=new shipment(18.17122,40,50);
        double vol;
        vol = o1.volume();
        System.out.println("Volume of shipment1 is " + vol);
        System.out.println("Weight of shipment1 is "+ o1.weightcal());
        System.out.println("Shipping cost: $" + o1.costcal());
        System.out.println();
        vol =o3.volume();
        System.out.println("Volume of shipment2 is " + vol);
        System.out.println("Weight of shipment2 is "+ o3.weightcal());
        System.out.println("Shipping cost: $" + o3.costcal());

    }
}

`

现在当我按下计算按钮时我想要的是它将获取可用值并为其指定适当的方法

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       double a,b,c,d,e;
       firstsubclass o2=new firstsubclass();
       a=Double.parseDouble(jTextField1.getText());
       b=Double.parseDouble(jTextField2.getText());
       c=Double.parseDouble(jTextField4.getText());
       d=Double.parseDouble(jTextField5.getText());
       e=Double.parseDouble(jTextField6.getText());
       shipment o3=new shipment(a,b,c,d,e);




       jTextField3.setText(String.valueOf(o3.costcal()));

}

我做了这个代码来获取所有五个输入然后计算。但我不能对三个值做同样的事情(宽度高度或深度字段中的任何一个都可用)。我知道我可以使用一些控制语句来确保哪种方法可以工作,但我想知道java中是否有任何动态控件,以便我可以直接执行此操作而无需任何其他代码。

注意:两个代码都在同一个包中

2 个答案:

答案 0 :(得分:0)

我建议您查看Java Collections Framework,特别是ArrayList作为一般用途的好集合。

http://docs.oracle.com/javase/7/docs/technotes/guides/collections/index.html

如果你的程序有n个东西,但你不知道“编译时”有多少东西,你可以编写你的逻辑来期待一个事物列表,然后遍历列表。

答案 1 :(得分:0)

  

如何制作一个程序,可以了解有多少输入可用,并根据它进行计算?

您可以单独测试每个字段。

if (!jTextField1.getText().trim().equals("")) {
    a = Double.parseDouble(jTextField1.getText().trim());
}

然后测试Double值以查看已设置的值。