小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

java-在cardlayout中切換卡片后運行方法

 印度阿三17 2019-10-11

我敢肯定有人問過這個問題,但是今天我的google-fu并不強大.

我有一個使用CardLayout作為其管理器的JFrame.在不使用開關(guān)的情況下切換到每個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());
 }

當(dāng)前,我有一個ActionListener集合,它將調(diào)用切換代碼,但是我無法弄清楚如何在將要切換到的屏幕中調(diào)用“ Start”方法.

我為每個JPanels設(shè)置了接口,以便每個方法名稱都相同.

解決方法:

您只能將ComponentListener用于面板.當(dāng)面板成為CardLayout的視圖時,它將觸發(fā)組件事件并由偵聽器中的componentShown處理(以及從視圖中取出的面板,處理componentHidden).在此調(diào)用您的start()方法.這樣,當(dāng)面板更改時,您不必顯式調(diào)用start(),因為它會為您調(diào)用.

有關(guān)更多詳細信息,請參見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方法在哪里,因此此代碼/答案僅是假設(shè)您在自定義面板中有一些start方法.希望我猜對了.在將來,甚至現(xiàn)在,您都應(yīng)該始終發(fā)布MCVE,這樣我們就不必做所有這些猜測了.

來源:https://www./content-1-501601.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多