java核心技术简单例子

import java.awt.BorderLayout;

import javax.swing.JFrame;

public class EventSourceTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  EventSourceFrame frame = new EventSourceFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
 }

 
}

 

 

 

 

 

 

 

 

 

 

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;

class EventSourceFrame extends JFrame {
  public static final int DEFAULT_WITH = 400;
  public static final int DEFAULT_HEIGHT = 200;

  public EventSourceFrame() {
   setTitle("EventSourceTest");
   setSize(DEFAULT_WITH, DEFAULT_HEIGHT);

   final PaintCountPanel panel = new PaintCountPanel();
   add(panel);
   
   panel.addPropertyChangeListener(new
     PropertyChangeListener()
   {
   public void propertyChange(PropertyChangeEvent event){
    setTitle("java核心技术简单例子"+event.getNewValue());
   }
   });

  }
 }

 

 

 

 

 

 

 

 

 

 

 

 

import java.awt.Graphics;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.EventListener;

import javax.swing.JPanel;

class PaintCountPanel extends JPanel {
  public void paintComponent(Graphics g) {
   int oldPaintCount = paintCount;
   paintCount++;
   firePropertyChangeEvent(new PropertyChangeEvent(this, "paintCount",oldPaintCount,
     paintCount));
  }
 public void addPropertyChangeListener(PropertyChangeListener listener) {
  listenerList.add(PropertyChangeListener.class, listener);
 }

 public void removePropertyChangeListener(PropertyChangeListener listener) {
  listenerList.remove(PropertyChangeListener.class, listener);
 }

 public void firePropertyChangeEvent(PropertyChangeEvent event) {
  EventListener[] listeners = listenerList
    .getListeners(PropertyChangeListener.class);
  for (EventListener l : listeners)
   ((PropertyChangeListener)l).propertyChange(event);
 }

 public int getPaintCoutn() {
  return paintCount;
 }

 private int paintCount;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值