gpt4 book ai didi

Java代码没有显示错误,但JFrame不会显示

转载 作者:行者123 更新时间:2023-12-01 14:02:26 25 4
gpt4 key购买 nike

我有一个项目要在几天后到期,我在 Eclipse 中的代码没有显示错误,也没有警告,但是游戏(JFrame)不会显示。我相信这个错误与我完成 Action 的方式有关

this.addKeyListener(movement);

欢迎任何想法!

Main.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Main extends JFrame implements Runnable {
private Movement movement = new Movement();

public int width = 800;
public int height = 600;
public int fps = 1000;
public int score;
public int charX;
public int charY;
public int charUp;
public int charDown;
public int charLeft;
public int charRight;
public int movementSpeed = 5;
public int movementFrame = 0;
public int movementDiagonal = 10;

public boolean bCharUp = false;
public boolean bCharDown = false;
public boolean bCharLeft = false;
public boolean bCharRight = false;

public Color cytoplasm = new Color(50,130,255);

public Image character;

public Thread game;

//Double Buffer
private Image dbImage;
private Graphics dbg;

public Main() {
//Images
ImageIcon characterImage = new ImageIcon("F:/workplace/com.thecellsells.lysosome/src/com/thecellsells/lysosome/Lysosome.gif");
character = (characterImage.getImage());

//Game Properties
setSize(width, height);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(cytoplasm);
this.addKeyListener(movement);


//Threads
game = new Thread(this);
game.start();

//Other
charY = height/2 - 10;
charX = width/2 - 16;
}

public void paint(Graphics g) {
//Double Buffer
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);


}

public void paintComponent(Graphics g) {
g.drawImage(character, charX, charY, this);
repaint();
}

public static void main(String[] args) {
@SuppressWarnings("unused")
Main Main = new Main();
}

@Override
public void run() {
while (true) {
fpsSetter();
}
}

//FPS -- set's how fast game runs
@SuppressWarnings("static-access")
public void fpsSetter() {
try {
game.sleep(fps/fps);
} catch (Exception e) {
e.printStackTrace();
}
}
}

运动.java

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

@SuppressWarnings("serial")
public class Movement extends Main implements KeyListener {
public int charUp;
public int charDown;
public int charLeft;
public int charRight;
public int movementSpeed = 5;
public int movementFrame = 0;
public int movementDiagonal = 10;

public boolean bCharUp = false;
public boolean bCharDown = false;
public boolean bCharLeft = false;
public boolean bCharRight = false;
public void keyPressed (KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W ) {

bCharUp = true;
if (bCharUp) {
charY -= 1;
}
}
if (e.getKeyCode() == KeyEvent.VK_A) {
bCharLeft = true;
if (bCharLeft) {
charX -=1;
}
}
if (e.getKeyCode() == KeyEvent.VK_W ) {
bCharDown = true;
if (bCharDown) {
charY -=1;
}
}
if (e.getKeyCode() == KeyEvent.VK_D) {
bCharRight = true;
if (bCharRight) {
charX +=1;
}
}

}
@Override
public void keyReleased (KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W ) {
bCharUp = false;
}
if (e.getKeyCode() == KeyEvent.VK_A) {
bCharLeft = false;
}
if (e.getKeyCode() == KeyEvent.VK_S) {
bCharDown = false;
}
if (e.getKeyCode() == KeyEvent.VK_D) {
bCharRight = false;
}
}
@Override
public void keyTyped(KeyEvent e) { }
}

托马斯

最佳答案

尝试使用setPreferredSize(width, height);

编辑:问题在于您在 Movement 类中扩展了 Main 类。因此,当您启动程序时,运动类会调用 Main 类,并再次初始化运动类,从而形成竞争条件。只是不要扩展 Main 类并将变量 charXcharY 设为静态。因此,您可以使用 Main.. 访问任何地方的变量。

关于Java代码没有显示错误,但JFrame不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19253544/

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