在Jframe中随机放置JPanel

时间:2013-12-01 19:36:35

标签: java swing random

我是一名Java初学者,我试图在JFrame中随机更改JPanel的位置,或者随机更改JPanel中的标签。我设法得到一个网格随机改变颜色,并认为我会尝试在Matrix电影的开头做一些像改变1和0的东西。到目前为止,我无法达到预期的效果,并希望得到一些帮助。在我的代码中,我有随机换色器(我知道它不是我需要的,但我假设我需要向Action Listener添加正确的代码)。 这就是我所拥有的:

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

public class MatrixGrid extends JFrame {
    final int grid = 20;
    final int pnlCnt = grid * grid;
    final JFrame frame = new JFrame("WELCOME TO THE MATRIX");
    final JPanel[] panels = new JPanel[pnlCnt];
    Timer t;

    public MatrixGrid() {
        for (int i = 0; i < panels.length; i++) {
            final String[] labels = new String[]{"0", "1"};
            Random rand = new Random(); 
            int index = rand.nextInt(labels.length);
            String randomTitle = labels[index];
            JLabel label = new JLabel(randomTitle, JLabel.CENTER);
            label.setForeground(Color.green);
            label.setVerticalAlignment(JLabel.CENTER);
            panels[i] = new JPanel();
            panels[i].setBackground(Color.BLACK);
            panels[i].add(label);
            frame.getContentPane().add(panels[i]);
            }
        frame.setLayout(new GridLayout(grid,grid));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
        frame.setVisible(true);

        //I know this won't do what I want, but I think this is where I need 
        //to add code to randomize the panels or labels
        ActionListener action = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < panels.length; i++) {
                    Color mix = new Color(255,255,255);
                    Random random = new Random();
                    int r = random.nextInt(255);
                    int g = random.nextInt(255);
                    int b = random.nextInt(255);

                    if(mix != null) 
                    {
                        r = (r + mix.getRed()) / 2;
                        g = (g + mix.getGreen()) / 2;
                        b = (b + mix.getBlue()) / 2;
                        }
                    Color color = new Color(r, g, b);
                    panels[i].setBackground(color);
                    }
                }
            };

        t = new Timer(100, action);
        t.setRepeats(true);
        t.start();
        }
    public static void main(String args[]) {
        MatrixGrid q = new MatrixGrid();
        }
    }

有人能指出我正确的方向吗?我用谷歌搜索,直到我的眼睛即将脱落。任何帮助将非常感谢。干杯

1 个答案:

答案 0 :(得分:1)

我不知道我是否做了你想做的事。 我更改了构造函数中的for

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

public class NewClass extends JFrame {

final int grid = 20;
final int pnlCnt = grid * grid;
final JFrame frame = new JFrame("WELCOME TO THE MATRIX");
final JPanel[] panels = new JPanel[pnlCnt];
Timer t;

public NewClass() {
    for (int i = 0; i < panels.length; i++) {
        final String[] labels = new String[]{"0", "1"};
        final Random rand = new Random();
        int index = rand.nextInt(labels.length);
        String randomTitle = labels[index];
        final JLabel label = new JLabel(randomTitle, JLabel.CENTER);
        Timer lblt = new Timer(00, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
               label.setText(labels[rand.nextInt(labels.length)]);
            }
        });
        lblt.setRepeats(true);
        lblt.start();
        label.setForeground(Color.green);
        label.setVerticalAlignment(JLabel.CENTER);
        panels[i] = new JPanel();
        panels[i].setBackground(Color.BLACK);
        panels[i].add(label);
        frame.getContentPane().add(panels[i]);
    }
    frame.setLayout(new GridLayout(grid, grid));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
    frame.setVisible(true);

    //I know this won't do what I want, but I think this is where I need 
    //to add code to randomize the panels or labels
    ActionListener action = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < panels.length; i++) {
                Color mix = new Color(255, 255, 255);
                Random random = new Random();
                int r = random.nextInt(255);
                int g = random.nextInt(255);
                int b = random.nextInt(255);

                if (mix != null) {
                    r = (r + mix.getRed()) / 2;
                    g = (g + mix.getGreen()) / 2;
                    b = (b + mix.getBlue()) / 2;
                }
                Color color = new Color(r, g, b);
                panels[i].setBackground(color);
            }
        }
    };

    t = new Timer(00, action);
    t.setRepeats(true);
    t.start();
}

public static void main(String args[]) {
    NewClass q = new NewClass();
    }
}

编辑:我把整个代码,尝试运行它,看看它是否符合你的要求。