在多个 JPanel 上使用 JPanel

Use JPanel on multiple JPanels

我正在开发我的第一个 Java UI,我正在使用 CardLayout 在多个面板之间切换。

我有几个按钮,例如退出按钮,在所有卡片中都保持相同。因此,我希望将该按钮及其操作处理程序复制到每个卡片面板,但我不知道该怎么做。它似乎只是在我添加它的最后一个面板上,这意味着我必须多次重复那个按钮的代码,这感觉不对。

有谁知道在多个 panels/cards 上重用 JPanelJButton 组件的方法吗?

这里我针对你的情况写了一个示例代码。我希望您会发现它作为起点很有用 ;) Combobox 更改了 pnlCard 中的功能面板。最底部的按钮对所有功能面板都是通用的。

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * Main class for starting the application.
 */
public class MainProgram {
    public static void main(String[] args) {
        new TopFrame();
    }
}

/**
 * The main, top-most frame in demo app including combobox panel, card panel, and a common action button.
 */
class TopFrame extends JFrame implements ItemListener {

    CardPanel pnlCard = new CardPanel();
    JButton btnWithCommonAction = new JButton("Common action");

    public TopFrame() {
        JPanel comboBoxPane = new JPanel();
        String comboBoxItems[] = {"A", "B"};
        JComboBox cb = new JComboBox(comboBoxItems);
        cb.setEditable(false);
        cb.addItemListener(this);
        comboBoxPane.add(cb);
        //
        // !
        getContentPane().add(comboBoxPane, BorderLayout.PAGE_START);
        getContentPane().add(pnlCard, BorderLayout.CENTER);
        getContentPane().add(btnWithCommonAction, BorderLayout.PAGE_END);
        //
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void itemStateChanged(ItemEvent evt) {
        CardLayout cl = (CardLayout) (pnlCard.getLayout());
        cl.show(pnlCard, (String) evt.getItem());
    }
}

class CardPanel extends JPanel {
    JPanel cardA = new PanelWithSomeFunc("Functionality A");
    JPanel cardB = new PanelWithSomeFunc("Functionality B");
    public CardPanel() {
        setLayout(new CardLayout());
        add(cardA, "A");
        add(cardB, "B");
    }
}

/**
 * Simulates a panel with some functionality.
 * For demo purposes it contains a label and a sample button only.
 */
class PanelWithSomeFunc extends JPanel {

    private JLabel lblTitle;
    private JButton btnAction;

    public PanelWithSomeFunc(String functionalityName) {
        lblTitle = new JLabel(functionalityName);
        btnAction = new JButton(functionalityName);
        organizeLayout();
        final String textToDisplay = functionalityName;
        btnAction.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JOptionPane.showMessageDialog(new JFrame(), textToDisplay);
                    }
                }
        );
    }

    private void organizeLayout() {
        setLayout(new BorderLayout());
        add(lblTitle, BorderLayout.CENTER);
        add(btnAction, BorderLayout.PAGE_END);
    }
}

class CommonAction extends AbstractAction {
    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(new JFrame(), "This action is available regardless of which panel is displayed");
    }
}