简体   繁体   English

两个JPanel之间的通信

[英]Communication between two JPanels

I have this "main" panel (let's call it AAA ) with BorderLayout, and two panels ( BBB and CCC ) in it: 我有这个“主”面板(让我们称之为AAA )与BorderLayout,以及两个面板( BBBCCC ):

public class AAA extends JPanel {
    BBB pnlNorth = new BBB();
    CCC pnlCenter = new CCC();
    public AAA(){
        setLayout(new BorderLayout());
        add(pnlNorth,BorderLayout.NORTH);
        add(pnlCenter,BorderLayout.CENTER);        
    }
}

Panel CCC is currently empty, with GridLayout. Panel CCC目前为空,带有GridLayout。

My panel BBB looks like this: 我的面板BBB看起来像这样:

public class BBB extends JPanel {
    public BBB (){
        JLabel labNum = new JLabel("Number of items: ");
        JTextField txtNum = new JTextField();
        JButton cmdOK = new JButton("OK");
        txtNum.setColumns(5);
        cmdOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                /* ???????????? */
            }
        });
        add(labNum);
        add(txtNum);
        add(cmdOK);        
    }
}

When a user enters a number in txtNum and presses "OK", panel CCC should be populated with appropriate number of rows for data input. 当用户在txtNum中输入一个数字并按“OK”时,面板CCC应填充适当数量的行以进行数据输入。 Each row should contain two text fields, two drop-down lists and a checkbox. 每行应包含两个文本字段,两个下拉列表和一个复选框。 It would be nice if all the items would be in a JScrollPane, if the user enters some large number. 如果用户输入一些大数字,那么所有项目都在JScrollPane中会很好。

My question : How should I implement the action listener in BBB ? 我的问题 :我应该如何在BBB中实现动作监听器? I have no idea what number will be typed in by the user. 我不知道用户输入的是什么号码。 Therefore, I don't know the precise number of rows in CCC 's GridLayout (I just know it should have 5 columns). 因此,我不知道CCC的GridLayout中的确切行数(我只知道它应该有5列)。 Can I modify its layout from the listener in BBB ? 我可以从BBB中的侦听器修改其布局吗? And how can I add components to the panel CCC from the listener in the panel BBB ? 如何从面板BBB中的监听器向面板CCC添加组件?

Of course, if you have better solution (without two separate panels), let me know :) 当然,如果你有更好的解决方案(没有两个单独的面板),请告诉我:)

You may be thinking about this wrong. 你可能会想到这个错误。 It's perhaps better to think not of two JPanel's that are communicating, but rather more simply two objects that are communicating, and they will communicate just the same as any other two objects -- via methods that affect state. 或许最好不要考虑两个正在通信的JPanel ,而是更简单地说两个正在通信的对象 ,它们将与任何其他两个对象进行通信 - 通过影响状态的方法。 This information can be pushed from one object to the other by having the one object call the methods of the other and publish its information to it, or it can be pulled from one object to another by using the observer design pattern such as can be achieved with one of the various listeners that are available. 通过让一个对象调用另一个对象的方法并将其信息发布给另一个对象,可以将该信息从一个对象推送到另一个对象,或者可以通过使用观察者设计模式将其从一个对象拉到另一个对象。与各种可用的听众之一。 Myself, I like using a PropertyChangeListener for this. 我自己,我喜欢使用PropertyChangeListener。 So the observed object will accept listeners that are notified once its state has been changed, and these observers will then call public methods of the observed to extract the changed information. 因此,观察对象将接受一旦其状态被更改就被通知的侦听器,然后这些观察者将调用被观察者的公共方法来提取已更改的信息。

For example, please check out the code in this answer or perhaps even better the answer to this question . 例如,请查看此答案中的代码,或者更好地回答这个问题

I managed to do this. 我设法做到了这一点。 I simply passed the Center panel as a parameter in the North panel's constructor. 我只是将Center面板作为North面板构造函数中的参数传递。 It works perfectly. 它完美地运作。 Thank you all for the answers :) 谢谢大家的答案:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM