本文共 4420 字,大约阅读时间需要 14 分钟。
基本事件定义(观察者模式 Command模式)
import java.awt.*;
import java.awt.event.*;
public class TFActionEvent {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TFFrame();
}
}
class TFFrame extends Frame
{
TFFrame()
{
TextField tf = new TextField();
tf.setName("tf");
add(tf,"North");
TextField tf2 = new TextField();
tf2.setName("tf2");
//add(tf2);
add(tf2,"Center");
tf.addActionListener(new TFActionListener());
tf2.addActionListener(new TFActionListener());
pack();
setVisible(true);
}
}
class TFActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
TextField tf = (TextField)arg0.getSource();
System.out.println(tf.getName()+":"+tf.getText());
//tf.setText("");
}
}
内部类的一个应用
import java.awt.*;import java.awt.event.*;public classTFMath {public static voidmain(String[] args) {newTFFrame().launchFrame();
}
}class TFFrame extendsFrame {
TextField num1, num2, num3;public voidlaunchFrame() {
num1= new TextField(10);
num2= new TextField(10);
num3= new TextField(15);
Label lblPlus= new Label("+");
Button btnEqual= new Button("=");
btnEqual.addActionListener(newMyMonitor());
setLayout(newFlowLayout());
add(num1);
add(lblPlus);
add(num2);
add(btnEqual);
add(num3);
pack();
setVisible(true);
}private class MyMonitor implementsActionListener {public voidactionPerformed(ActionEvent e) {int n1 =Integer.parseInt(num1.getText());int n2 =Integer.parseInt(num2.getText());
num3.setText("" + (n1+n2));
}
}
}//class MyMonitor implements ActionListener {//TextField num1, num2, num3;
/*public MyMonitor(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}*/
/*TFFrame tf = null;
public MyMonitor(TFFrame tf) {
this.tf = tf;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf.num1.getText());
int n2 = Integer.parseInt(tf.num2.getText());
tf.num3.setText("" + (n1+n2));
}
}*/
优点: 可以方便的访问包装类的成员
可以清楚的组织逻辑,防止不应该被其他类访问的类进行访问
何时使用
该类不允许或不需要其他类进行访问
Java Swing中处理各组件事件的一般步骤是:
1. 新建一个组件(如JButton)。
2. 将该组件添加到相应的面板(如JPanel)。
3. 注册监听器以监听事件源产生的事件(如通过ActionListener来响应用户点击按钮)。
4. 定义处理事件的方法(如在ActionListener中的actionPerformed中定义相应方法)。
3种时间处理机制:
(1)使用内部类
import java.awt.*;import java.awt.event.*;public class TFFrame extendsFrame {
TextField num1,num2,num3;public voidlaunchFrame()
{
num1=new TextField(10);
num2=new TextField(10);
num1.setText("0");
num2.setText("0");
num3=new TextField(15);
num3.setText("0");
Label lblplus=new Label("+");
Button btnEqual=new Button("=");
btnEqual.addActionListener(newmyMontion1(num1,num2,num3));
num2.addActionListener(new myMontion2(this));
num1.addActionListener(newmyMontion());
setLayout(newFlowLayout());
add(num1);
add(lblplus);
add(num2);
add(btnEqual);
add(num3);
pack();
setVisible(true);
addWindowListener(newWindowAdapter() {public voidwindowClosing(WindowEvent e) {
System.exit(-1);
}
});
}public class myMontion implementsActionListener
{
@Overridepublic voidactionPerformed(ActionEvent e)
{int n1=Integer.parseInt(num1.getText());int n2=Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
}
}
}
(2)传递窗体 到事件
importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;public class myMontion2 implementsActionListener {
TFFrame tf=null;publicmyMontion2(TFFrame tf)
{this.tf=tf;
}
@Overridepublic voidactionPerformed(ActionEvent e) {//TODO Auto-generated method stub
int n1=Integer.parseInt(tf.num1.getText());int n2=Integer.parseInt(tf.num2.getText());
tf.num3.setText(""+(n1+n2));/*if ((this.tf.num1.getText()!=null) && (this.tf.num2.getText()!=null))
{
int n1=Integer.parseInt(this.tf.num1.getText());
int n2=Integer.parseInt(this.tf.num2.getText());
this.tf.num3.setText(""+(n1+n2));
}*/}
}
(3)传递窗体的组件 到事件
importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;import java.awt.*;public class myMontion1 implementsActionListener {
TextField num1,num2,num3;publicmyMontion1(TextField num1,TextField num2 ,TextField num3)
{this.num1=num1;this.num2=num2;this.num3=num3;
}
@Overridepublic voidactionPerformed(ActionEvent e) {//TODO Auto-generated method stub
int n1=Integer.parseInt(num1.getText());int n2=Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));/*if ((this.tf.num1.getText()!=null) && (this.tf.num2.getText()!=null))
{
int n1=Integer.parseInt(this.tf.num1.getText());
int n2=Integer.parseInt(this.tf.num2.getText());
this.tf.num3.setText(""+(n1+n2));
}*/}
}
// new TFFrame().launchFrame();
转载地址:http://lfnzx.baihongyu.com/