码迷,mamicode.com
首页 > 编程语言 > 详细

java界面编程(4) ------ Swing事件模型

时间:2014-11-16 00:36:46      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:java界面编程   swing事件模型   

本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020

  在Swing的事件模型中,组件可以发起(触发)一个事件。每种事件的类型由不同的类表示,当事件被触发时,它将被一个或多个“监听器”接收,监听器负责处理事件。所以,事件发生的地方可以与事件处理的地方分离开。既然是以这种方式使用Swing组件,那么就只需要编写组件收到事件时将被调用的代码。所谓事件监听器,就是一个“实现特定类型的监听器接口”类对象。

  下面,就以一个查找Swing组件里的addListener的例子来演示Swing事件模型,完整代码如下:

public class ShowAddListeners extends JFrame {

	private JTextField name = new JTextField(18);
	private JButton search = new JButton("查询");
	private JTextArea results = new JTextArea(30,50);
	//定义正则表达式匹配addListener
	private static Pattern addListener = Pattern.compile("(add\\w+?Listener\\(.*?\\))");
	private static Pattern qualifier = Pattern.compile("\\w+\\.");
	
	public ShowAddListeners(){
		setVisible(true);
		setSize(600, 400);
		JPanel top = new JPanel();
		top.add(new JLabel("Swing Class name(press Enter):"));
		top.add(name);
		top.add(search);
		add(BorderLayout.NORTH,top);
		add(new JScrollPane(results));
		//设置不可编辑
		//results.setEditable(false);
		search.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				// TODO Auto-generated method stub
				//去掉关键字的前后空格
				String nm = name.getText().trim();
				//如果去掉关键字后长度为为,则显示没有匹配成功的
				if(nm.length() == 0){
					results.setText("no match!");
					return;
				}
				Class<?> kind;
				try {
					kind = Class.forName("javax.swing."+nm);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					results.setText("no match!");
					return;
				}
				
				Method[] methods = kind.getMethods();
				results.setText("");
				for(Method m : methods){
					
					Matcher matcher = addListener.matcher(m.toString());
					//如果找到了匹配的方法,则输出
					if(matcher.find()){
						results.append(qualifier.matcher(matcher.group(1)).replaceAll("")+"\n");
					}
				}
			}
		});
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new ShowAddListeners();
	}

}
  效果:

bubuko.com,布布扣bubuko.com,布布扣

      这里面主要用到了正则表达式用于匹配相应的监听器,这里只是用到了一个监听器接收,上面也提到了,组件也可以绑定多个监听器。

  下面就实现JButton的几个重要的事件监听器,分别是FocusListener、KeyListener、MouseListener和MouseMotionListener
,给按钮绑定这些监听器,当对应的事件发生时,触发相应的事件,从而执行对应的操作,完整代码如下:

public class TrackEvent extends JFrame {

	
	private HashMap<String,JTextField> map = new HashMap<String,JTextField>();
	private String[] event = {
			"focusGained",
			"focusLost",
			"keyPressed",
			"keyReleased",
			"keyTyped",
			"mouseClicked",
			"mouseEntered",
			"mouseExited",
			"mousePressed",
			"mouseReleased",
			"mouseDragged",
			"mouseMoved"
	};
	private MyButton b1 = new MyButton(Color.BLUE,"test1");
	private MyButton b2 = new MyButton(Color.RED,"test2");
	
	class MyButton extends JButton{
		private void report(String field,String msg){
			map.get(field).setText(msg);
		}
		private FocusListener fl = new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				report("focusLost",e.paramString());
			}
			
			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				report("focusGained",e.paramString());
			}
		};
		
		private KeyListener kl = new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				report("keyTyped",e.paramString());
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				report("keyReleased",e.paramString());
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				report("keyPressed",e.paramString());
			}
		};
		
		private MouseListener ml = new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				report("mouseReleased",e.paramString());
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				report("mousePressed",e.paramString());
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				report("mouseExited",e.paramString());
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				report("mouseEntered",e.paramString());
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				report("mouseClicked",e.paramString());
			}
		};
		
		private MouseMotionListener mml = new MouseMotionListener() {
			
			@Override
			public void mouseMoved(MouseEvent e) {
				// TODO Auto-generated method stub
				report("mouseMoved",e.paramString());
			}
			
			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				report("mouseDragged",e.paramString());
			}
		};
		
		public MyButton(Color color,String label) {
			super(label);
			setBackground(color);
			addFocusListener(fl);
			addKeyListener(kl);
			addMouseListener(ml);
			addMouseMotionListener(mml);
			
		}
	}
	
	public TrackEvent() {
		// TODO Auto-generated constructor stub
		setLayout(new GridLayout(event.length + 1,2));
		for(String evt : event){
			JTextField tf = new JTextField();
			tf.setEditable(false);
			add(new JLabel(evt,JLabel.RIGHT	));
			add(tf);
			setSize(700,500);
			setVisible(true);
			map.put(evt, tf);
		}
		add(b1);
		add(b2);
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				new TrackEvent();
			}
		});
	}

}
  执行结果:

bubuko.com,布布扣

  这样,鼠标单击按钮,拖动按钮,移动按钮等事件发生时,就会触发相应的监听器,从而将事件的动作以字符串的形式显示在JTextField中,即组件跟踪多个事件。





java界面编程(4) ------ Swing事件模型

标签:java界面编程   swing事件模型   

原文地址:http://blog.csdn.net/jesson20121020/article/details/41123217

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!