不同类别的JPanels之间切换(Switch between JPanels of differe

2019-10-21 13:26发布

如何不同类别的JPanels之间切换? 我不希望使用卡布局。

我有2类- MainPageMenuPage 。 举例来说,我想清楚了contentPane (一JPanel )@ MainPage并与内容窗格@替换它MenuPage 。 出于测试目的,我包括一个按钮@ MenuPage

请参阅我下面的尝试 - 它提供了一个错误:

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);
    }
}

Answer 1:

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

这是非常简单的。 两者的GUI延伸JFrame ,因此顶层容器。 我们不能在同一个顶层容器添加到另一个。

相反延伸的框架,无论是图形用户界面可能会延长JPanel 。 甲JPanel (或多于一个)然后被添加到JFrame在实例化的main(String[])showGUI()方法。



文章来源: Switch between JPanels of different classes