Swing的事件模型的优点就在于它的灵活性。你可以调用方法给组件添加或删除事件。今天再看这一节时,想到了一个以前没想过的小问题,如果一个按钮先后注册了两个监听者Listerner1, Listerner2,那么在单击按钮时,事件处理的顺序到底是如何的哪?真的是我以前一直以为的两者构成了一个监听者链表,Listerner2接在Listerner1后面吗?那就是说Listerner1会先处理接收的事件哪?真的是这样吗?
Swing的事件模型的优点就在于它的灵活性。你可以调用方法给组件添加或删除事件。今天再看这一节时,想到了一个以前没想过的小问题,如果一个按钮先后注册了两个监听者Listerner1, Listerner2,那么在单击按钮时,事件处理的顺序到底是如何的哪?真的是我以前一直以为的两者构成了一个监听者链表,Listerner2接在Listerner1后面吗?那就是说Listerner1会先处理接收的事件哪?真的是这样吗?
package com.vitamin.UI;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
![](/Images/OutliningIndicators/None.gif)
import com.vitamin.*;
import com.vitamin.Console.console;
import java.awt.*;
import javax.swing.*;
![](/Images/OutliningIndicators/None.gif)
public class HelloTest extends JFrame
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
private JButton btnOK = null;
private JLabel lbInfo = null;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* @param args
*/
public HelloTest()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.btnOK = new JButton("确定");
this.lbInfo = new JLabel();
this.btnOK.addActionListener(new L1());
this.btnOK.addActionListener(new L2());
Container con = this.getContentPane();
con.setLayout(new BorderLayout());
con.add(this.btnOK,BorderLayout.SOUTH);
con.add(this.lbInfo,BorderLayout.NORTH);
console.run(this,400,400);
}
class L1 implements ActionListener
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void actionPerformed(ActionEvent e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lbInfo.setText("");
}
}
class L2 implements ActionListener
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void actionPerformed(ActionEvent e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
JOptionPane.showMessageDialog(HelloTest.this,"L2接收到事件");
lbInfo.setText("Hello");
}
}
public static void main(String[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// TODO Auto-generated method stub
HelloTest ht = new HelloTest();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
运行后居然发现” L2接收到事件”先发生了,因为lbInfo的Text是空的。汗。。。
一般情况下,设计类的时候总是强调一个类"只作一件事情"。涉及用户界面的时候更是如此,因为你很可能会把"要作什么"同"要怎样显示"给混在一起了。这种耦合严重妨碍了代码的复用。比较好的做法是将"业务逻辑(business login)"同GUI分离开来。这样不仅方便了业务逻辑代码的复用,也简化了GUI的复用。
还有一种情况,就是多层系统(multitiered systems),也就是说”业务对象(business object)"完全贮存在另一台机器上。业务规则的集中管理能使规则的更新立即对新交易生效,因此这是这类系统所追求的目标。但是很多应用程序都会用到这些业务对象,所以它们绝不能同特定的显示模式连在一起。它们应该只做业务处理,别的什么都不管.
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.applet.*;
import com.vitamin.Console.*;
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
class BusinessLogic
{
private int modifier;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public BusinessLogic(int mod)
{ modifier = mod; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setModifier(int mod)
{ modifier = mod; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int getModifier()
{ return modifier; }
// Some business operations:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int calculation1(int arg)
{ return arg * modifier;}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int calculation2(int arg)
{ return arg + modifier;}
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class Separation extends JApplet
{
private JTextField
t = new JTextField(15),
mod = new JTextField(15);
private JButton
calc1 = new JButton("Calculation 1"),
calc2 = new JButton("Calculation 2");
private BusinessLogic bl = new BusinessLogic(2);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static int getValue(JTextField tf)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
return Integer.parseInt(tf.getText());
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch(NumberFormatException e)
{
return 0;
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
class Calc1L implements ActionListener
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void actionPerformed(ActionEvent e)
{
t.setText(Integer.toString(
bl.calculation1(getValue(t))));
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
class Calc2L implements ActionListener
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void actionPerformed(ActionEvent e)
{
t.setText(Integer.toString(
bl.calculation2(getValue(t))));
}
}
// If you want something to happen whenever
// a JTextField changes, add this listener:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
class ModL implements DocumentListener
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void changedUpdate(DocumentEvent e)
{}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void insertUpdate(DocumentEvent e)
{
bl.setModifier(getValue(mod));
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void removeUpdate(DocumentEvent e)
{
bl.setModifier(getValue(mod));
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void init()
{
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(t);
calc1.addActionListener(new Calc1L());
calc2.addActionListener(new Calc2L());
JPanel p1 = new JPanel();
p1.add(calc1);
p1.add(calc2);
cp.add(p1);
mod.getDocument().addDocumentListener(new ModL());
JPanel p2 = new JPanel();
p2.add(new JLabel("Modifier:"));
p2.add(mod);
cp.add(p2);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
console.run(new Separation(), 250, 100);
}
}