在 JFrame 上制作网格并填充随机颜色

0 java eclipse swing jframe

for(int i = 0; i < sizex/10; i++)
{
    for(int u = 0; u < sizey/10; u++)
    {
        JPanel temp = new JPanel();
        //temp.setSize(10, 10);
        temp.setBounds(i*10,u*10, 10, 10);
        //temp.setLocation(i*10, u*10);
        Random r = new Random();
        int rand = r.nextInt(4-0);
        if(rand == 0)
        {
            temp.setBackground(Color.GREEN);
        }
        else if(rand == 1)
        {
            temp.setBackground(Color.BLUE);
        }
        else if(rand == 2)
        {
            temp.setBackground(Color.RED);
        }
        else if(rand == 3)
        {
            temp.setBackground(Color.MAGENTA);
        }

        frame.add(temp);
    }
}
Run Code Online (Sandbox Code Playgroud)

在我这里的代码中,它背后的逻辑有效(在我的脑海中),如果我将大小xy除以 100 并使框的大小为 100 而不是 10 ,则此代码有效。

在它当前的情况下,它会生成看起来大小合适的框,但应用程序的侧面只有几个而不是全屏。

这是该应用程序的图片:

应用程序

Mad*_*mer 5

你可以...

使用GridLayout...

随机单元格

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RandomCells {

    public static void main(String[] args) {
        new RandomCells();
    }

    public RandomCells() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridLayout(10, 10, 0, 0));
            Random rnd = new Random();
            Color[] colors = new Color[]{Color.GREEN, Color.BLUE, Color.RED, Color.MAGENTA};
            for (int col = 0; col < 10; col++) {
                for (int row = 0; row < 10; row++) {
                    JPanel cell = new JPanel() {
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(10, 10);
                        }
                    };
                    int color = rnd.nextInt(4);
                    cell.setBackground(colors[color]);
                    add(cell);
                }
            }
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅如何使用 GridLayout

或者你可以...

使用GridBagLayout...

随机单元

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RandomCells {

    public static void main(String[] args) {
        new RandomCells();
    }

    public RandomCells() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            Random rnd = new Random();
            Color[] colors = new Color[]{Color.GREEN, Color.BLUE, Color.RED, Color.MAGENTA};
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridy = 0;
            for (int col = 0; col < 10; col++) {
                gbc.gridx = 0;
                for (int row = 0; row < 10; row++) {
                    JPanel cell = new JPanel() {
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(10, 10);
                        }
                    };
                    int color = rnd.nextInt(4);
                    cell.setBackground(colors[color]);
                    add(cell, gbc);
                    gbc.gridx++;
                }
                gbc.gridy++;
            }
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅如何使用 GridBagLayout

有什么不同?

GridLayout 将始终均匀地调整其组件的大小,以便它们填充可用空间,因此,例如,如果您调整窗口大小,则所有面板都将更改大小以尝试填充可用空间。

GridBagLayout(在我展示的配置中)将继续尊重preferredSize面板,因此当您调整窗口大小时,面板不会改变大小