【问题标题】:Java code shows no errors, but JFrame won't showJava 代码没有显示错误,但 JFrame 不会显示
【发布时间】:2013-10-08 16:42:32
【问题描述】:

我有一个项目在几天后到期,我在 Eclipse 中的代码没有显示任何错误,也没有任何警告,但游戏 (JFrame) 不会出现。我相信这个错误与我做运动的方式有关

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

Movement.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) { }        
}

托马斯

【问题讨论】:

    标签: java swing jframe keylistener


    【解决方案1】:

    尝试使用setPreferredSize(width, height);

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

    【讨论】:

    • 谢谢,黑暗王子!我删除了extends main 并制作了charXcharY public int static,修复了JFrame;但是现在我的角色不会出现。看着它自动取款机。
    • 我想通了!我觉得很愚蠢:(我最初使用的是不同的计算机,文件位于不同的位置。糟糕!
    【解决方案2】:

    在您的 main() 方法中,您正在创建 Main 对象(从 JFrame 扩展而来),但您尚未将其设置为可见。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多