Test Foro de elhacker.net SMF 2.1

Programación => Programación General => Java => Mensaje iniciado por: mapers en 27 Abril 2014, 08:05 AM

Título: KeyListener Jpanel
Publicado por: mapers en 27 Abril 2014, 08:05 AM
Buen señores no se porque no captura el evento del teclado tengo mi codigo asi :

Código (java) [Seleccionar]

    private JPanel createToolbar() {
        final JPanel panel = new JPanel();
        panel.setBackground(Color.GREEN);
        panel.setLayout(new FlowLayout());
        panel.addKeyListener(new KeyListener() {

         @Override
         public void keyTyped(KeyEvent e) {
         System.out.println("Pressed " + e.getKeyChar());
         }

         @Override
         public void keyReleased(KeyEvent e) {
         System.out.println("Pressed " + e.getKeyChar());
         }

         @Override
         public void keyPressed(KeyEvent e) {
         // startCapture();
         System.out.println("Pressed " + e.getKeyChar());
         }
         });

         panel.setFocusable(true);
         panel.requestFocusInWindow();
         
        //KeyListener listener = new MyKeyListener();
        //panel.addKeyListener(listener);
        //panel.setFocusable(true);

        // panel.KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this)
        //KeyboardExampl keyboardExample = new KeyboardExample();
        //panel.add(keyboardExample);
     //   capturing = false;
    //    btnStartStop = new Button("Compartir Recorte");

      //  btnStartStop.addActionListener(new ActionListener() {
        //    @Override
         //   public void actionPerformed(ActionEvent e) {
                // if (capturing) {
                // capturing = false;
                // btnStartStop.setLabel("Start Capture");
                // stopCapture();
                // } else {
                // capturing = true;
                // btnStartStop.setLabel("Stop Capture");
           //     System.out.println("Capturando ");
            //    startCapture();
                // }
           // }
       // });
       // Detener_frame = new Button("Detener Recorte");
       // Detener_frame.addActionListener(new ActionListener() {
         //   @Override
          //  public void actionPerformed(ActionEvent e) {
            //    startCapture();
             //   stopCapture();
           // }
       // });
        //btnStartStop.setFocusable(true);
       // panel.add(btnStartStop);
       // panel.add(Detener_frame);

        panel.setFocusable(true);
        //  panel.setVisible(false);
        return panel;
    }
Título: Re: KeyListener Jpanel
Publicado por: ThinkByYourself en 28 Abril 2014, 01:26 AM
No estoy seguro, pero como nota, a lo mejor es por lo de "final" en el JPanel, que no permite añadir más cambios en panel, aunque no lo sé, creo que si fuera así ya debería indicarte error antes el IDE. Ni idea, sólo como nota...