Ver Mensaje Individual
  #1 (permalink)  
Antiguo 20/05/2008, 12:28
abulon
 
Fecha de Ingreso: diciembre-2006
Mensajes: 127
Antigüedad: 18 años, 1 mes
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);

  }