【发布时间】:2015-04-19 21:59:49
【问题描述】:
我正在开发一个程序,该程序根据房间的尺寸和所选地板的类型来计算地板的成本。我有 5 个课程(Cost、CustomerInfo、OrderSummary、MainForm 和 MainApp),程序本身有 3 个选项卡(成本、客户信息、订单摘要)。
每个选项卡都在MainForm 类中实例化为其各自的对象。我遇到的问题是我的OrderSummary 类需要从Cost 类调用getFloorArea() 和getTotalCost() 方法,还需要从CustomerInfo 调用getCustName() 和getCustAddress() 方法班级。每个选项卡本身都可以正常工作(计算面积、建议房间的成本/获取客户的姓名和地址),但我不知道如何将这些信息提取到 OrderSummary 类中。订单摘要选项卡仅将所有信息显示为空。
我确定这是因为我需要在 OrderSummary 类中实例化 Cost 和 CustomerInfo 类,但我不知道该怎么做。我觉得问题是创建选项卡时创建了 3 个不同的对象,但我不知道如何访问从 OrderSummary 类输入到每个选项卡中的信息。任何帮助都非常感谢,我正在努力弄清楚该怎么做。
我可以根据需要提供代码,但这是一个相当长的程序。
编辑:以下是我认为会有所帮助的一些内容:
这是在我创建标签的 MainForm 中:
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 类中将变量和方法设为静态,但计算和成本在该选项卡本身内不起作用。例如,我的清除按钮将清除除静态变量之外的所有变量。
【问题讨论】:
-
没有一些代码不可能准确回答。只需发布代码的相关部分以帮助我们提供帮助,尽管这似乎是一个可以通过 MVC 等经典模式解决的经典问题。
-
你能澄清两件事吗? 1)当您说“3 个选项卡”时,您指的是用户界面代码(例如 JFrame/JTabbedPane)对吗? 2)你能澄清一下'MainForm'类的作用吗?
-
1) 是的,有 3 个 JTabbedPanes。 2) MainForm 类为整个 GUI 创建框架。
标签: java class object methods invoke