当它们位于不同的类中时,如何从另一个JPanel事件侦听器更改JPanel的组件?

问题描述 投票:0回答:1

我有一个很大的UI,所以我的JPanels都被创建为扩展JPanel的类,而不是使用JFrame在类中设计其组件。因此,在下图中,右侧的JList部分是一个面板,而TabbedPane内部的左侧内容区域是另一面板。

enter image description here

我希望能够从右侧面板中选择更改监听器,更改左侧面板中存在的内容。我对如何执行此操作完全不知所措,因为侦听器无法访问其他面板。我可以在网上找到的所有示例中,一个面板更改为另一个面板,所有示例都在一个类或方法中创建和配置了面板。

这是用于在类中设置面板布局的类中的面板创建的代码片段。

// ===tabbedPane===
    tabbedPane = new JTabbedPane();
    tabbedPane.addChangeListener(e -> selectedTabChanged());
    OutfitsPanel outfitsPanel = new OutfitsPanel();
    tabbedPane.addTab("Outfits", outfitsPanel);
    ItemsPanel itemsPanel = new ItemsPanel();
    tabbedPane.addTab("Items", itemsPanel);
    OptionsPanel optionsPanel = new OptionsPanel();
    tabbedPane.addTab("Options", optionsPanel);

    contentPanel.add(tabbedPane, "cell 0 0");
    contentPane.add(contentPanel, BorderLayout.CENTER);
    listPanel = new NavListPanel(myInv.outfits, myInv.items);
    contentPane.add(listPanel, BorderLayout.EAST);

JList面板中的代码段

itemList.addListSelectionListener(e -> selectionChanged(e));

    private void selectionChanged(ListSelectionEvent e) {
        // what do I do here to change values of components in ItemsPanel object???
}
java swing jpanel listener
1个回答
0
投票

[好吧,我不确定这是否正确,但是我通过公开面板中的组件并在生成面板的主类中创建事件处理程序来使其正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.