将JPanel添加到JPanel数组

时间:2012-04-27 13:08:28

标签: java arrays swing jpanel

我已经声明了一个数组:

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

我还有3个面板:panel0,panel1和panel2。我可以将这些面板添加到阵列中吗?即

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

然后像这样操纵数组?

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

因为那对我不起作用

3 个答案:

答案 0 :(得分:5)

你想做的事情可以做,但这里要记住几点:

  1. 确保在引用之前初始化JPanel。
  2. 语句“panelArray [i] .setVisible(true)”后面需要一个分号。
  3. 除非您将这些面板添加到另一个组件(例如JFrame),否则这些面板都不可见。
  4. 您可以直接导入页面顶部的JPanel,而不是状态javax.swing.JPanel,只需将其引用为JPanel。
  5. 您的“if”声明是不必要的。做.setVisible(myBools [i]);
  6. 希望这些对你有所帮助。

答案 1 :(得分:5)

在我这边,它在这个程序中工作正常:

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

答案 2 :(得分:1)

是的,你可以。您是否真的没有使用myBools初始化new数组?