监听JScrollPane按PageUp,PageDown事件

package com.cyl;  import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.event.KeyAdapter;  import javax.swing.*; /**  * 监听事件测试,监听JScrollPane按PageUp,PageDown事件  * @author cyl  *  */ public class KeyListenerTest { 	public static void main(String[] args) { 		JPanel p = new JPanel(); 		p.setBackground(Color.white); 		p.addKeyListener(new KeyAdapter() { 			public void keyPressed(java.awt.event.KeyEvent e) { 				System.out.println(e); 				//按下PageUp或者PageDown 				if (e.getKeyCode() == java.awt.event.KeyEvent.VK_PAGE_UP || e.getKeyCode() == java.awt.event.KeyEvent.VK_PAGE_DOWN) { 					System.out.println("roll!!"); 				} 				/* Consumes this event so that it will not be processed in the default manner by the source which originated it. */ 				//e.consume(); 			} 		}); 		// 此句是监听KeyListener的关键,如果没有此句,则监听不到Key事件的发生 		p.setFocusable(true); 		// --end 		p.setPreferredSize(new Dimension(500, 500)); 		JScrollPane sp = new JScrollPane(p);  		JFrame f = new JFrame(); 		f.getContentPane().add(sp, BorderLayout.CENTER); 		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 		f.setSize(300, 300); 		f.setLocationRelativeTo(null); 		f.setVisible(true); 	} }
 

 

你可能感兴趣的:(java,swing,F#,UP)