在不同类的JPanel之间切换 [英] Switch between JPanels of different classes

查看:152
本文介绍了在不同类的JPanel之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不同类的JPanel之间切换?我不希望使用卡片布局.

How do I switch between JPanels of different classes? I do not wish to use a Card Layout.

我有2个课程-MainPage& MenuPage.例如,我想清除contentPane(a JPanel)@ MainPage并将其替换为内容窗格@ MenuPage.为了进行测试,我添加了一个按钮@ MenuPage.

I have 2 classes - MainPage & MenuPage. For instance, I would like to clear the contentPane (a JPanel) @ MainPage and replace it with the content pane @ MenuPage. For testing purposes, I included a button @ MenuPage.

请查看我的以下尝试-它给出了错误:

Please see my following attempt - it gives an error:

java.lang.IllegalArgumentException: adding a window to a container

MainPage

public class MainPage extends JFrame {

    private static JPanel contentPane;
    private JLabel imgBackground;
    private JLabel lblTitle;
    private JLabel imgLogo;
    private Dimension dim;

    //timer
    private final static int interval = 40;
    private int i;
    private Timer t;
    //private JButton start;
    //private JLabel lblLoading;
    private JProgressBar pbar;

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

    public MainPage() {             
        dim = Toolkit.getDefaultToolkit().getScreenSize();      
        System.out.println(dim);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        contentPane.setBounds(0,0,dim.width,dim.height);
        setContentPane(contentPane);

        this.setExtendedState(JFrame.MAXIMIZED_BOTH);       

        t = new Timer (interval, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if (i == 20){
                    t.stop();
                    //start.setEnabled(true);   


                    //refresh + load next page ???              
                    contentPane.removeAll();
                    MenuPage loadpanel2 = new MenuPage();
                    setContentPane(loadpanel2);


                    revalidate();                   
                    repaint();
                }
                else{
                    i++;
                    pbar.setValue(i);
                }               
            }           
        });
        t.start();

MenuPage

public class MenuPage extends JFrame {

    private JPanel contentPane;

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


    public MenuPage() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        setContentPane(contentPane);

        JButton btnSadfsafsa = new JButton("sadfsafsa");
        btnSadfsafsa.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnSadfsafsa.setBounds(10, 52, 89, 23);
        btnSadfsafsa.setEnabled(true);
        btnSadfsafsa.setVisible(true);
        contentPane.add(btnSadfsafsa);
    }
}

推荐答案

java.lang.IllegalArgumentException: adding a window to a container

那很简单.两个GUI都扩展JFrame,因此它们是顶级容器.我们不能将一个顶级容器添加到另一个.

That is pretty straightforward. Both GUIs extend JFrame and are therefore top level containers. We cannot add one top level container to another.

两个GUI都可以扩展JPanel,而不是扩展框架.然后,可以将JPanel(或多个)添加到以main(String[])showGUI()方法实例化的JFrame中.

Instead of extending frame, both GUIs might extend JPanel. A JPanel (or more than one) can then be added to a JFrame instantiated in the main(String[]) or a showGUI() method.

这篇关于在不同类的JPanel之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆