java jpanel颜色_java – JPanel如何从颜色数组中显示不同的颜色?

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

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

{

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

{

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值