JAVA编程。为何小球能在窗口内弹跳,而由键盘上下左右键控制的矩形方框在窗口内没有反应?求大神解答

importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.RenderingHints;impor... import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.RenderingHints;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.event.*;import java.awt.*;@SuppressWarnings("serial")public class Game extends JPanel {int direction=1,NE=0,SE=1,SW=2,NW=3;//int x = 0;int y = 0;int a=xmax-300,b=ymax-200;//定义矩形方块位置int ballsize=30;static int xmax=600;static int ymax=800;//private static TextField tf;private void moveBall() { switch(direction){ case 0: x=x+1; y=y-1; if(y<=0) direction=SE;//1 if(x>=xmax-ballsize-22) direction=NW;//3 break; case 1: x=x+1; y=y+1; if(y>=ymax-ballsize-55) direction=NE;//0 if(x>=xmax-ballsize-22) direction=SW;//2 break; case 2: x=x-1; y=y+1; if(y>=ymax-ballsize-55) direction=NW;//3 if(x<=0) direction=SE;//1 break; case 3: x=x-1; y=y-1; if(y<=0) direction=SW;//2 if(x<=0-ballsize) direction=NE;//0 break; } }public void moveRect(){ addKeyListener(new KeyAdapter(){ /*********注意这里***********/ public void keyPressed(KeyEvent e){ if(e.getKeyCode()== KeyEvent.VK_UP){ //System.out.println("\n Go Up"); b=b-5;/*********注意这里***********/ } else if(e.getKeyCode()== KeyEvent.VK_DOWN){ // System.out.println("\n Go Down"); b=b+5;/*********注意这里***********/ } else if(e.getKeyCode()== KeyEvent.VK_LEFT){ //System.out.println("\n Go Left"); a=a-5;/*********注意这里***********/ } else if(e.getKeyCode()== KeyEvent.VK_RIGHT){ //System.out.println("\n Go Right"); a=a+5;/*********注意这里***********/ } //else // System.out.println(e.getKeyChar()); // repaint();/*********注意这里***********/ } } );} @Overridepublic void paint(Graphics g) {super.paint(g); Graphics2D g2d = (Graphics2D) g;g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.BLUE); g.fillOval(x, y, 30, 30); g.fillRect(a, b , 100, 10);//绘制一个长100宽10的黑色矩形 }//public interface ActionListener extends MouseMotionListener//{ //public void mouseMoved(MouseEvent m) //{ //}//} public static void main(String[] args) throws InterruptedException { JFrame frame = new JFrame("Sample Frame"); Game game = new Game(); frame.add(game); frame.setSize(600, 800); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //tf=new TextField(30); //frame.add("North",tf); while (true) {game.moveRect();game.moveBall();game.repaint();Thread.sleep(3); } }} 展开
 我来答
_诗礼_
2016-06-16 · 超过69用户采纳过TA的回答
知道小有建树答主
回答量:184
采纳率:50%
帮助的人:108万
展开全部
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.KeyEventPostProcessor;
import java.awt.KeyboardFocusManager;
import java.awt.RenderingHints;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.event.*;
import java.awt.*;

@SuppressWarnings("serial")
public class Game extends JPanel {
    int direction = 1, NE = 0, SE = 1, SW = 2, NW = 3;//
    int x = 0;
    int y = 0;
    int a = xmax - 300, b = ymax - 200;//定义矩形方块位置
    int ballsize = 30;
    static int xmax = 600;
    static int ymax = 800;

    //private static TextField tf;

    private void moveBall() {
        switch (direction) {
            case 0 :
                x = x + 1;
                y = y - 1;
                if (y <= 0)
                    direction = SE;//1
                if (x >= xmax - ballsize - 22)
                    direction = NW;//3
                break;

            case 1 :
                x = x + 1;
                y = y + 1;
                if (y >= ymax - ballsize - 55)
                    direction = NE;//0
                if (x >= xmax - ballsize - 22)
                    direction = SW;//2
                break;

            case 2 :
                x = x - 1;
                y = y + 1;
                if (y >= ymax - ballsize - 55)
                    direction = NW;//3
                if (x <= 0)
                    direction = SE;//1
                break;

            case 3 :
                x = x - 1;
                y = y - 1;
                if (y <= 0)
                    direction = SW;//2
                if (x <= 0 - ballsize)
                    direction = NE;//0
                break;
        }

    }

    public void moveRect() {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventPostProcessor(new KeyEventPostProcessor() {
            @Override
            public boolean postProcessKeyEvent(KeyEvent e) {
                if (e.getID() == KeyEvent.KEY_PRESSED) {
                    if (e.getKeyCode() == KeyEvent.VK_UP) {
                        System.out.println("\n Go Up");
                        b = b - 5;
                        /*********注意这里***********/
                    }
                    else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                        System.out.println("\n Go Down");
                        b = b + 5;
                        /*********注意这里***********/
                    }
                    else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                        System.out.println("\n Go Left");
                        a = a - 5;
                        /*********注意这里***********/
                    }
                    else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                        System.out.println("\n Go Right");
                        a = a + 5;
                        System.out.println(a);
                        /*********注意这里***********/
                    }
                    //else
                    // System.out.println(e.getKeyChar());
                    // repaint();/*********注意这里***********/
                }
                return false;
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.BLUE);
        g.fillOval(x, y, 30, 30);
        g.fillRect(a, b, 100, 10);//绘制一个长100宽10的黑色矩形
    }

    //public interface ActionListener extends MouseMotionListener
    //{
    //public void mouseMoved(MouseEvent m)
    //{

    //}
    //}

    public static void main(String[] args) throws InterruptedException {

        JFrame frame = new JFrame("Sample Frame");
        Game game = new Game();
        game.moveRect();
        frame.add(game);
        frame.setSize(600, 800);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //tf=new TextField(30);
        //frame.add("North",tf);

        while (true) {
            game.moveBall();
            game.repaint();
            Thread.sleep(3);
        }
    }
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式