gpt4 book ai didi

java - Jframe 中 JPanel 的随机放置

转载 作者:行者123 更新时间:2023-12-01 13:46:43 40 4
gpt4 key购买 nike

我是一名 Java 初学者,我正在尝试随机更改 JFrame 中 JPanel 的位置或随机更改 JPanel 中的标签。我设法让网格随机改变颜色,并想尝试制作类似于《黑客帝国》电影开头不断变化的 1 和 0 的东西。到目前为止,我无法达到预期的结果,需要一些帮助。在我的代码中,我有随机颜色更改器(mutator)(我知道这不是我需要的,但我假设我需要向 Action 监听器添加正确的代码)。这是我所拥有的:

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();
}
}

有人能给我指出正确的方向吗?我一直在谷歌上搜索,直到我的眼睛快要从我的脑袋里掉出来了。任何帮助将非常感激。干杯

最佳答案

我不知道我是否做了你想要的。我更改了构造函数中的 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();
}
}

编辑:我放置了整个代码,尝试运行它,看看它是否符合您的要求。

关于java - Jframe 中 JPanel 的随机放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20316310/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com