两个JPanel之间的通信 [英] Communication between two JPanels

查看:115
本文介绍了两个JPanel之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个主面板(让我们称之为 AAA )和BorderLayout,以及两个面板( BBB CCC ) :

I have this "main" panel (let's call it AAA) with BorderLayout, and two panels (BBB and CCC) in it:

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

面板 CCC 目前空格,有GridLayout。

Panel CCC is currently empty, with GridLayout.

我的面板 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);        
    }
}

当用户在txtNum中输入数字并按确定,面板 CCC 应填充适当数量的行以进行数据输入。每行应包含两个文本字段,两个下拉列表和一个复选框。如果用户输入一些大数字,那么所有项目都在JScrollPane中会很好。

When a user enters a number in txtNum and presses "OK", panel CCC should be populated with appropriate number of rows for data input. 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.

我的问题:我该怎么办?在 BBB 中实现动作监听器?我不知道用户输入的是什么号码。因此,我不知道 CCC 的GridLayout中的确切行数(我只知道它应该有5列)。我可以在 BBB 中从监听器修改其布局吗?如何从面板 BBB 中的监听器向面板 CCC 添加组件?

My question: How should I implement the action listener in 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). Can I modify its layout from the listener in BBB? And how can I add components to the panel CCC from the listener in the panel BBB?

当然,如果您有更好的解决方案(没有两个单独的面板),请告诉我:)

Of course, if you have better solution (without two separate panels), let me know :)

推荐答案

您可能会想到这个错误。或许最好不要考虑两个正在通信的 JPanel ,而是更简单地说两个正在通信的对象,它们将与任何其他两个对象进行通信 - - 通过影响状态的方法。通过让一个对象调用另一个对象的方法并将其信息发布到另一个对象,可以将该信息从一个对象推送到另一个对象,或者可以通过使用观察者设计模式将其从一个对象拉到另一个对象。与各种可用的听众之一。我自己,我喜欢使用PropertyChangeListener。因此,观察对象将接受一旦其状态发生变化就被通知的侦听器,然后这些观察者将调用观察者的公共方法来提取已更改的信息。

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. 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. 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例如,请查看此答案中的代码,或者甚至更好这个问题的答案

For example, please check out the code in this answer or perhaps even better the answer to this question.

这篇关于两个JPanel之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆