发布的密钥上的可编辑JcomboBox Action事件无法正常工作

时间:2014-06-16 19:37:12

标签: java swing actionlistener jcombobox

我有一个名为c的JComboBox,我保持可编辑状态,只要在该ComboBox中按下/释放任何键,我想调用名为search()的函数?我尝试了这个,但这既没有调用我的函数,也没有在条件为真时写入if()的工作。

请告诉我如何通过可编辑的组合框从键盘按键来触发动作,我使用netbeans。

private void cKeyReleased(java.awt.event.KeyEvent evt) {                              

     if(evt.getKeyCode()==KeyEvent.VK_A )
    {
        //DOES NOT EVEN ENTER THIS METHOD WHEN A IS PRESSED.
        try 
         {
             search();      // MY CALL TO FUNCTION
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }

     if(evt.getKeyCode()==KeyEvent.VK_B )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_C )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_D )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_E )
    {
         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_F )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_G )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_H )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_I )
    {
        try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_J )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_K )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_L )
    {
      try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_M )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_N )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }




     if(evt.getKeyCode()==KeyEvent.VK_O )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_P )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_Q )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_R )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_S )
    {

         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_T )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }



     if(evt.getKeyCode()==KeyEvent.VK_U )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_V )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }

    if(evt.getKeyCode()==KeyEvent.VK_W )
    {

         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    } 

    if(evt.getKeyCode()==KeyEvent.VK_X )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }

     if(evt.getKeyCode()==KeyEvent.VK_Y )
    {

         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }

   if(evt.getKeyCode()==KeyEvent.VK_Z )
    {

         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }  

     if(evt.getKeyCode()==KeyEvent.VK_1 )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_2 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }


     if(evt.getKeyCode()==KeyEvent.VK_3 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_4 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_5 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }


     if(evt.getKeyCode()==KeyEvent.VK_6 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }


     if(evt.getKeyCode()==KeyEvent.VK_7 )
    {
          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }
    }


     if(evt.getKeyCode()==KeyEvent.VK_8 )
    {

          try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }


     if(evt.getKeyCode()==KeyEvent.VK_9 )
    {
         try 
         {
             search();
         } catch (     ClassNotFoundException | SQLException ex) 
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }


    }


     if(evt.getKeyCode()==KeyEvent.VK_0 )
    {
         try 
         {
             search();
         }
         catch (ClassNotFoundException | SQLException ex)
         {
             Logger.getLogger(gui.class.getName()).log(Level.SEVERE, null, ex);
         }

    }


}       

1 个答案:

答案 0 :(得分:0)

我使用了这个,即使是关键的听众也没有工作,我不需要文件监听器@camickr相信我,我想要的是如何使用文件监听器,我仍然愿意使用它!我重复我是java的新手,我正在开发一个项目,通过阅读问题和答案塑造它们以满足我的要求! 当我说我做了我的研究时,我的意思是我甚至在发布我的问题之前就做过了。 再一个如何使用文档监听器的小片段将会有很大的帮助,因为我使用netbeans我只想在我的可编辑组合框中输入密钥时进行函数调用。

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ComboBoxEditor;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.text.DateFormatter;


public class Adhoc_JComboBox extends JFrame implements KeyListener{

private static JComboBox combobox; 
String a1,a2,a3;
public static void main(String[] args) {
new Adhoc_JComboBox();
}

private Adhoc_JComboBox(){
String array[]= new String[3];

a1=new String("a1");
a2=new String("a2");
a3=new String("a3");
array[0]= a1;
array[1]= a2;
array[2]= a3;
combobox = new JComboBox(array);
combobox.setEditable(true);
JTextField editor = (JTextField) combobox.getEditor().getEditorComponent();
editor.addKeyListener(this);
setLayout(new FlowLayout());
add(combobox);
pack();
setVisible(true);
}

public void keyTyped(KeyEvent arg0) {
System.out.println("Key Typed " +arg0.getKeyCode());
}

public void keyPressed(KeyEvent arg0) {
System.out.println("Key Pressed " +arg0.getKeyCode());
} 
public void keyReleased(KeyEvent arg0) {
System.out.println("Key Released " +arg0.getKeyCode());}}