简体   繁体   中英

Adding JPanels to JPanel array

I have declared an array:

private javax.swing.JPanel[] panelArray = new javax.swing.JPanel[3];

I also have 3 panels: panel0, panel1 and panel2. Can I add these panels to the array? ie

panelArray[0] = panel0;
panelArray[1] = panel1;
panelArray[2] = panel2;

And then manipulate the arrays like this?

boolean[] myBools; .... then set them as true/false
for(int i=0; i<3; i++)
{
    if(myBools[i])
        panelArray[i].setVisible(true)
}

Because that does not work for me

What you want to do can be done, but here's a few points to bear in mind:

  1. Make sure you initialise the JPanels before referencing them.
  2. The statement "panelArray[i].setVisible(true)" needs a semicolon after it.
  3. None of these panels will be visible unless you add them to another component, such as a JFrame.
  4. Rather than state javax.swing.JPanel, you could just import the JPanel at the top of the page and refer to it as simply JPanel.
  5. Your "if" statement is unnecessary. Just do .setVisible(myBools[i]);

Hope these were of some help to you.

On my side it's working fine, in this program :

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

public class MyPanel
{   
    private JPanel[] panelArray = new JPanel[3];
    private boolean[] myBools = new boolean[]{false, false, false};
    private int counter = 0; 
    private int prvPanelCounter = 0;
    private Timer timer;
    private ActionListener timerAction = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            counter++;
            if (counter > 2)
                counter = 0;
            myBools[counter] = true;
            for (int i = 0; i < 3; i++) 
            {
                if (myBools[i])
                {
                    panelArray[i].setVisible(myBools[i]);                   
                    panelArray[prvPanelCounter].setVisible(myBools[prvPanelCounter]);
                    myBools[i] = false; 
                    prvPanelCounter = i;
                    break;
                }
            }
        }
    };

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Locate Mouse Position");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel panel0 = new JPanel();
        panel0.setOpaque(true);
        panel0.setBackground(Color.BLUE);       
        JPanel panel1 = new JPanel();
        panel1.setOpaque(true);
        panel1.setBackground(Color.RED);
        JPanel panel2 = new JPanel();
        panel2.setOpaque(true);
        panel2.setBackground(Color.DARK_GRAY);

        panelArray[0] = panel0;
        panelArray[1] = panel1;
        panelArray[2] = panel2;

        JComponent contentPane = (JComponent) frame.getContentPane();
        contentPane.setLayout(new GridLayout(0, 1));
        frame.add(panel0);  
        frame.add(panel1);  
        frame.add(panel2);  
        panel0.setVisible(myBools[counter]);
        panel1.setVisible(myBools[counter]);
        panel2.setVisible(myBools[counter]);
        frame.setSize(300, 300);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        timer = new Timer(1000, timerAction);
        timer.start();
    }

    public static void main(String\u005B\u005D args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new MyPanel().createAndDisplayGUI();
            }
        });
    }
}

Yes, you can. Did you really not initialize the myBools array with new ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM