2015-04-19 54 views
0

我正在研究一个计算基于房间尺寸和所选地板类型的地板成本的程序。我有5个班级(Cost,CustomerInfo,OrderSummary,MainFormMainApp),并且该程序本身有3个选项卡(成本,客户信息,订单摘要)。从多个班级访问方法

每个选项卡都被实例化为MainForm类中的相应对象。我遇到的问题是我的OrderSummary类需要调用Cost类中的getFloorArea()getTotalCost()方法,还需要调用CustomerInfo类中的getCustName()getCustAddress()方法。每个标签本身都能正常工作(计算面积,建议房间的成本/获取客户的姓名和地址),但我无法弄清楚如何将这些信息放入OrderSummary类。 Order Summary选项卡仅显示所有信息为空。

我敢肯定,这是因为我需要在OrderSummary类中实例化CostCustomerInfo类,但我无法弄清楚如何去做。我觉得问题是创建我的选项卡时创建了3个不同的对象,但我不知道如何访问正在输入OrderSummary类的每个选项卡中的信息。任何帮助真的很感激,我拉着我的头发试图找出该怎么做。

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

编辑:这里有一些位,我认为这将有助于:

这是我的MainForm,在那里我创造我的标签:

jtp.addTab("Cost", new Cost()); 
    jtp.addTab("Customer Info", new CustomerInfo()); 
    jtp.addTab("Order Summary", new OrderSummary()); 

这是方法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类中使变量和方法静态,但是然后计算和成本在该选项卡本身内不起作用。例如,我的清除按钮将清除除静态按钮之外的所有变量。

+1

无法准确地回答没有一些代码。只要发布代码中**相关的**部分以帮助我们提供帮助,虽然它似乎是一个可以通过经典模式(如MVC)解决的经典问题。 – Dici

+0

你能澄清两件事吗? 1)当您说'3 Tabs'时,您正在讨论用户界面代码(例如JFrame/JTabbedPane)是否正确? 2)你能否澄清'MainForm'类的作用? – jrbeverly

+0

1)是的,有3个JTabbedPanes。 2)MainForm类为整个GUI创建框架。 – stevanc

回答

2

当您在MainForm类中创建对象时,可以将CostCustomerInformation对象传递给OrderSummary。虽然你可能想考虑重塑你的项目。

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

    ... 
} 
+1

这是一个解决方法 - 问题是没有'Order'类,其中的一切都是儿童。每个窗口都有自己的数据。要将它们缝合在一起,请让窗口知道对象的层次结构,其中Order看起来像是一个很好的父对象。加里对OrderSummary的建议也会起作用。 –