Java 从多个类访问方法

Java 从多个类访问方法,java,class,object,methods,invoke,Java,Class,Object,Methods,Invoke,我正在编写一个程序,根据房间尺寸和所选地板类型计算地板成本。我有5个类(Cost、CustomerInfo、OrderSummary、MainForm、和MainApp),程序本身有3个选项卡(成本、客户信息、订单摘要) 每个选项卡在MainForm类中被实例化为其各自的对象。我遇到的问题是我的orderssummary类需要从Cost类调用getFloorArea()和getTotalCost()方法,还需要从CustomerInfo类调用getCustName()和getCustAddres

我正在编写一个程序,根据房间尺寸和所选地板类型计算地板成本。我有5个类(
Cost
CustomerInfo
OrderSummary
MainForm
、和
MainApp
),程序本身有3个选项卡(成本、客户信息、订单摘要)

每个选项卡在
MainForm
类中被实例化为其各自的对象。我遇到的问题是我的
orderssummary
类需要从
Cost
类调用
getFloorArea()
getTotalCost()
方法,还需要从
CustomerInfo
类调用
getCustName()
getCustAddress()
方法。每个选项卡本身都可以正常工作(计算面积、建议房间的成本/获取客户的姓名和地址),但我不知道如何将这些信息拉到
orderssummary
类中。订单摘要选项卡仅将所有信息显示为空

我确信这是因为我需要在
OrderSummary
类中实例化
Cost
CustomerInfo
类,但我不知道怎么做。我觉得问题在于,当我创建选项卡时,会创建3个不同的对象,但我不知道如何从
OrderSummary
类访问每个选项卡中输入的信息。我真的很感激你的帮助。我正在绞尽脑汁想办法做什么

我可以根据需要提供代码,但这是一个相当长的程序

编辑:以下是一些我认为会有所帮助的信息:

这是在我的主窗体中,我在其中创建选项卡:

    jtp.addTab("Cost", new Cost());
    jtp.addTab("Customer Info", new CustomerInfo());
    jtp.addTab("Order Summary", new OrderSummary());
这是类Cost中的getFloorArea()方法

    public double getFloorArea() {

        FloorLength = Double.parseDouble(enterLength.getText());
        FloorWidth = Double.parseDouble(enterWidth.getText());

        FloorArea = FloorLength * FloorWidth;

        return FloorArea;
    }
这是我的类OrderSummary,在其中我不知道如何调用这些函数来显示信息:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import helpers.*;

@SuppressWarnings("serial")
public class OrderSummary extends JPanel {

    JTextArea orderSummary;

    public OrderSummary() {
        createPanel();
    }

    private void createPanel() {
        super.setLayout(new GridBagLayout());
        GridBagConstraints bag = new GridBagConstraints();

        bag.fill = GridBagConstraints.BOTH;
        bag.anchor = GridBagConstraints.FIRST_LINE_START;
        bag.insets = new Insets(5,5,5,5);

        bag.gridx = 0;
        bag.gridy = 0;
        orderSummary = new JTextArea(5, 20);
        orderSummary.setFont(new Font("Arial", Font.BOLD, 12));
        orderSummary.setBackground(Color.WHITE);
        this.add(orderSummary, bag);

//This is my trouble area, I can't figure out how to access the classes to display the information in this JTextArea        
orderSummary.setText("Customer Name: " +   CustomerInfo.getFirstName() + " " + CustomerInfo.getLastName() +
            "\nAddress: " + CustomerInfo.getStreet() + "\n" + CustomerInfo.getCity() + "\n" + CustomerInfo.getCustState() + "\n" + CustomerInfo.getZip() +
            "\n\nTotal Area: " + Cost.getFloorArea() + " square feet" + 
            "\nCost: " + OutputHelpers.formattedCurrency(Cost.getTotalCost()));
    }

}

我已经尝试在Cost类中使变量和方法保持静态,但是计算和成本在该选项卡本身中不起作用。例如,“我的清除”按钮将清除除静态变量以外的所有变量。

MainForm
类中创建
Cost
CustomerInformation
对象时,您可以将它们传递给
ordersumsummary
。尽管您可能想重新构建您的项目

public class MainForm {

    public void myMethod() {
        Cost cost = new Cost();
        CustomerInfo custInfo = new CustomerInfo();
        OrderSummary orderSummary = new OrderSummary(cost, custInfo); 

        jtp.addTab("Cost", cost);
        jtp.addTab("Customer Info", custInfo);
        jtp.addTab("Order Summary", orderSummary);

        ...
    }
}
还有像

public class OrderSummary {

    private Cost cost;
    private CustomerInformation custInfo;

    public OrderSummary(Cost cost, CustomerInformation custInfo) {
        this.cost = cost;
        this.custInfo = custInfo;
    }

    ...
}

如果没有一些代码,就不可能准确回答。尽管这似乎是一个经典的问题,可以通过MVC等经典模式来解决,但只需发布代码的相关部分就可以帮助我们了?1) 当你说“3个标签”时,你指的是用户界面代码(例如JFrame/JTabbedPane),对吗?2) 你能澄清“MainForm”类做什么吗?1)是的,有3个jtabbedpane。2) MainForm类为整个GUI创建框架。这是一个解决方法-问题是没有
Order
类,所有的东西都是子类。每个窗口都有自己的数据。要将其缝合在一起,请让windows了解对象的层次结构,其顺序看起来像是一个好的父对象。Gary关于OrderSummary的建议同样有效。