1

当按下框架左侧 JPanel 中的相应按钮时,我希望替换框架中心的 JPanel。我正在使用卡片布局来更换中心的面板。这是我用于更改按钮单击面板的示例代码,但它不起作用。谁能让我知道代码中有什么问题。

库扩展了 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;
4

1 回答 1

2

我给你做了一个例子,看看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();
            }
        });
    }
}
于 2013-08-30T16:20:59.667 回答