免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

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

我敢肯定有人問過這個問題,但是今天我的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.icode9.com/content-1-501601.html
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Swing 之 Layout
Swing使用JFileChooser選擇文件目錄
swing組件的paint問題_技術(shù)專輯
java學(xué)習(xí)——59.文本編輯組件
布局管理器之CardLayout(卡片布局管理器)
JScrollPane的使用
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服