java awt 事件监听器

//TestActionEvent.java


import java.awt.*;
import java.awt.event.*;

public class TestActionEvent{
	public static void main(String[] args){
		Frame f=new Frame("TestActionEvent");
		Button b=new Button("Press Me !");
		Monitor bh=new Monitor();
		b.addActionListener(bh);
		f.add(b,BorderLayout.CENTER);
		f.pack();
//TestTextField.java

import java.awt.*;
import java.awt.event.*;

class TestTextField{
	public static void main(String[] args){
		new TFFrame();
	}	
}

class TFFrame extends Frame{
	TFFrame(){
		TextField tf=new TextField();
		add(tf);
		tf.addActionListener(new TFActionListener());
		pack();
		setVisible(true);	
	}	
}

class TFActionListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			TextField tf=(TextField)e.getSource();
			System.out.println(tf.getText());
			tf.setText("");
		}
}

//TestTextField.java 更改回显字符

import java.awt.*;
import java.awt.event.*;

class TestTextField{
	public static void main(String[] args){
		new TFFrame();
	}	
}

class TFFrame extends Frame{
	TFFrame(){
		TextField tf=new TextField();
		add(tf);
		tf.addActionListener(new TFActionListener());
		tf.setEchoChar('*');
		pack();
		setVisible(true);	
	}	
}

class TFActionListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			TextField tf=(TextField)e.getSource();
			System.out.println(tf.getText());
			tf.setText("");
		}
}

//加法计算器

import java.awt.*;
import java.awt.event.*;

class TestTFMath{
	public static void main(String[] args){
		new TFMath();
	}	
}

class TFMath extends Frame{
	TextField a=null;
	TextField b=null;
	TextField c=null;
	
	TFMath(){
		a=new TextField();
		b=new TextField();
		c=new TextField();
		Button plus=new Button("计算");
		Label lbPlus=new Label("+");
		Label lbIs=new Label("=");
		plus.addActionListener(new ActionListener_plus(this));
		setLayout(new FlowLayout());
		add(a);
		add(lbPlus);
		add(b);
		add(lbIs);
		add(c);
		add(plus);
		pack();
		setVisible(true);	
	}	
}

class ActionListener_plus implements ActionListener{
		/*TextField a,b,c;
		ActionListener_plus(TextField a,TextField b,TextField c){
			this.a=a;
			this.b=b;
			this.c=c;
		}*/
		
		TFMath tm=null;
		ActionListener_plus(TFMath tm){
			this.tm=tm;
		}
		public void actionPerformed(ActionEvent e){
			int i_a=Integer.parseInt(tm.a.getText());
			int i_b=Integer.parseInt(tm.b.getText());
			tm.c.setText(""+(i_a+i_b));
		}
}

//匿名内部类事件监听器

import java.awt.*;
import java.awt.event.*;

class TestTFMath{
	public static void main(String[] args){
		new TFMath();
	}	
}

class TFMath extends Frame{
	TFMath(){
		final TextField a=new TextField();
		final TextField b=new TextField();
		final TextField c=new TextField();
		Button plus=new Button("计算");
		Label lbPlus=new Label("+");
		Label lbIs=new Label("=");
		plus.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					c.setText(""+(Integer.parseInt(a.getText())
						+Integer.parseInt(b.getText())));
				}
		});
		setLayout(new FlowLayout());
		add(a);
		add(lbPlus);
		add(b);
		add(lbIs);
		add(c);
		add(plus);
		pack();
		setVisible(true);	
	}	
}



//TestActionEvent2.java

import java.awt.*;
import java.awt.event.*;

public class TestActionEvent2{
	public static void main(String[] args){
		Frame f=new Frame("TestActionEvent");
		Button btn_start=new Button("Start");
		Button btn_stop=new Button("Stop");
		Monitor bh=new Monitor();
		btn_start.addActionListener(bh);
		btn_stop.addActionListener(bh);
		f.add(btn_start,BorderLayout.WEST);
		f.add(btn_stop,BorderLayout.EAST);
		f.pack();
		f.setVisible(true);
	}	
}

class Monitor implements ActionListener{
		public void actionPerformed(ActionEvent e){
			System.out.println("Button " + e.getActionCommand() + " Pressed!");
		}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值