java中处理事件的几种方式

实现效果

在文本框中输入内容,当单击+hello按钮时,在下方文本域中显示输入的内容+hello;当单击+bye按钮时,在下方文本域中显示输入的内容+bye。
在这里插入图片描述

实现过程

外部类实现
package ch9.dealevent;

import javax.swing.*;
import java.awt.*;

public class OutClass extends JFrame{
	JLabel label;
	JTextField text;
	JButton button1,button2;
	JTextArea textArea;
	Box boxH;
	Box boxOne,boxTwo;
	
	
	OutClass(){
		init();
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	void init(){
		setLayout(new FlowLayout());
		label = new JLabel("请输入:");
		text = new JTextField(10);
		button1 = new JButton("+hello");
		button2 = new JButton("+bye");
		textArea = new JTextArea(9,25);
		boxTwo = Box.createHorizontalBox();
		boxOne = Box.createHorizontalBox();
		boxOne.add(label);
		boxOne.add(text);
		boxTwo.add(button1);
		boxTwo.add(button2);
		add(boxOne);
		add(boxTwo);
		add(new JScrollPane(textArea));
		button1.addActionListener(new OutClassListener(text,textArea,button1));
		button2.addActionListener(new OutClassListener(text,textArea,button1));
	}
}
//外部监听器

package ch9.dealevent;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class OutClassListener implements ActionListener{
	JButton button1,button2;
	JTextField text;
	JTextArea textArea;
	OutClassListener(JTextField text,JTextArea textArea,JButton button){
		this.text = text;
		this.textArea = textArea;
		this.button1 = button;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String textStr = text.getText();
		
		if(e.getSource() == button1){
			
			textArea.append(textStr+"hello\n");
		}else{
			textArea.append(textStr+"bye\n");
		}
		
	}

}

内部类实现
package ch9.dealevent.InnerClass;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class InnerClass extends JFrame{
	JLabel label;
	JTextField text;
	JButton button1,button2;
	JTextArea textArea;
	Box boxH;
	Box boxOne,boxTwo;
	class InnerListener implements ActionListener {
		public void actionPerformed(ActionEvent e){
			String textStr = text.getText();
			
			if(e.getSource() == button1){
				
				textArea.append(textStr+"hello\n");
			}else{
				textArea.append(textStr+"bye\n");
			}
		}
	}
	
	public InnerClass(){
		init();
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	void init(){
		setLayout(new FlowLayout());
		label = new JLabel("请输入:");
		text = new JTextField(10);
		button1 = new JButton("+hello");
		button2 = new JButton("+bye");
		textArea = new JTextArea(9,25);
		boxTwo = Box.createHorizontalBox();
		boxOne = Box.createHorizontalBox();
		boxOne.add(label);
		boxOne.add(text);
		boxTwo.add(button1);
		boxTwo.add(button2);
		add(boxOne);
		add(boxTwo);
		add(new JScrollPane(textArea));
		button1.addActionListener(new InnerListener());
		button2.addActionListener(new InnerListener());
	}
	
}

匿名类
package ch9.dealevent.InnerClass;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class AnonymousClass extends JFrame {
	JLabel label;
	JTextField text;
	JButton button1,button2;
	JTextArea textArea;
	Box boxH;
	Box boxOne,boxTwo;
	
	
	public AnonymousClass(){
		init();
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	public void init(){
		setLayout(new FlowLayout());
		label = new JLabel("请输入:");
		text = new JTextField(10);
		button1 = new JButton("+hello");
		button2 = new JButton("+bye");
		textArea = new JTextArea(9,25);
		boxTwo = Box.createHorizontalBox();
		boxOne = Box.createHorizontalBox();
		boxOne.add(label);
		boxOne.add(text);
		boxTwo.add(button1);
		boxTwo.add(button2);
		add(boxOne);
		add(boxTwo);
		add(new JScrollPane(textArea));
		button1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String textStr = text.getText();
				textArea.append(textStr+"hello\n");
				text.setText(null);
			}
		});
		button2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String textStr = text.getText();
				textArea.append(textStr+"hello\n");
				text.setText(null);
			}
		});
	}
	
}

自身类实现
package ch9.dealevent.InnerClass;

import java.awt.*;

import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.event.*;
import ch9.dealevent.OutClassListener;

public class SelfClass extends JFrame implements ActionListener{
	JLabel label;
	JTextField text;
	JButton button1,button2;
	JTextArea textArea;
	Box boxH;
	Box boxOne,boxTwo;
	
	
	public SelfClass(){
		init();
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	void init(){
		setLayout(new FlowLayout());
		label = new JLabel("请输入:");
		text = new JTextField(10);
		button1 = new JButton("+hello");
		button2 = new JButton("+bye");
		textArea = new JTextArea(9,25);
		boxTwo = Box.createHorizontalBox();
		boxOne = Box.createHorizontalBox();
		boxOne.add(label);
		boxOne.add(text);
		boxTwo.add(button1);
		boxTwo.add(button2);
		add(boxOne);
		add(boxTwo);
		add(new JScrollPane(textArea));
		button1.addActionListener(this);
		button2.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e){
		String textStr = text.getText();
		
		if(e.getSource() == button1){
			
			textArea.append(textStr+"hello\n");
		}else{
			textArea.append(textStr+"bye\n");
		}
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值