gpt4 book ai didi

关于 GridLayout 和 paintComponent 方法的 Java Swing 问题

转载 作者:行者123 更新时间:2023-11-29 04:03:32 25 4
gpt4 key购买 nike

在这个程序中,我们需要创建一个 8x8 的“LifeCell”小部件网格。讲师没有提到小部件必须是 Shape 的对象,所以我继续使用 GridLayout 类。 GridLayout 类工作正常(据我所知,因为没有视觉辅助来确认。)该程序的目的是玩生命游戏,用户可以点击其中一个LifeCell 小部件并在“活着”或“死亡”状态之间切换。

我的问题在很大程度上取决于如何绘制要绘制的单元格。这可能是我的代码有问题,但我不是 100% 确定。

程序2.java

public class Program2 extends JPanel implements ActionListener {
private LifeCell[][] board; // Board of life cells.
private JButton next; // Press for next generation.
private JFrame frame; // The program frame.

public Program2() {
// The usual boilerplate constructor that pastes the main
// panel into a frame and displays the frame. It should
// invoke the "init" method before packing the frame
frame = new JFrame("LIFECELL!");
frame.setContentPane(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.init();
frame.pack();
frame.setVisible(true);
}
public void init() {
// Create the user interface on the main panel. Construct
// the LifeCell widgets, add them to the panel, and store
// them in the two-dimensional array "board". Create the
// "next" button that will show the next generation.
LifeCell[][] board = new LifeCell[8][8];
this.setPreferredSize(new Dimension(600, 600));
this.setBackground(Color.white);
this.setLayout(new GridLayout(8, 8));
// here is where I initialize the LifeCell widgets
for (int u = 0; u < 8; u++) {
for (int r = 0; r < 8; r++) {
board[u][r] = new LifeCell(board, u, r);
this.add(board[u][r]);
this.setVisible(true);

}
}

生命细胞.java

 public class LifeCell extends JPanel implements MouseListener {
private LifeCell[][] board; // A reference to the board array.
private boolean alive; // Stores the state of the cell.
private int row, col; // Position of the cell on the board.
private int count; // Stores number of living neighbors.

public LifeCell(LifeCell[][] b, int r, int c) {
// Initialize the life cell as dead. Store the reference
// to the board array and the board position passed as
// arguments. Initialize the neighbor count to zero.
// Register the cell as listener to its own mouse events.
this.board = b;
this.row = r;
this.col = c;
this.alive = false;
this.count = 0;
addMouseListener(this);
}

这是 paintComponent 方法:

   public void paintComponent(Graphics gr) {
// Paint the cell. The cell must be painted differently
// when alive than when dead, so the user can clearly see
// the state of the cell.
Graphics2D g = (Graphics2D) gr;
super.paintComponent(gr);
g.setPaint(Color.BLUE);
}

我不需要确切的解决方案来修复它,但我无能为力地试图让它工作。

谢谢。

编辑:

我添加了更多的 Program2.java 类片段,明天我可以回来查看我要去 sleep 了,我感谢所有帮助的人。

编辑#2:

当我用 8x8 GridLayout 填充我的框架时,我真正感到困惑的是,由于缺少更好的词,每个单独的“单元格”都是 LifeCell 类型。如何为每个 LifeCell 涂上不同的颜色?如果这对你们有任何意义,我可以尝试尽可能多地修改它。还有 camickr,我会看看那个网站,谢谢。

可以找到赋值here以避免对我的问题和/或代码片段产生任何混淆。

最佳答案

JPanel 没有默认的首选大小或可见内容。您需要添加某种可见组件(例如 JLabel)或为其指定首选大小。

除此之外,如果您按如下方式设置,您的布局应该可以正常工作:

JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
cp.add(new JLabel(i + "-" + j));
frame.pack();
frame.setVisible(true);

关于关于 GridLayout 和 paintComponent 方法的 Java Swing 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1817789/

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