Java可复用事件处理的筹划与实现解析[Java编程]
本文“Java可复用事件处理的筹划与实现解析[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
目前,面向对象是软件系统建模的主流技术,利用面向对象技术建模的主要指标之一是可复用性.为了更好地办理软件复用性和扩大性问题,计划情势得到了越来越多的关注与利用.
结合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+" Now 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);
}
}
}
以上是“Java可复用事件处理的筹划与实现解析[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |