Java JPanel如何显示颜色数组中的不同颜色?
问题是当我将正方形JPanel的背景色设置为square.setBackground(colors[j])时,正方形只绘制颜色列表中的第一种颜色,而不显示其他3种颜色。这是我的代码:Java JPanel如何显示颜色数组中的不同颜色?,java,arrays,swing,colors,jpanel,Java,Arrays,Swing,Colors,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 exten
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
}
}
import java.awt.*;
导入java.util.*;
导入javax.swing.*;
导入java.awt.*;
@抑制警告({“未使用”、“串行”})
公共类RegionPartition扩展了JFrame
{
JLayeredPane layeredPane;
JPanel;
JLabel区域片;
专用静态整数延迟=200;
私有颜色[]颜色=新颜色[]{Color.PINK,Color.GREEN,Color.BLACK,Color.RED};
公共区域分区()
{
尺寸板尺寸=新尺寸(500500);
//为此应用程序使用分层窗格
layeredPane=新的JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
regionBoard=新的JPanel();
layeredPane.add(regionBoard,JLayeredPane.DEFAULT_LAYER);
设置布局(新网格布局(4,4));
regionBoard.setPreferredSize(boardSize);
立根(0,0,boardSize.width,boardSize.height);
随机=新随机();
对于(inti=0;i<16;i++){
JPanel square=newjpanel(newborderlayout());
square.setboorder(BorderFactory.createLineBorder(Color.black));
加上(正方形);
正方形。背景(颜色。绿色);
int j=0;
方形退根地面(颜色[j]);
j++;
}
}
{
JPanel面板=新的JPanel()
{
客户[]c=新客户[128];
随机=新随机();
私有最终整数大小=450;
专用整数延迟=9999999;
公共组件(图形g)
{
超级组件(g);
对于(int i=0;i,这是因为每次迭代时总是将j设置为0:
int j=0;
square.setBackground(colors[j]);
j++;
您可能希望将j更改为i或执行嵌套循环,这取决于您在这里真正想要执行的操作
如果要使所有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);
}
}
for(inti=0;i<16;i++){
JPanel square=新JPanel(新网格布局(2,2));
square.setboorder(BorderFactory.createLineBorder(Color.black));
加上(正方形);
for(int j=0;j您正在for循环的范围内实例化int j
,因此它的值不会在多次迭代中保留。您应该在代码中的某个点声明它,以允许它作用于整个for循环
int j = 0;
<for loop>
square.setBackground(colors[j]);
j++;
<end for>
由于color
数组中只有4种颜色,但循环索引超过了此值,因此可以使用:
square.setBackground(colors[ i % colors.length]);
改变正方形的颜色。每行之间的额外间距无助于可读性,缩进会!在设置颜色之前,j始终设置为0…顺便说一句:您不应该在paintComponent()方法中调用repaint()(这将导致组件被反复绘制)。此外,多次调用repaint()是没有用的(使用循环调用它200次)因为它们合并成一个调用。我想更改JPanel正方形的背景颜色。问题是,即使我使用for循环,它也只绘制数组颜色的一种颜色,特别是在这种情况下,它只绘制数组的最后一种颜色!将其更改为I???@user1633022是什么意思“它只绘制数组颜色的一种颜色,特别是在本例中,它只绘制最后一种颜色”这是因为JPanel
s只能有一种背景颜色,每次都要覆盖它。还应该使用模(“%”)运算符,因为数组中只有4种颜色,但通过更改颜色[i]的颜色[j],循环重复16次将每个面板绘制为数组的一种颜色作为1:1映射,即如果您有16种颜色如果目标是简单地绘制一个2x2网格,则没有理由使用多个JPanel。简单地覆盖一个面板中的paintComponent,然后将其大小划分为四个相等的正方形t,将更加干净和传统o color.我按照你的建议做了,但我有编译错误:线程“main”java.lang.ArrayIndexOutOfBoundsException:4在RegionPartition.(RegionPartition.java:55)在RegionPartition.main(RegionPartition.java:109)中出现异常这是一个运行时错误,不是语法错误。无论如何,如果您正确地实现了intj
,就会发生这种情况,所以我必须知道您的颜色目标是什么。
square.setBackground(colors[ i % colors.length]);