import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Edible extends JFrame{

public Edible(){
KeyBoardPanel keyBoardPanel1 = new KeyBoardPanel();
KeyBoardPanel keyBoardPanel2 = new KeyBoardPanel();
setLayout(new GridLayout(2,1));
add(keyBoardPanel1);
add(keyBoardPanel2);
}
public static void main(String[] args){
Edible frame = new Edible();
frame.setTitle("Test Winedows");
frame.setLocation(100, 100);
frame.setSize(300, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class KeyBoardPanel extends JPanel{

private int x = 100;
private int y = 100;
private char keyChar = 'A';
public KeyBoardPanel(){
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_DOWN:y+= 10;break;
case KeyEvent.VK_UP:y-= 10;break;
case KeyEvent.VK_LEFT:x-= 10;break;
case KeyEvent.VK_RIGHT:x+= 10;break;
default:keyChar = e.getKeyChar();
}
repaint();
}
});

addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
//setFocusable(true);
}
});
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setFont(new Font("TimesRoman",Font.PLAIN,24));
g.drawString(String.valueOf(keyChar), x, y);
}
}
}简要说明一下我的问题,我画了两个面板,当我键盘上按下一个键时,他会在面板上显示字符,而且可以根据我按上下左右移动(这些都不关键),主要问题是:我需要当我的鼠标在其中一个面板上点击后,就使那一块面板接受KeyEvent,我注释的代码是错误的,请问各位,怎样修改才可以,谢谢了

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Edible extends JFrame{
        
        public Edible(){
            KeyBoardPanel keyBoardPanel1 = new KeyBoardPanel();
            KeyBoardPanel keyBoardPanel2 = new KeyBoardPanel();
            setLayout(new GridLayout(2,1));
            add(keyBoardPanel1);
            add(keyBoardPanel2);
        }
        public static void main(String[] args){
            Edible frame = new Edible();
            frame.setTitle("Test Winedows");
            frame.setLocation(100, 100);
            frame.setSize(300, 600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
        class KeyBoardPanel extends JPanel{
            
            private int x = 100;
            private int y = 100;
            private char keyChar = 'A';
            public KeyBoardPanel(){
                addKeyListener(new KeyAdapter(){
                    public void keyPressed(KeyEvent e){
                        switch(e.getKeyCode()){
                            case KeyEvent.VK_DOWN:y+= 10;break;
                            case KeyEvent.VK_UP:y-= 10;break;
                            case KeyEvent.VK_LEFT:x-= 10;break;
                            case KeyEvent.VK_RIGHT:x+= 10;break;
                            default:keyChar = e.getKeyChar();
                        }
                        repaint();
                    }
                });
                
                addMouseListener(new MouseAdapter(){
                    public void mouseClicked(MouseEvent e){
                       requestFocus(true);
                    }
                });
            }
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.setFont(new Font("TimesRoman",Font.PLAIN,24));
                g.drawString(String.valueOf(keyChar), x, y);
            }
        }
    }
    就一步