Foros del Web » Programación para mayores de 30 ;) » Java »

No responde el Array JButton con los KeyStrokes

Estas en el tema de No responde el Array JButton con los KeyStrokes en el foro de Java en Foros del Web. Hola gente tengo el siguiente problema con mi programa no me jala el array de Jbutton con los KeyStroke y InputMap no se que estoy ...
  #1 (permalink)  
Antiguo 20/05/2008, 12:28
 
Fecha de Ingreso: diciembre-2006
Mensajes: 127
Antigüedad: 17 años, 4 meses
Puntos: 1
No responde el Array JButton con los KeyStrokes

Hola gente tengo el siguiente problema con mi programa no me jala el array de Jbutton con los KeyStroke y InputMap no se que estoy haciendo mal, con los demas botones si lo hace alguien me podria corregir mi error o sugerir...gracias



Código:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

class calc extends JFrame implements ActionListener
{

	public JButton arr[]=new JButton[10];
JButton boton;
Action actionListener;

  public calc()
  {
   super( "OK" );


InputMap map=new InputMap();
InputMap inputMap=new InputMap();


   Container content=this.getContentPane();
   JPanel pan=new JPanel();
   JPanel pan1=new JPanel();
   JPanel pan2=new JPanel();
   JPanel pan3=new JPanel();
   JTextField text=new JTextField();

   content.setBackground(Color.lightGray);
   content.setLayout(null);


   pan.setLayout(null);
   pan.setBackground(Color.lightGray);
   pan.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
   pan.setBounds(new Rectangle(20,30,180,60));
   text.setBounds(new Rectangle(30,20,130,30));
   pan.add(text);

   pan1.setBorder(BorderFactory.createLoweredBevelBorder());
   pan1.setBounds(new Rectangle(20,100,120,60));


   pan2.setLayout(new GridLayout(5,3));
   pan2.setBounds(new Rectangle(20,170,280,120));
   pan2.setBackground(Color.lightGray);
   pan2.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));


   //pan3.setLayout(new )


   for (int i=0;i<arr.length;i++)
  {

	  /*
	  arr[i]=new JButton(Integer.toString(i));
    pan2.add(arr[i]);*/

     JButton boton;
	      boton = new JButton(String.valueOf(i));


	      	      
pan2.add( boton );

      arr[i] = boton;


      KeyStroke enter = KeyStroke.getKeyStroke(i+48,0);

      System.out.println(enter);


inputMap=arr[i].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

      inputMap.put(enter, actionListener);
      ActionMap actionMap = boton.getActionMap();

      actionMap.put("action", actionListener);


      arr[i].addActionListener(this);
      arr[i].getActionMap().put("action",actionListener);


   }

   text.setEnabled(false);

JButton botadd=new JButton("+");
JButton botmin=new JButton("-");
JButton botpro=new JButton("x");
JButton botdiv=new JButton("/");
JButton botequ=new JButton("=");
JButton botp=new JButton(".");
JButton botcl=new JButton("C");

pan2.add(botadd);
pan2.add(botmin);
pan2.add(botpro);
pan2.add(botdiv);
pan2.add(botequ);
pan2.add(botp);
pan2.add(botcl);


botadd.addActionListener(this);
botmin.addActionListener(this);
botpro.addActionListener(this);
botdiv.addActionListener(this);
botequ.addActionListener(this);
botp.addActionListener(this);
botcl.addActionListener(this);


   content.add(pan,null);
   content.add(pan1,null);
   content.add(pan2,null);



  setVisible(true);
   setSize(400,400);
   setDefaultCloseOperation(EXIT_ON_CLOSE);

   setResizable(false);





Action actionListener = new AbstractAction() {
      public void actionPerformed(ActionEvent actionEvent) {
        JButton source = (JButton) actionEvent.getSource();
        System.out.println("Act BUTON: " + source.getText());
      }
    };



   KeyStroke stroke=KeyStroke.getKeyStroke(KeyEvent.VK_C,0);

   map=botcl.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
   map.put(stroke,"action");

   ActionMap actm=botcl.getActionMap();
   actm.put("action",actionListener);


KeyStroke strok=KeyStroke.getKeyStroke(KeyEvent.VK_ADD,0);

   map=botadd.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
   map.put(strok,"action");

   ActionMap actb=botadd.getActionMap();
   actb.put("action",actionListener);




KeyStroke s=KeyStroke.getKeyStroke(106,0);

   map=botpro.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
   map.put(s,"action");

   ActionMap bn=botpro.getActionMap();
   bn.put("action",actionListener);

  }
  #2 (permalink)  
Antiguo 28/05/2008, 17:29
 
Fecha de Ingreso: diciembre-2006
Mensajes: 127
Antigüedad: 17 años, 4 meses
Puntos: 1
Sonrisa Respuesta: No responde el Array JButton con los KeyStrokes

La unica forma que lo hice hacer funcionar es de esta manera no haciendolo dentro del ciclo for

Código PHP:

KeyStroke cero 
KeyStroke.getKeyStroke('0');

map=arr[0].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

map.put(cero,"action");

ActionMap actionmap=arr[0].getActionMap();
actionmap.put("action",actionListener);




KeyStroke one KeyStroke.getKeyStroke('1');

map=arr[1].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

map.put(one,"action");

ActionMap actuno=arr[1].getActionMap();
actuno.put("action",actionListener);




KeyStroke twoKeyStroke.getKeyStroke('2');

map=arr[2].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

map.put(two,"action");

ActionMap acttwo=arr[2].getActionMap();
acttwo.put("action",actionListener);



KeyStroke threeKeyStroke.getKeyStroke('3');

map=arr[3].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

map.put(three,"action");

ActionMap actthree=arr[3].getActionMap();
actthree.put("action",actionListener);




KeyStroke fourKeyStroke.getKeyStroke('4');

map=arr[4].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

map.put(four,"action");

ActionMap actfour=arr[4].getActionMap();
actfour.put("action",actionListener);




KeyStroke fiveKeyStroke.getKeyStroke('5');

map=arr[5].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

map.put(five,"action");

ActionMap actfive=arr[5].getActionMap();
actfive.put("action",actionListener);




KeyStroke sixKeyStroke.getKeyStroke('6'); //KeyEvent.VK_NUMPAD6,0

map=arr[6].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

map.put(six,"action");

ActionMap actsix=arr[6].getActionMap();
actsix.put("action",actionListener);



KeyStroke sevenKeyStroke.getKeyStroke('7');

map=arr[7].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

map.put(seven,"action");

ActionMap actseven=arr[7].getActionMap();
actseven.put("action",actionListener);




KeyStroke eightKeyStroke.getKeyStroke('8');

map=arr[8].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

map.put(eight,"action");

ActionMap acteight=arr[8].getActionMap();
acteight.put("action",actionListener);




KeyStroke nineKeyStroke.getKeyStroke('9');

map=arr[9].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

map.put(nine,"action");

ActionMap actnine=arr[9].getActionMap();
actnine.put("action",actionListener); 
La razon es :

By sharing an InputMap or ActionMap, any change to the shared InputMap or ActionMap will affect all components sharing the InputMap or ActionMap. WHEN_FOCUSED and WHEN_ANCESTOR_OF_FOCUSED_COMPONENT types of InputMaps can be shared. WHEN_IN_FOCUSED_WINDOW InputMaps cannot be shared.


Espero ha alguien le sirva

Última edición por abulon; 28/05/2008 a las 17:32 Razón: comentario
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:21.