gpt4 book ai didi

java - 我想知道我的代码是否缺少某些内容,或者我是否写错了某些内容

转载 作者:行者123 更新时间:2023-12-01 23:49:21 25 4
gpt4 key购买 nike

我为特定游戏的窗口编写了代码。而且,由于某种原因,除非我调整窗口大小,否则我的板无法显示在窗口上。每次运行程序时,我都必须稍微调整窗口大小才能显示我的板。希望您理解并希望您能帮助我。

代码:

import javax.swing.JFrame;

public class Window
{
public static final int WIDTH = 307, HEIGHT = 637;
private JFrame window;
private Board board;

public Window()
{
window = new JFrame("TETRIS");
window.setSize(WIDTH, HEIGHT);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
window.setLocationRelativeTo(null);
window.setVisible(true);

board = new Board();
window.add(board);
window.addKeyListener(board);
}

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

我不确定是否需要在这里编写板的代码,所以我只写了第一行。

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionListener;

public class Board extends JPanel implements KeyListener
{
private static final long serialVersionUID = 1L;


private BufferedImage blocks;
private Shape[] shapes = new Shape[7];
private Shape currentShape;
private Timer timer;

private final int blockSize = 30;
private final int boardWidth = 10, boardHeight = 20;
private int [][] board = new int [boardHeight][boardWidth];
private final int FBS = 60;
private final int delay = 1000/60;

private boolean gameOver = false;

...

提前致谢!

最佳答案

Java 有一个线程负责 View 事件,因此您应该在那里更改 View 属性:

 public static void main(String[] args) 
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Window();
}
}
}

关于java - 我想知道我的代码是否缺少某些内容,或者我是否写错了某些内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58225109/

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