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

时间:2013-10-08 16:42:33

标签: java swing jframe keylistener

我有一个项目将在几天后到期,我在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) { }        
}

托马斯

3 个答案:

答案 0 :(得分:1)

在你的代码中,不要在运动中扩展Main类,你正在超载堆栈!

注意:在你的按键中,down应该是KeyEvent.VK_S,所以它不会上升。

试试这个。

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

public class Movement 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 int charX;
public int charY;

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

public Movement(int charX , int charY){
this.charX = charX;
this.charY = charY;

}

public Movement(){
this(0,0);
}

public void keyPressed (KeyEvent e) {
System.out.println("" + e.toString());
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_S ) {
    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) { }        

Movement.class

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;

public int width = 800;
public int height = 600;
public int fps = 1000;
public int score;
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() {
movement = new Movement();
//Images
ImageIcon characterImage = new ImageIcon("image here");
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
movement.charY = height/2 - 10;
movement.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, movement.charX, movement.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();
}
}
}

答案 1 :(得分:0)

尝试使用setPreferredSize(width, height);

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

在任何地方访问变量

答案 2 :(得分:0)

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