我猜是Canvas没有获得焦点^_^。

解决方案 »

  1.   

    谢谢楼上的mu_x(阿木) 。我猜也是。只是要如何让Canvas获得焦点?请再帮忙。
      

  2.   

    一个Frame类add(new Canvas()).
    ……………………………………………………………………
    Canvas c = new Canvas();
    frame.getContentPane().add(c);
    c.requestFocus(true);
      

  3.   

    感谢thomas_20(执子之手,与子偕老) 的解答,遗憾的是,问题依然没有解决。我想可能是我的代码中有一些问题吧因此帖出来,望指点。。(Core JAVA II线程一章的代码)
    /**                                                                                                    
     * The frame with canvas and buttons.                                                                     
     */
    class BounceFrame extends JFrame {    /**                                                                                                    
         * Constructs the frame with the canvas for showing the                                                   
         * bouncing ball and Start and Close buttons                                                              
         */
        public BounceFrame() {
            setSize(WIDTH, HEIGHT);
            setTitle("BounceThread");
            setLocation(112, 84);        Container contentPane = this.getContentPane();
            canvas = new BallCanvas();
            contentPane.add(canvas, BorderLayout.CENTER);
    canvas.requestFocus();

            JPanel buttonPanel = new JPanel();
            addButton(buttonPanel, "Start", new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    addBall();
                }
            });
            addButton(buttonPanel, "Close", new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.exit(0);
                }
            });
            contentPane.add(buttonPanel, BorderLayout.SOUTH);
            
            this.setFocusable(true);
        }    /**                                                                                                    
        Adds a button to a container.                                                                          
        @param c the container                                                                                 
        @param title the button title                                                                          
        @param listener the action listener for the button                                                     
        */
        public void addButton(Container c, String title, ActionListener listener) {
            JButton button = new JButton(title);
            c.add(button);
            button.addActionListener(listener);
        }    /**                                                                                                    
        Adds a bouncing ball to the canvas and starts a thread                                                 
        to make it bounce                                                                                      
        */
        public void addBall() {
            Ball b = new Ball(canvas);
            canvas.add(b);
            BallThread thread = new BallThread(b);
            thread.start();
        }    private BallCanvas canvas;
        public static final int WIDTH = 800;
        public static final int HEIGHT = 600;}
      

  4.   

    线程类---------------------------
    /**                                                                                                    
    A thread that animates a bouncing ball.                                                                
    */
    class BallThread extends Thread {    /**                                                                                                    
        Constructs the thread.                                                                                 
        @aBall the ball to bounce                                                                              
        */
        public BallThread(Ball aBall) {
            b = aBall;
        }    public void run() {
            try {
                while (true) {
                    b.move();
                    sleep(5);
                }
            } catch (InterruptedException exception) {}
        }    private Ball b;
    }
      

  5.   

    画布类------------------
    /**                                                                                            
     *The canvas that draws the balls.                                                               
     */
    class BallCanvas extends JPanel {    public BallCanvas() {
         this.setBackground(new Color(255,255,255));
            addKeyListener(new KeyListener() {            /* (非 Javadoc)
                 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
                 */
                public void keyPressed(KeyEvent e) {
                    System.out.println("the keyPressed method is invoked!");
                }            /* (非 Javadoc)
                 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
                 */
                public void keyReleased(KeyEvent e) {}            /* (非 Javadoc)
                 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
                 */
                public void keyTyped(KeyEvent e) {}        });
        }    /**                                                                                            
        Add a ball to the canvas.                                                                      
        @param b the ball to add                                                                       
        */
        public void add(Ball b) {
            balls.add(b);
        }    public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            for (int i = 0; i < balls.size(); i++) {
                Ball b = (Ball) balls.get(i);
                b.draw(g2);
            }
        }    private ArrayList balls = new ArrayList();
        private static int width = 800;
        private static int height = 600;}
      

  6.   

    运行的时候焦点在JButton上了吧(接着猜^_^),不用JButton试一下。
      

  7.   

    刚才边帖了三帖,不让继续了。还有一个Ball类。
    /**                                                                                            
     * A ball that moves and bounces off the edges of a                                               
     * component                                                                                      
     */
    class Ball {    /**                                                                                            
         * Constructs a ball in the upper left corner                                                     
         * @c the component in which the ball bounces                                                     
         */
        public Ball(Component c) {
            canvas = c;
        }    /**                                                                                            
         * Draws the ball at its current position                                                         
         * @param g2 the graphics context                                                                 
         */
        public void draw(Graphics2D g2) {
            g2.fill(new Ellipse2D.Double(x, y, XSIZE, YSIZE));
        }    /**                                                                                            
         * Moves the ball to the next position, reversing direction                                       
         * if it hits one of the edges                                                                    
         */
        public void move() {
            canvas.repaint();
        }    /* 画布 */
        private Component canvas;    /* ball的宽度 */
        private static final int XSIZE = 10;    /* ball的高度 */
        private static final int YSIZE = 10;    /* ball的初始位置的X坐标 */
        private int x = 0;    /* ball的初始位置的Y坐标 */
        private int y = 0;    /* ball每次移动的X坐标长度 */
        private int dx = 10;    /* ball每次移动的Y坐标长度 */
        private int dy = 10;}
      

  8.   

    没错。焦点就是在JButton上。现在的问题是,我不知道他是如何获得焦点的。也不知道该如何让别的组件获得焦点。
      

  9.   

    那我的问题应该如何解决?如何让shit Canvas获得焦点。
      

  10.   

    把JButton换成JLabel试试?它是没有焦点的。这个问题没考虑过,纯属瞎猜^_^
      

  11.   

    其实我以前也碰到过类似的问题,后来实在解决不了,就在每个能获得焦点的控件里加了KeyListener
    方法比较笨,后来也就没怎么研究过!希望楼下的能有好的解决办法!
      

  12.   

    OOOOOOOOOOK。问题解决。问题在于,原代码中给canvas设置焦点后。会执行addButton(),添加Button,结果将焦点又转移到了Button上。
    因此需要在button之后给canvas设置焦点。我将Frame中的canvas.requestFocus(true)挪到了addBall()方法中。
    Frame代码如下:/**                                                                                                    
     * The frame with canvas and buttons.                                                                     
     */
    class BounceFrame extends JFrame {    /**                                                                                                    
         * Constructs the frame with the canvas for showing the                                                   
         * bouncing ball and Start and Close buttons                                                              
         */
        public BounceFrame() {
            setSize(WIDTH, HEIGHT);
            setTitle("BounceThread");
            setLocation(112, 84);        Container contentPane = this.getContentPane();
            canvas = new BallCanvas();
            contentPane.add(canvas, BorderLayout.CENTER);

            JPanel buttonPanel = new JPanel();
            addButton(buttonPanel, "Start", new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    addBall();
                }
            });
            addButton(buttonPanel, "Close", new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.exit(0);
                }
            });
            contentPane.add(buttonPanel, BorderLayout.SOUTH);
            
        }    /**                                                                                                    
        Adds a button to a container.                                                                          
        @param c the container                                                                                 
        @param title the button title                                                                          
        @param listener the action listener for the button                                                     
        */
        public void addButton(Container c, String title, ActionListener listener) {
            JButton button = new JButton(title);
            c.add(button);
            button.addActionListener(listener);
        }    /**                                                                                                    
        Adds a bouncing ball to the canvas and starts a thread                                                 
        to make it bounce                                                                                      
        */
        public void addBall() {
            Ball b = new Ball(canvas);
            canvas.add(b);
            canvas.requestFocus(true);
            BallThread thread = new BallThread(b);
            thread.start();
        }    private BallCanvas canvas;
        public static final int WIDTH = 800;
        public static final int HEIGHT = 600;}
    感谢各位的积极参与并给了我启示,我是新手分不多,但见者有份,以示鼓励。谢谢。