Netbeans的灰色屏幕

时间:2013-06-05 02:49:31

标签: java swing jframe keylistener paintcomponent

所以我使用netbeans,我开始进入编码游戏......到目前为止我已经完成了没有任何错误,但是当我运行它只是一个灰色的盒子,我的标题“zachs游戏出现,多数民众赞成....如果你知道问题,请帮忙1 - 谢谢你

package swing9;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;

public class JavaApplication2 extends JFrame implements Runnable {

    int x, y, xDirection, yDirection;
    Font font = new Font("Arial", Font.BOLD | Font.ITALIC, 30);

    public void run() {
        try {
            while (true) {

                move();

                Thread.sleep(5);
            }
        } catch (Exception e) {

            System.out.println("Error");
        }
    }

    public void move() {

        x += xDirection;
        y += yDirection;

        if (x <= 0)
            x = 0;

        if (x >= 300)
            x = 300;

        if (y <= 50)
            y = 50;

        if (y <= 300)
            y = 300;

    }

    public void seyXDir(int xdir) {

        xDirection = xdir;
    }

    public void setYDirection(int ydir) {

        yDirection = ydir;
    }

    public class AL extends KeyAdapter {

        public void keyPressed(KeyEvent e) {

            int keyCode = e.getKeyCode();

            if (keyCode == e.VK_LEFT) {

                int setXDirection = -1;

            }

            if (keyCode == e.VK_RIGHT) {
                int setXDirection = +1;

            }

            if (keyCode == e.VK_UP) {
                int setYDirection = -1;
            }

            if (keyCode == e.VK_DOWN) {
                int setYDirection = +1;

            }

        }

    }

    public void keyReleased(KeyEvent e) {

        int keyCode = e.getKeyCode();

        if (keyCode == e.VK_LEFT) {
            int setXDirection = 0;
        }

        if (keyCode == e.VK_RIGHT) {
            int setXDirecetion = 0;
        }

        if (keyCode == e.VK_UP) {
           int setYDirectiom = 0;
        }

        if (keyCode == e.VK_DOWN) {
            int setYDirecction = 0;
        }

    }

    public JavaApplication2() {

        addKeyListener((KeyListener) new JavaApplication2.AL());

        setTitle("Zachs Game");
        setSize(300, 300);

        setResizable(false);

        setVisible(true);
        setBackground(Color.blue);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        x = 150;
        y = 150;

    }

    public void paintComponent(Graphics g) {

        g.setColor(Color.red);
        g.drawString("Play", 40, 40);
        g.setFont(font);
        g.setColor(Color.red);
        g.fillOval(x, y, 15, 15);

        repaint();
    }

    public static void main(String[] args) {

        new JavaApplication2();

        // threads

        Thread t1 = new Thread();
        t1.start();
    }
}

1 个答案:

答案 0 :(得分:2)

JFrame或其任何超类都没有实现paintComponent方法,因此永远不会被调用。通过添加@Override注释自行检查。

将此方法移至扩展JComponent的新类,并调用super.paintComponent(g)作为第一个语句。

不要在repaint内调用paintComponent,这会造成无限循环并降低性能。 Swing Timers旨在更轻松地与摇摆组件进行互动。使用这些而不是原始线程进行定期更新。

除此之外:默认情况下JFrame无法关注,因此在不使窗口可对焦的情况下不会触发需要对焦的KeyEvents。请改用Key Bindings