单击按钮时如何在jframe上用anotherjpanel替换jpanel

时间:2013-08-30 15:40:05

标签: java swing actionlistener cardlayout

我希望在按下框架左侧的JPanel中的相应按钮时替换框架中心的JPanel。 我正在使用cardlayout来更换中心的面板。 这是我用于在按钮点击时更改面板的示例代码,但它不起作用。任何人都可以让我知道代码中的错误。

Library扩展了JFrame

 Public Library(){
          Container container = getContentPane();
      container.setLayout( new BorderLayout() );
      setExtendedState(JFrame.MAXIMIZED_BOTH);

      banner = new BannerPanel();
      container.add(banner,BorderLayout.NORTH);

      MenuButtons = new MenuButtonPanel();
      container.add(MenuButtons,BorderLayout.WEST);

      SelectedButtonPanel = new SelectedPanel(container);

      setLocationRelativeTo(null);
      setVisible( true );
      init();

      setDefaultCloseOperation(EXIT_ON_CLOSE);
}

MenuButtonPanel扩展了JPanel并在其中有多个按钮。 ButtonHandler的内部类为按钮

实现了hte actionListner
 //inner class
   class ButtonEventHandler implements ActionListener {     
        public void actionPerformed( ActionEvent event ){

            SelectedPanel selectObj = new SelectedPanel();

            if("about".equals(event.getActionCommand()))
                {
                    selectObj.showReplacePanel("about");

                }
                else if("checkout".equals(event.getActionCommand()))
                {
                    selectObj.showReplacePanel("checkout");
                }
                else if("search".equals(event.getActionCommand()))
                {
                    selectObj.showReplacePanel("search");
                }

SelectedPanel应该在框架的中心显示替换的Jpanel

    public SelectedPanel() {}

public SelectedPanel(Container framePane)
{
    addSelectedPanel(framePane);
}
public void addSelectedPanel(Container framePane)
{
    cardPanel = new JPanel();
    //set layout of panel
    cardPanel.setLayout(new CardLayout());
    cardPanel.setBorder(null);
    cardPanel.setForeground(new Color(0, 0, 0));
    //this.selObj = selObj;
            aboutPanel = new About();
            checkoutPanel = new Checkout();
            searchPanel = new Search();
                           cardPanel.add(aboutPanel,ABOUTBUTTON);
            cardPanel.add(checkoutPanel,CHECKOUTBUTTON);
            cardPanel.add(searchPanel, SEARCHBUTTON);
                             framePane.add(cardPanel, BorderLayout.CENTER);  }

selectedPanel类还有方法showReplacePanel(),它在buttonClick的actionPerformed中调用。

public void showReplacePanel(String selObj)
{
    System.out.println("cardlayout entered");
    CardLayout cl = (CardLayout)(cardPanel.getLayout());
    System.out.println("cardlayout entered");
    switch(selObj){        
    case "about":

        cl.show(cardPanel,ABOUTBUTTON);
                break;          
    case "search":
        //this.repPanel = searchPanel;
        cl.show(cardPanel,SEARCHBUTTON);
        break;
    case "checkout":
        cl.show(cardPanel,CHECKOUTBUTTON);
        //this.repPanel = checkoutPanel;
        break;

1 个答案:

答案 0 :(得分:1)

我为您创建了一个示例,请查看actionPerformed(ActionEvent e)方法

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PanelSwap extends JPanel implements ActionListener {

    JPanel firstPanel = new JPanel();
    JPanel secondPanel = new JPanel();

    public PanelSwap() {
        super(new BorderLayout());
        firstPanel.setBackground(Color.RED);

        secondPanel.setBackground(Color.YELLOW);

        JButton swap1 = new JButton("SwapToYellow");
        swap1.addActionListener(this);

        JButton swap2 = new JButton("SwapToRed");
        swap2.addActionListener(this);

        firstPanel.add(swap1);
        secondPanel.add(swap2);

        add(firstPanel);
    }

    /** Listens to the buttons and perfomr the swap. */
    public void actionPerformed(ActionEvent e) {

        for (Component component : getComponents())
            if (firstPanel == component) {
                remove(firstPanel);
                add(secondPanel);

            } else {
                remove(secondPanel);
                add(firstPanel);
            }

        repaint();
        revalidate();
    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event-dispatching thread.
     */
    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("PanelSwap");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create and set up the content pane.
        JComponent newContentPane = new PanelSwap();
        newContentPane.setOpaque(true); // content panes must be opaque
        frame.setContentPane(newContentPane);

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}