对于像我这样的新手来说,这将非常困难,但我会尝试一下。我已经创建了创建该类的方法:
public class MainFrame extends JFrame {
private JTabbedPane tabPane;
private CustomerPanel customerPanel;
private VehiclePanel vehiclePanel;
private OrderPanel orderPanel;
private Controller controller;
public void setController(Controller controller) {
this.controller = controller;
}
public MainFrame() {
setLayout(new BorderLayout());
tabPane = new JTabbedPane();
customerPanel = new CustomerPanel();
vehiclePanel = new VehiclePanel();
orderPanel = new OrderPanel();
tabPane.add("Kunder", customerPanel);
tabPane.add("Fordon", vehiclePanel);
tabPane.add("Order", orderPanel);
add(tabPane, BorderLayout.CENTER);
}
}
如您所见,它包含几个面板。在这些面板中,更确切地说,在CustomerPanel中,您将找到一个名为CustomerFormPanel的类。此类作为一种形式,用户可以在其中输入名称,人员ID等信息。它还连接到控制器,该控制器将GUI和其余类合并在一起。这里的重要代码是:
[...]
public void setController(Controller controller) {
this.controller = controller;
}
public void CustomerFormPanel() {
[...]
regBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cName = nameField.getText();
String pNumber = pNrField.getText();
controller.addCustomer(pNumber, cName);
responseLabel.setText("Status: A Customer has been created!");
}
});
}
现在到班上。这是我将所有内容汇集在一起的课程,它看起来像这样:
public class CustomerApplication {
MainFrame myFrame;
public CustomerApplication() {
myFrame = new MainFrame();
myFrame.setMinimumSize(new Dimension(700, 640));
myFrame.setLocation(150, 150);
myFrame.setSize(1000, 900);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
public static void main(String[] args) {
CustomerApplication customerApplication = new CustomerApplication();
customerApplication.link();
}
private void link() {
CustomerRegister customerRegistry = new CustomerRegister();
Controller controller = new Controller(customerRegistry, myFrame);
myFrame.setController(controller);
}
}
最后,这是我的控制器类:
public class Controller {
CustomerRegister customers;
MainFrame mainFrame;
public Controller(CustomerRegister customerRegister, MainFrame mainFrame) {
this.customers = customerRegister;
this.mainFrame = mainFrame;
}
问题是我在CustomerFormPanel中输入一些信息并单击“添加”进行注册时,我遇到了问题。什么都没发生。我认为问题是控制器连接到大型机而不是CustomerFormPanel。但是我不知道如何解析来自CustomerFormPanel,CustomerPanel,MainFrame以及CustomerApplication的信息。
抱歉,这里没有任何意义,但是很难从新手的角度进行解释。而且我确实知道该问题是否已删除或标题是否已更改。
编辑:
这是addCustomer方法:
public void addCustomer(String pNumber, String cName) {
Customer tmpCustomer = new Customer(pNumber, cName);
customers.addCustomer(tmpCustomer);
}
最佳答案
首先...CustomerFormPanel
需要某种方式公开其包含的信息。一种解决方案是提供setter和getter,以允许CustomerPanel
更改/设置并从此组件获取值。
这样可以确保您不会不必要地暴露组件的某些部分(例如,例如字段),而仅提供对所需数据的访问。
然后,您需要对CustomerPanel
做相同的事情,这实际上将充当CustomerFormPanel
的代理。因此,您无需直接公开CustomerFormPanel
的实例,只需通过CustomerPanel
提供代理getter / setter
另外,您可以使用Visitor Pattern
基本上,这意味着,您将创建一个对象,该对象具有构建所需结果所需的必需的setter和getter,并将其传递给CustomerPanel
,然后CustomerPanel
将设置该对象的值(如果必须),并且然后将其他形式传递给它,它们将执行相同的操作,每种形式都将值设置为其管理的对象。
完成后,您将能够从该对象收集所有信息以生成所需的结果,该结果实际上描述了build pattern