在cardlayout中切换卡后运行方法 [英] Running a method after switching cards in cardlayout

查看:225
本文介绍了在cardlayout中切换卡后运行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定之前有人问过这个问题,但今天我的google-fu并不强大。

I'm sure someone has asked this question before, but my google-fu is not strong today.

我有一个使用CardLayout作为其经理的JFrame 。当我在不使用开关的情况下切换到每个JPanel时,如何运行开始方法?

I have a JFrame that uses a CardLayout as its manager. How do I run a "Start" method when I switch to each JPanel without using a switch?

我用于将帧添加到布局的代码是:

The code I use to add the frames to the layout is:

/**
 * 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());
 }

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

The code I use to switch the layout is:

/**
 * 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设置,可以调用切换代码,但我可以不知道如何调用它将切换到的屏幕中的开始方法。

Currently I have an ActionListener set that will call the switch code, but I can't work out how to call the "Start" method within the screen that it will be switching to.

我为每个JPanel设置了一个接口,以便方法名称各相同。

I have an interface setup for each of the JPanels so that the method name will be identical in each.

推荐答案

你可以使用 ComponentListener 。当面板成为CardLayout的视图时,它将触发一个组件事件并由侦听器中的 componentShown 处理(以及从视图中取出的面板,处理的componentHidden )。在那里调用 start()方法。这样,当面板改变时,您不必显式调用 start(),因为它会为您调用。

You can just use a ComponentListener for the panel(s). When the panel becomes the view of the CardLayout, it will fire a component event and handled by componentShown in your listener (as well as the panel taken out of view, handling the componentHidden). Call your start() method there. This way you don't have to explicitly call the start() when the panel changes, as it be called for you.

有关更多信息,请参阅如何编写组件监听器详情。

See How to Write Component Listeners for more details.

这是一个简单的例子。

Here is a simple example.

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

注意你实际上没有说过启动方法的位置,所以这段代码/答案只是假设您在自定义面板中有一些启动方法。希望我猜对了。在将来,甚至现在,您应该始终发布 MCVE ,以便我们不必进行所有这些猜测。

Note you haven't actually said where the start method is, so this code/answer is just assuming you have some start method in custom panel. Hope I guessed right. In the future, or even now, you should always post an MCVE so that we don't have to do all this guessing.

这篇关于在cardlayout中切换卡后运行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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