日志三:GUI事件处理

【导语】
今天已是实训的第三天了,时间过得很快,一下子一个星期的实训已经过了一半了,而我至今,对于自己这几天学到的东西还不太懂,可能因为基础不怎么扎实,很多的JAVA编程知识我都不知道,今天我因为要做一个类似计算机的页面,我才发现我真的很多都不懂的。经过老师的一番指导,我开始慢慢的了解了GUI,了解了最基本的图形用户页面,也开始懂得了一些事件处理的知识,以下的一些知识点都是我从网上看到的,我不知道是否全部都对,但是在我自己看来,我暂时还看不出有什么,这些大概就是我对于事件处理的一些理解。


【目的】
1. 熟悉GUI中事件处理的步骤。
2. 了解GUI事件处理的操作。
 

【目标】
项目一:事件处理的概念
项目二:类似计算器的页面


【方法与步骤】
1. 熟悉GUI中事件处理概念。
触发事件:在TextField组件中按下回车键时,可以触发ActionEvent事件,因此在TextField组件上可注册ActionListener监听器,以关联所需的处理逻辑。
事件编程:用户编程定义每个特定事件发生时程序应做出何种响应,并且这些响应代码会在对应的事件发生时由系统自动调用。
事件源:产出事件的组件。
监听器:对组件所产生的事件作出具体响应的代吗,即事件产出与处理分别由两个不同类(它们可以分别放在不同的程序中)加以编程实现。
事件处理机制:AWT组件自身不编程处理相应的事件,面是交由事件监听器(它可以是组件所在的容器类或另外的Java程序类,只要它们实现了相关的事件监听器接口即可)处理(事件授权处理模型)。
事件编程的基本原则:事件处理的类代码要对某一类事件加以处理,则应实现它们所对应的接口,并且给出该接口中定义的全部事件响应函数的功能实现(重写其函数体);然后在创建组件时注册该事件的监听器(响应者)
事件注册:事件源通过对特定的事件进行注册,以指定该事件的监听器(响应者)是谁。
事件注册函数:函数名由“add + 事件类型对应的监听器接口名称”组成;函数参数为监听器对象(实现事件响应的类的对象,如容器组件自身响应该事件,则监听器对象应用this代表)。


2. 事件编程步骤:
①实现某一事件的监听器接口(定义事件处理类并实现监听器接口)。
②在事件处理类中重写(实现)其事件处理的函数体。
③在创建AWT组件时注册事件处理代码以指定该事件的监听器(响应者)是谁。


3. 事件编程的基本规则:
(1)组件对事件的响应形式:忽略它(本类不实现对应的监听器接口)或编程事件函数以处理它(可根据应用需要替换某一个组件的相应缺省事件处理函数,从而响应用户对该组件的操作。本类实现某类事件对应的监听器接口,并实现对应的响应函数),也可屏蔽它(将其事件响应函数体置空)。
(2)事件响应类(监听器)可以实现多个监听器接口,以响应多组不同事件,从而可使同一个组件可以注册多种事件。
(3)利用事件响应函数中的事件对象获取事件产生时的相关信息(event.getSource())事件源对象,event.getX(), event.getY(),事件产生时的鼠标位置,event.getActionCommand(),获取组件的字符串名称。


4. 结合WB进行简单编程
     老师上一节课因为有教我们学习如何使用WB,所以我结合了WB和自己的编程,成功的完成了“类似计算机”的页面,而这其中也少不了老师的帮忙,老师的指导让我知道了事件处理的原理,让我可以更好的运用Swing包,更好的学习GUI。下面有两段代码,是我根据我对GUI事件处理理解,它们都可以实现一样的功能。

 
【实验代码】

package test;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.AncestorListener;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FileChoosertext extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private Object button;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileChoosertext frame = new FileChoosertext();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FileChoosertext() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textField = new JTextField();
		textField.setBounds(107, 55, 80, 21);
		contentPane.add(textField);
		textField.setColumns(10);

		textField_1 = new JTextField();
		textField_1.setBounds(107, 103, 80, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);

		textField_2 = new JTextField();
		textField_2.setBounds(107, 150, 80, 21);
		contentPane.add(textField_2);
		textField_2.setColumns(10);

		JButton button = new JButton("+");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					int t1= Integer.parseInt(textField.getText());
					int t2= Integer.parseInt(textField_1.getText());
					textField_2.setText(t1+t2+"");
			}
		});
		button.setBounds(284, 33, 93, 23);
		contentPane.add(button);

		JButton button_1 = new JButton("-");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					int t1= Integer.parseInt(textField.getText());
					int t2= Integer.parseInt(textField_1.getText());
					textField_2.setText(t1-t2+"");
			}
		});
		button_1.setBounds(284, 82, 93, 23);
		contentPane.add(button_1);

		JButton button_2 = new JButton("*");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					int t1= Integer.parseInt(textField.getText());
					int t2= Integer.parseInt(textField_1.getText());
					textField_2.setText(t1*t2+"");
			}
		});
		button_2.setBounds(284, 132, 93, 23);
		contentPane.add(button_2);

		JButton button_3 = new JButton("/");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					int t1= Integer.parseInt(textField.getText());
					int t2= Integer.parseInt(textField_1.getText());
					textField_2.setText(t1/t2+"");
			}
		});
		button_3.setBounds(284, 183, 93, 23);
		contentPane.add(button_3);

		JLabel label = new JLabel("\u64CD\u4F5C\u65701");
		label.setBounds(40, 58, 47, 15);
		contentPane.add(label);

		JLabel label_1 = new JLabel("\u64CD\u4F5C\u65702");
		label_1.setBounds(40, 106, 47, 15);
		contentPane.add(label_1);

		JLabel label_2 = new JLabel("\u7ED3\u679C\uFF1A");
		label_2.setBounds(40, 153, 47, 15);
		contentPane.add(label_2);

		}

		}
	


package test;

import java.awt.BorderLayout;

public class FileChooser extends JFrame implements ActionListener{

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JButton button;
	private JButton button_1;
	private JButton button_2;
	private JButton button_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileChooser frame = new FileChooser();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FileChooser() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textField = new JTextField();
		textField.setBounds(107, 55, 80, 21);
		contentPane.add(textField);
		textField.setColumns(10);

		textField_1 = new JTextField();
		textField_1.setBounds(107, 103, 80, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);

		textField_2 = new JTextField();
		textField_2.setBounds(107, 150, 80, 21);
		contentPane.add(textField_2);
		textField_2.setColumns(10);

		button = new JButton("+");
		button.addActionListener(this);
		button.setBounds(284, 33, 93, 23);
		contentPane.add(button);

		button_1 = new JButton("-");
		button_1.addActionListener(this);
		button_1.setBounds(284, 82, 93, 23);
		contentPane.add(button_1);

		button_2 = new JButton("*");
		button_2.addActionListener(this);
		button_2.setBounds(284, 132, 93, 23);
		contentPane.add(button_2);

		button_3 = new JButton("/");
		button_3.addActionListener(this);
		button_3.setBounds(284, 183, 93, 23);
		contentPane.add(button_3);

		JLabel label = new JLabel("\u64CD\u4F5C\u65701");
		label.setBounds(40, 58, 47, 15);
		contentPane.add(label);

		JLabel label_1 = new JLabel("\u64CD\u4F5C\u65702");
		label_1.setBounds(40, 106, 47, 15);
		contentPane.add(label_1);

		JLabel label_2 = new JLabel("\u7ED3\u679C\uFF1A");
		label_2.setBounds(40, 153, 47, 15);
		contentPane.add(label_2);

	}
	public void actionPerformed(ActionEvent e) {
		int t1= Integer.parseInt(textField.getText());
		int t2= Integer.parseInt(textField_1.getText());
		if(e.getSource()==button){
			textField_2.setText(t1+t2+"");
		}
		if(e.getSource()==button_1){
			textField_2.setText(t1-t2+"");
		}
		if(e.getSource()==button_2){
			textField_2.setText(t1*t2+"");
		}
		if(e.getSource()==button_3){
			textField_2.setText(t1/t2+"");
		}
	}
}



【实验结果】

  



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值