posts - 2, comments - 27, trackbacks - 0, articles - 60
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

在JTextField中实现对Ctrl+Enter的响应

Posted on 2008-12-12 11:41 ZhouFeng 阅读(397) 评论(0)  编辑  收藏 所属分类: 转载JAVA
这段代码可以实现
  public   class   Test   {  
          
private   boolean   isPressed   =   false;  
          
public   void   show(){  
                  JFrame   frame   
=   new   JFrame();  
   
                  MyTableModel   tableModel
=new   MyTableModel();  
                  JTable   table
=new   JTable(tableModel);  
                  JTextArea   text   
=   new   JTextArea(3,20);  
                  text.addKeyListener(
new   KeyAdapter(){  
                          
public   void   keyPressed(KeyEvent   e)   {  
                                  
if(KeyEvent.VK_CONTROL   ==   e.getKeyCode()){  
                                          isPressed   
=   true;  
                                  }  
                                  
if(e.getKeyCode()   ==   KeyEvent.VK_ENTER   &&   isPressed){  
                                            JOptionPane.showMessageDialog(
null,"Ctrl+Enter   was   pressed.");  
                                  }  
                          }  
                          
public   void   keyReleased(KeyEvent   e){  
                                  System.out.println(e.getKeyCode());  
                                  
if(KeyEvent.VK_CONTROL   ==   e.getKeyCode()   ||   KeyEvent.VK_ENTER   ==   e.getKeyCode()){  
                                          isPressed   
=   false;  
                                  }  
                          }  
                  });  
                  frame.getContentPane().add(
new   JScrollPane(text));  
                  frame.setSize(
300,400);  
                  frame.show();  
          }  
          
public   static   void   main(String[]   args){  
                  Test   test   
=   new   Test();  
                  test.show();  
          }  
  }

只有注册用户登录后才能发表评论。


网站导航: