国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

“Java中可復用事件處理的設計與實現”代碼

2019-11-18 12:01:25
字體:
來源:轉載
供稿:網友

  目前,面向對象是軟件系統建模的主流技術,使用面向對象技術建模的主要指標之一是可復用性。為了更好地解決軟件復用性和擴展性問題,設計模式得到了越來越多的關注與應用。
  
  結合command設計模式和java語言的反射技術,本文設計實現了一個可復用的事件處理框架。
  
  在面向對象的系統設計中,有些方面的可復用性經常被忽略了,用戶界面(User Interface, 下文簡稱UI)及其事件處理就是其中之一。一個完整的UI設計應該包括兩部分:UI及其相應的事件處理機制,沒有事件處理機制的UI是沒有用的,對于事件處理,也應該考慮可復用設計。雖然看上去有些希奇,但是這種設計是有實用價值的——提高了代碼的可復用性、健壯性和可維護性。
  
  command設計模式的主要設計思想是把對某一對象的請求封裝為一個對象,從而把發出命令的責任和執行任務的責任分開,委派給不同的對象,請求的一方不必知道接收請求一方的接口。這種引入第三方類的做法是設計模式所慣用的,引入的第三方類解耦了緊耦合對象。command設計模式中,第三方類解耦了調用操作的對象與知道如何實現該操作的對象,提高了軟件的可復用性。
  
  JDK 1.1及其以后的版本,引入了反射(reflection)技術。反射是Java中非常突出的動態特征,利用它可以加載一個運行時才得知名字的class,獲取其完整結構,這一切是通過反射API完成的。
  
  //UIDemo1
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class UIDemo1 implements ActionListener{
  PRivate JFrame frame;
  private JButton button;
  private JTextArea area;
  private int index = -1;
  private String [] params;
  
  private UIDemo1(String args[]){
  params = args;
  area = new JTextArea(5,25);
  button = new JButton("Click here!");
  button.addActionListener(this);
  
  frame = new JFrame(getClass().getName());
  frame.addWindowListener(new ReusableWindowAdapter());
  Container cont = frame.getContentPane();
  JScrollPane scrollPane = new JScrollPane(area);
  cont.add(scrollPane,BorderLayout.NORTH);
  cont.add(button,BorderLayout.SOUTH);
  frame.pack();
  frame.setVisible(true);
  }
  
  public void actionPerformed(ActionEvent ae){
  //provide equality check to see if source was the
  //button defined above.. useful only if we register
  //this ActionListener with multiple sources
  if(ae.getSource().equals(button)){
  index = ++index % params.length;
  area.setText(params[index]);
  }
  }
  
  public static void main(String args[]){
  if(args.length > 1){
  UIDemo1 one = new UIDemo1(args);
  }else{
  usage();
  }
  }
  private static void usage(){
  System.err.println("You may excute this program as below:");
  System.err.println("java UIDemo1 params1 params2 ...");
  System.exit(-1);
  }
  }
  
  //UIDemo2
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class UIDemo2{
  private JFrame frame;
  private JButton button;
  private JTextArea area;
  private int index = -1;
  private String [] params;
  
  private UIDemo2(String args[]){
  params = args;
  area = new JTextArea(5,25);
  button = new JButton("Click Here!");
  button.addActionListener(new SemiReusableActionListener(this));
  
  frame = new JFrame(getClass().getName());
  frame.addWindowListener(new ReusableWindowAdapter());
  Container cont = frame.getContentPane();
  JScrollPane scrollPane = new JScrollPane(area);
  cont.add(scrollPane,BorderLayout.NORTH);
  cont.add(button,BorderLayout.SOUTH);
  frame.pack();
  frame.setVisible(true);
  }
  void setMessage(Object source){
  if(source.equals(button)){
  index = ++index % params.length;
  area.setText(params[index]);
  }
  }
  
  public static void main(String args[]){
  if(args.length > 1){
  UIDemo2 two = new UIDemo2(args);
  }else{
  usage();
  }
  }
  private static void usage(){
  System.err.println("You may excute this program as below:");
  System.err.println("java UIDemo2 params1 params2 ...");
  System.exit(-1);
  }
  }
  
  //SemiReusableActionListener
  
  import java.awt.event.*;
  
  public class SemiReusableActionListener implements ActionListener{
  private UIDemo2 demo2;
  SemiReusableActionListener(UIDemo2 uiDemo){
  demo2 = uiDemo;
  }
  public void actionPerformed(ActionEvent ae){
  demo2.setMessage(ae.getSource());
  }
  }
  
  //UIDemo3
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import java.lang.reflect.*;
  
  public class UIDemo3{
  private JFrame frame;
  private JButton button1;
  private JTextArea area;
  private int index = -1;
  private String [] params;
  private UIDemo3(String args[]){
  params = args;
  area = new JTextArea(5,25);
  button1 = new JButton("Click here!");
  //setup required information to use GenericActionListener
  String methodName = "setMessage";
  Class [] paramTypes = {java.lang.Object.class};
  Object [] methodArgs = { button1 };
  Class clazz = this.getClass();
  try{
  Method method = clazz.getMethod(methodName, paramTypes);
  
  button1.addActionListener(new
  ReusableActionListener(method, this, methodArgs));
  }
  catch(Exception e){
  System.out.println("Could not find the method: "+methodName+"/nNow Exiting...");
  System.exit(-1);
  }
  
  frame = new JFrame(getClass().getName());
  frame.addWindowListener(new ReusableWindowAdapter());
  Container cont = frame.getContentPane();
  JScrollPane scrollPane = new JScrollPane(area);
  cont.add(scrollPane,BorderLayout.NORTH);
  cont.add(button1,BorderLayout.SOUTH);
  frame.pack();
  frame.setVisible(true);
  }
  public void setMessage(Object source){
  if(source.equals(button1)){
  index = ++index % params.length;
  area.setText(params[index]);
  }
  }
  public static void main(String args[]){
  if(args.length > 1){
  UIDemo3 three = new UIDemo3(args);
  }else{
  usage();
  }
  }
  private static void usage(){
  System.err.println("You may excute this program as below:");
  System.err.println("java UIDemo3 params1 params2 ...");
  System.exit(-1);
  }
  }
  
  //ReusableWindowAdapter
  
  import java.awt.*;
  import java.awt.event.*;
  
  public class ReusableWindowAdapter extends WindowAdapter{
  public void windowClosing(WindowEvent we){
  Object source = we.getSource();
  if(source instanceof Frame){
  ((Frame)source).setVisible(false);
  ((Frame)source).dispose();
  System.exit(0);
  }
  }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长海县| 永丰县| 衢州市| 南涧| 石河子市| 甘孜县| 靖宇县| 遵义县| 宁晋县| 惠安县| 汉寿县| 桃园市| 白水县| 阳江市| 眉山市| 巴青县| 堆龙德庆县| 牙克石市| 开封县| 依安县| 平泉县| 廉江市| 茌平县| 关岭| 垦利县| 宾阳县| 建水县| 介休市| 怀宁县| 安阳市| 揭西县| 湖口县| 政和县| 汝州市| 龙井市| 都安| 嘉禾县| 连州市| 永寿县| 永寿县| 瑞丽市|