2013年10月29日星期二

Regardless of why I wrote a listener to use it ? Online requirements.

public class Test extends JPanel {
private JFrame frame;
private Container contentPane;
private JButton button;
public D () {
frame = new JFrame ();
frame.setSize (300,300);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
contentPane = frame.getContentPane ();
frame.setVisible (true);
contentPane.setLayout (null);
button = new JButton (" Button" ) ;
button.setBounds (50, 50, 80, 30);
contentPane.add (button);
frame.addKeyListener (new KeyAdapter ()
{
public void keyPressed (KeyEvent e)
{
System.out.println (" realized listening" ) ;
if (e.getKeyCode () == e.VK_UP) {
System.out.println ("up");
}

if (e.getKeyCode () == e.VK_DOWN) {
System.out.println ("down");
}
}
});
}

public static void main (String [] args) {
new Test ();
}
}
Sometimes you can , sometimes on the listener than the keyboard. . I do not know why. . . Help. .

------ Solution ------------------------------------ --------
this is a focal point transfer. . . You lose the focus frame that caused
------ For reference only ------------------------------ ---------
can you give an example? Best to have the code to help me change
------ For reference only ------------------------------ ---------

I started when I committed such a mistake . . . You look at this code it. .
and you basically the same, adding the focus of the request just fine.

public class D extends JPanel{
    private static final long serialVersionUID = 453890560369815259L;
    private JFrame frame;
    private Container contentPane;
    private JButton button;
    public D(){
        frame=new JFrame();
        contentPane=frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);
        init();
    }
    private void init(){
        contentPane.setLayout(null);
        button=new JButton("按钮");
        button.setBounds(50, 50, 80, 30);
        contentPane.add(button);
        frame.addKeyListener(new KeyListener(){
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode=e.getKeyCode();
                if(keyCode==KeyEvent.VK_UP){
                    System.out.println("up");
                }else if(keyCode==KeyEvent.VK_DOWN){
                    System.out.println("down");
                }
            }
            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
            }
            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
            }
        });
        frame.addFocusListener(new FocusListener(){
            @Override
            public void focusGained(FocusEvent arg0) {
                // TODO Auto-generated method stub
            }
            @Override
            public void focusLost(FocusEvent arg0) {
                // TODO Auto-generated method stub
                frame.requestFocus();
            }           
        });
        frame.requestFocus();
    }
    public static void main(String[] args) {
        new D();
    }
}

------ For reference only ----------------------------------- ----

I started when I committed such a mistake . . . You look at this code it. .   
and you basically the same, adding the focus of the request just fine.   
  

public class D extends JPanel{
    private static final long serialVersionUID = 453890560369815259L;
    private JFrame frame;
    private Container contentPane;
    private JButton button;
    public D(){
        frame=new JFrame();
        contentPane=frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);
        init();
    }
    private void init(){
        contentPane.setLayout(null);
        button=new JButton("按钮");
        button.setBounds(50, 50, 80, 30);
        contentPane.add(button);
        frame.addKeyListener(new KeyListener(){
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode=e.getKeyCode();
                if(keyCode==KeyEvent.VK_UP){
                    System.out.println("up");
                }else if(keyCode==KeyEvent.VK_DOWN){
                    System.out.println("down");
                }
            }
            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
            }
            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
            }
        });
        frame.addFocusListener(new FocusListener(){
            @Override
            public void focusGained(FocusEvent arg0) {
                // TODO Auto-generated method stub
            }
            @Override
            public void focusLost(FocusEvent arg0) {
                // TODO Auto-generated method stub
                frame.requestFocus();
            }           
        });
        frame.requestFocus();
    }
    public static void main(String[] args) {
        new D();
    }
}
 
Thank you, have a question to ask you .
------ For reference only ---------------------- -----------------

I started when I committed such a mistake . . . You look at this code it. .     
and you basically the same, adding the focus of the request just fine.     
    

public class D extends JPanel{
    private static final long serialVersionUID = 453890560369815259L;
    private JFrame frame;
    private Container contentPane;
    private JButton button;
    public D(){
        frame=new JFrame();
        contentPane=frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);
        init();
    }
    private void init(){
        contentPane.setLayout(null);
        button=new JButton("按钮");
        button.setBounds(50, 50, 80, 30);
        contentPane.add(button);
        frame.addKeyListener(new KeyListener(){
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode=e.getKeyCode();
                if(keyCode==KeyEvent.VK_UP){
                    System.out.println("up");
                }else if(keyCode==KeyEvent.VK_DOWN){
                    System.out.println("down");
                }
            }
            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
            }
            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
            }
        });
        frame.addFocusListener(new FocusListener(){
            @Override
            public void focusGained(FocusEvent arg0) {
                // TODO Auto-generated method stub
            }
            @Override
            public void focusLost(FocusEvent arg0) {
                // TODO Auto-generated method stub
                frame.requestFocus();
            }           
        });
        frame.requestFocus();
    }
    public static void main(String[] args) {
        new D();
    }
}
         
Thank you, have a question to ask you .  
pro , if no problem statement posted it. . .

没有评论:

发表评论