JPanel如何从一系列颜色中显示不同的颜色?

时间:2012-08-29 12:57:25

标签: java arrays swing colors jpanel

问题是当我将方形JPanel的背景颜色设置为square.setBackground(colors [j])时,方形仅绘制颜色列表的第一种颜色而不显示其他颜色3.这是我的代码:< / p>

import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;

@SuppressWarnings({ "unused", "serial" })

public class RegionPartition extends JFrame
{
    JLayeredPane layeredPane;
    JPanel regionBoard;
    JLabel regionPiece;

    private static int DELAY = 200;

    private Color[] colors = new Color[]{Color.PINK, Color.GREEN, Color.BLACK, Color.RED};

    public RegionPartition()
    {
        Dimension boardSize = new Dimension(500, 500);

        //  Use a Layered Pane for this this application
        layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(boardSize);

        regionBoard = new JPanel();

        layeredPane.add(regionBoard, JLayeredPane.DEFAULT_LAYER);

        regionBoard.setLayout( new GridLayout(4, 4) );

        regionBoard.setPreferredSize( boardSize );
        regionBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        Random random = new Random();


        for (int i = 0; i < 16; i++) {          
            JPanel square = new JPanel(new BorderLayout());
            square.setBorder(BorderFactory.createLineBorder(Color.black));
            regionBoard.add( square );  

            square.setBackground(Color.green);

            int j=0;

            square.setBackground(colors[j]);

            j++;
        }
    }

    {
        JPanel panel = new JPanel()  
        {  
            Clients[] c = new Clients[128];

            Random random = new Random();

            private final int SIZE = 450;
            private int DELAY = 9999999;
            public void paintComponent (Graphics g)
            {
                super.paintComponent(g);

                for (int i=0; i<c.length; i++)
                {
                    int x = ( int ) ( random.nextFloat() * SIZE ) + 10;
                    int y = ( int ) ( random.nextFloat() * SIZE ) + 10;

                    g.drawOval( x, y, 10, 10 );
                    g.fillOval(x, y, 10, 10);
                }

                for (int j=0; j<DELAY; j++)
                {
                    repaint();
                }
            }
        };  

        panel.setOpaque(false);  

        //Set the glass pane in the JFrame  
        setGlassPane(panel);  

        //Display the panel  

        panel.setVisible(true);  
    }

    public static void main(String[] args)
    {
        JFrame frame = new RegionPartition();

        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
        frame.pack();
        frame.setResizable(true);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);

    }

    protected void paintComponent(Graphics g)
    {
        // TODO Auto-generated method stub
    }
}

3 个答案:

答案 0 :(得分:5)

这是因为你总是在每次迭代时将j设置为0:

    int j=0;

    square.setBackground(colors[j]);

    j++;

您可能想要为i更改j或执行嵌套循环,这取决于您真正想要在此处执行的操作。

如果你想让所有16个方格都以网格方式拥有所有四种颜色,你可能想要将你的循环更改为:

     for (int i = 0; i < 16; i++) {          
        JPanel square = new JPanel(new GridLayout(2,2));
        square.setBorder(BorderFactory.createLineBorder(Color.black));
        regionBoard.add( square );  



        for(int j=0; j<4; ++j){
          JPanel insideSquare = new JPanel();
          insideSquare.setBackground(colors[j]);
          square.add(insideSquare);
        }
    }

答案 1 :(得分:4)

因为color数组中只有4种颜色,但循环索引超过此值,您可以使用:

square.setBackground(colors[ i % colors.length]);

替换方块的颜色。

答案 2 :(得分:3)

您在for循环的范围内实例化int j,因此不会在多次迭代中保留其值。您应该在代码中的某一点声明它,以允许它覆盖整个for循环。

int j = 0;
<for loop>
    square.setBackground(colors[j]);
    j++;
<end for>

但是,在这种情况下,j正在服务于i,其中i足以作为数组索引。完全删除j并执行以下操作更为正确:

    square.setBackground(colors[i]);