【问题标题】:Use JPanel on multiple JPanels在多个 JPanel 上使用 JPanel
【发布时间】:2015-08-09 13:45:20
【问题描述】:

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

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

有谁知道在多个面板/卡片上重用JPanelJButton 组件的方法?

【问题讨论】:

  • " 因此,我希望将该按钮及其操作处理程序复制到每个卡片面板," 更好的策略是将这些常用组件 移出 /i>(在CardLayout 的上方、下方、左侧或右侧),一种方法是将具有卡片布局的面板放入另一个具有BorderLayout 的面板的CENTER 中,然后将公共面板中的组件并将其添加到边框布局的PAGE_STARTPAGE_END
  • 请看最后一个尝试add ActionListener to JButton 的示例。这里使用Action,而不是ActionListener,这基本上有助于代码的可重用性。

标签: java swing jpanel layout-manager cardlayout


【解决方案1】:

在这里,我为您的情况编写了一个示例代码。我希望您会发现它作为起点很有用;) 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");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多