编程语言
首页 > 编程语言> > java-在cardlayout中切换卡片后运行方法

java-在cardlayout中切换卡片后运行方法

作者:互联网

我敢肯定有人问过这个问题,但是今天我的google-fu并不强大.

我有一个使用CardLayout作为其管理器的JFrame.在不使用开关的情况下切换到每个JPanel时如何运行“启动”方法?

我用来将框架添加到布局的代码是:

/**
 * Adds JPanels to the Card Layout.
 * @param panel is the JPanel to add to the layout.
 * @param windowName is the identifier used to recognise the Panel.
 */
 public final void addToCards(final JPanel panel, final WindowNames windowName) {
     view.getBasePanel().add(panel, windowName.getValue());
 }

我用来切换布局的代码是:

/**
 * Method to change the JPanel currently visible on the BasePanel.
 * @param windowName is the name of the JPanel to change to.
 */
 public final void changePanel(final WindowNames windowName) {
    view.getCardLayout().show(view.getBasePanel(), windowName.getValue());
 }

当前,我有一个ActionListener集合,它将调用切换代码,但是我无法弄清楚如何在将要切换到的屏幕中调用“ Start”方法.

我为每个JPanels设置了接口,以便每个方法名称都相同.

解决方法:

您只能将ComponentListener用于面板.当面板成为CardLayout的视图时,它将触发组件事件并由侦听器中的componentShown处理(以及从视图中取出的面板,处理componentHidden).在此调用您的start()方法.这样,当面板更改时,您不必显式调用start(),因为它会为您调用.

有关更多详细信息,请参见How to Write Component Listeners.

这是一个简单的例子.

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main {

    private static final String PANEL_A = "panelA";
    private static final String PANEL_B = "panelB";

    CardLayout layout = new CardLayout();
    JPanel panel = new JPanel(layout);
    ComponentListenerPanel p1 = new ComponentListenerPanel(PANEL_A);
    ComponentListenerPanel p2 = new ComponentListenerPanel(PANEL_B);
    JButton b1 = new JButton(PANEL_A);
    JButton b2 = new JButton(PANEL_B);

    public Main() {
        panel.add(p1, PANEL_A);
        panel.add(p2, PANEL_B);

        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                show(PANEL_A);
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                show(PANEL_B);
            }
        });
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(b1);
        buttonPanel.add(b2);

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.add(buttonPanel, BorderLayout.PAGE_END);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void show(String panelName) {
        layout.show(panel, panelName);
    }

    private class ComponentListenerPanel extends JPanel {
        private String panelName;

        public ComponentListenerPanel(String panelName) {
            this.panelName = panelName;
            addComponentListener(new ComponentAdapter() {
                @Override
                public void componentHidden(ComponentEvent evt) {
                    stop();
                }
                @Override
                public void componentShown(ComponentEvent evt) {
                    start();
                }
            });
        }

        public void start() {
            System.out.println(panelName + " started");
        }

        public void stop() {
            System.out.println(panelName + " stopped");
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Main();
            }
        });
    }
}

请注意,您实际上并未说过start方法在哪里,因此此代码/答案仅是假设您在自定义面板中有一些start方法.希望我猜对了.在将来,甚至现在,您都应该始终发布MCVE,这样我们就不必做所有这些猜测了.

标签:cardlayout,java,user-interface,swing
来源: https://codeday.me/bug/20191011/1892467.html