Java 在不同类中的JPanel之间切换

Java 在不同类中的JPanel之间切换,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,我偶然发现了这种复杂情况,花了4个多小时调试和谷歌搜索,但都没有用 基本上我这里有1个JFrame,2个JPanel。 我将JFrame设置为JPanel的第1个内容窗格,当我运行应用程序时,JFrame将显示,其中包含JPanel 现在这个JPanel里面有1个JButton,当我点击它时,我希望它切换到另一个JPanel。正如您从代码中看到的,当我单击JButton(AddProduct)时,我希望onlineshoppadpane切换到AddProduct。我尝试使用CardLayout,

我偶然发现了这种复杂情况,花了4个多小时调试和谷歌搜索,但都没有用

基本上我这里有1个JFrame,2个JPanel。 我将JFrame设置为JPanel的第1个内容窗格,当我运行应用程序时,JFrame将显示,其中包含JPanel

现在这个JPanel里面有1个JButton,当我点击它时,我希望它切换到另一个JPanel。正如您从代码中看到的,当我单击JButton(AddProduct)时,我希望onlineshoppadpane切换到AddProduct。我尝试使用CardLayout,但它只有NSEW格式

package OnlineShop.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OnlineShopMainFrame extends JFrame {

    /**
     * Launch the application.
     */
    AddProduct Add;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
                    MainFrame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public OnlineShopMainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);

        OnlineShopAdPane AdPanel = new OnlineShopAdPane();
        setContentPane(AdPanel);

    }


}

package OnlineShop.ui;

import javax.swing.JPanel;
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;

public class OnlineShopAdPane extends JPanel {

    /**
     * Create the panel.
     */

    public OnlineShopAdPane() {

        JLabel lblWhatDoYou = new JLabel("What do you want to do?");
        lblWhatDoYou.setBounds(28, 26, 160, 26);
        add(lblWhatDoYou);

        JButton btnAddProduct = new JButton("Add Product");
        btnAddProduct.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
            MainFrame.removeAll();
            MainFrame.add(new AddProduct());
            MainFrame.revalidate();
            MainFrame.repaint();
            }
        });

        btnAddProduct.setBounds(46, 75, 115, 23);
        add(btnAddProduct);

    }

}
package OnlineShop.ui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddProduct extends JPanel {
    private JTextField textField;

    /**
     * Create the panel.
     */
    public AddProduct() {

        JLabel lblProductName = new JLabel("Product Name:");
        lblProductName.setBounds(35, 26, 77, 24);
        add(lblProductName);

        JLabel lblProductDescription = new JLabel("Product Description:");
        lblProductDescription.setBounds(10, 50, 106, 24);
        add(lblProductDescription);

        textField = new JTextField();
        textField.setBounds(116, 28, 141, 20);
        add(textField);
        textField.setColumns(10);

        JTextArea textArea = new JTextArea();
        textArea.setBounds(116, 66, 141, 112);
        add(textArea);

        JButton btnClose = new JButton("Close");
        btnClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }
        });
        btnClose.setBounds(223, 244, 89, 23);
        add(btnClose);

    }

}

package OnlineShop.ui;

import javax.swing.JPanel;
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;

public class OnlineShopAdPane extends JPanel {

    /**
     * Create the panel.
     */

    public OnlineShopAdPane() {

        JLabel lblWhatDoYou = new JLabel("What do you want to do?");
        lblWhatDoYou.setBounds(28, 26, 160, 26);
        add(lblWhatDoYou);

        JButton btnAddProduct = new JButton("Add Product");
        btnAddProduct.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
            MainFrame.removeAll();
            MainFrame.add(new AddProduct());
            MainFrame.revalidate();
            MainFrame.repaint();
            }
        });

        btnAddProduct.setBounds(46, 75, 115, 23);
        add(btnAddProduct);

    }

}
package OnlineShop.ui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddProduct extends JPanel {
    private JTextField textField;

    /**
     * Create the panel.
     */
    public AddProduct() {

        JLabel lblProductName = new JLabel("Product Name:");
        lblProductName.setBounds(35, 26, 77, 24);
        add(lblProductName);

        JLabel lblProductDescription = new JLabel("Product Description:");
        lblProductDescription.setBounds(10, 50, 106, 24);
        add(lblProductDescription);

        textField = new JTextField();
        textField.setBounds(116, 28, 141, 20);
        add(textField);
        textField.setColumns(10);

        JTextArea textArea = new JTextArea();
        textArea.setBounds(116, 66, 141, 112);
        add(textArea);

        JButton btnClose = new JButton("Close");
        btnClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }
        });
        btnClose.setBounds(223, 244, 89, 23);
        add(btnClose);

    }

}

我认为使用
CardLayout
可以解决这个问题,但另一种方法是使用一个处理程序来切换面板

    private JComponent container; // this could be your Frame
    private JComponent loadedComponent;

public void loadContent(JComponent component, Object object ) {

        if (loadedComponent != null) {
            loadedComponent.setVisible(false);
            container.remove(loadedComponent);
            loadedComponent = null;
        }
        //TODO may check layout
        container.add(component,object);
        component.setVisible(true);
        loadedComponent = component;
        container.validate();
    }

问题可能出现在类
onlineshoppadpane.java

OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
MainFrame.removeAll();
MainFrame.add(new AddProduct());
MainFrame.revalidate();
MainFrame.repaint();
您没有引用嵌套JPanel的框架。而是创建一个新的
OnlineShopMainFrame

我尝试使用CardLayout,但它只有NSEW格式

package OnlineShop.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OnlineShopMainFrame extends JFrame {

    /**
     * Launch the application.
     */
    AddProduct Add;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
                    MainFrame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public OnlineShopMainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);

        OnlineShopAdPane AdPanel = new OnlineShopAdPane();
        setContentPane(AdPanel);

    }


}
这是什么意思?CardLayout仅包含两个或多个面板。一次只能看到一个面板。每个面板可以使用它想要在面板上布局组件的任何布局

当我单击它时,我希望它切换到另一个JPanel

这正是CardLayout所做的。有关工作示例和说明,请参见上的Swing教程


每当我看到像remove/add/revalidate/repaint这样的代码时,它几乎总是应该被CardLayout替换掉est@PrR3很高兴知道,谢谢。
N或者是最新的
-这可能就是它的意思,但它意味着什么?没有操作建议的NSEW格式。遵循Java命名约定。变量名不应以大写字符开头。“MainFrame”应该是
MainFrame
。我要注意命名惯例,它成了我的一个坏习惯呵呵。我想我把卡片布局中的NSEW格式搞砸了。我将再次尝试CardLayout,看看它是否有助于解决我的问题。我会再给你们回电话的~!我尝试在OnlineShopAdPane中键入以下代码:OnlineShopMainFrame大型机;但是当我运行程序时,它给出了我的错误..这段代码不是新的!我只是想给你一个提示,你的错误是从哪里来的,原因是什么…我如何引用我的JPanel嵌套的框架?我刚才发现了如何引用JPanel所在的框架,谢谢你给我强调!我尝试了CardLayout,它只允许我在里面有一个类似swing的组件。但我会接受你的建议,再次尝试CardLayout:)我会再次回到这个话题~谢谢你的支持help@kyle,将面板添加到CardLayout。一个面板可以有任意多个要添加到其中的组件。看看教程,这个面板有不止一个组件。我按照你的建议做了,效果很好,谢谢camickr!