我敢肯定有人問過這個問題,但是今天我的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