对于像我这样的新手来说,解释起来会非常困难,但我会尝试一下。我已经创建了这个类:
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 和其余类结合在一起的 Controller 。这里的重要代码是:
[...]
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!");
}
});
}
现在进入主课。这是我将所有内容整合在一起的类(class),如下所示:
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);
}
}
最后这是我的 Controller 类:
public class Controller {
CustomerRegister customers;
MainFrame mainFrame;
public Controller(CustomerRegister customerRegister, MainFrame mainFrame) {
this.customers = customerRegister;
this.mainFrame = mainFrame;
}
问题是,当我在 CustomerFormPanel 中输入一些信息并单击“添加”进行注册时;什么都没发生。我认为问题在于 Controller 连接到 MainFrame 而不是 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
或者,您可以使用Visitor Pattern
基本上,这意味着,您将创建一个对象,该对象具有构建所需结果所需的 setter 和 getter 方法,并将其传递到 CustomerPanel
中,CustomerPanel
然后将设置值(如果有)到此对象,然后将其传递给其他表单,这些表单将执行相同的操作,每个表单将值设置为其管理的此对象。
完成后,您将能够从该对象收集所有信息以生成您需要的结果,该结果实际上描述了 build pattern
关于java - 如何将代码从一个类传递到第二个类并传递到第三个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067337/