簡體   English   中英

在卡片布局中切換卡片后運行方法

[英]Running a method after switching cards in 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() ,因為它會為您調用。

有關更多詳細信息,請參見如何編寫組件偵聽器

這是一個簡單的例子。

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,這樣我們就不必做所有這些猜測。

我為每個JPanel設置了接口,以便每個JPanel的方法名稱都相同

因此,問題出在交換面板時,使當前面板可見,以便您可以調用該方法。

請查看Card Layout Focus中的類,該類擴展了CardLayout以提供一些幫助方法來為CardLayout添加其他功能。 您將使用getCurrentCard()方法。

因此,您的changePane(...)方法可能類似於:

public final void changePanel(final WindowNames windowName) {
    //view.getCardLayout().show(view.getBasePanel(), windowName.getValue());
    RXCardLayout layout = view.getCardLayout();
    layout.show(view.getBasePanel(), windowName.getValue());
    MyInterface panel = (MyInterface)layout.getCurrentCard();
    panel.someMethod(...);
 }

當然,您還需要使用RXCardLayout作為主面板的布局管理器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM