GUI事件处理

              GUI中的事件处理

  • GUI中增加事件处理,是GUI设计与实现的核心

  • GUI中事件处理的一般过程是:当用户在界面上利用鼠标或键盘进行操作时,监测GUI的操作系统将所发生的事件传送给GUI应用程序,应用程序根据事件的类型做出相应的反应。

  • AWT事件模型中主要包含了3类对象:

    1、事件(Event): 用户对应用程序进行操作时会产生事件,在java中,事件被封装成一个对象,事件是描述所发生事件的对象,Java中有很多不同类型的事件类,用来描述不同类型的用户动作

    2、事件源:产生事件的组件 3、事件处理器:事件处理器是一个方法,该方法接收一个事件对象,并做出相应处理。

    • Java GUI事件处理采用委托模型或称为监听器模型。在这种模型中,需要响应用户操作的组件事先已经注册一个或多个包含事件处理器的对象,称为监听器。

         requestFocus(); :光标下移

image-20220509223729906

  • 事件对象只向已注册的监听器报告。

 

事件操作接口名适配器类方法
ActionEvent 激活组件操作(对应按钮点击、菜单选择、列表框选择、在文本域中按回车键等) ActionListener actionPerformed
AdjustmentEvent 移动滚动条 AdjustmentListener adjustmentValueChanged
ComponentEvent 组件移动、缩放、显示、隐藏等 ComponentListener ComponentAdapter componentHidden componentMoved componentResized componentShown
ContainerEvent 容器中增加或删除组件 ContainerListener ContainerAdapter componentAdded componentRemoved
FocusEvent 组件得到焦点或失去焦点 FocusListener FocusAdapter focusGained focusLost
ItemEvent 条目状态改变 ItemListener itemStateChanged
KeyEvent 键盘输入 KeyListener KeyAdapter keyPressed keyReleased keyTyped
事件操作接口名适配器类方法
MouseEvent 鼠标按下、释放、单击、进入或离开 MouseListener MouseAdapter mouseClicked mouseEntered mouseExited mousePressed mouseReleased
鼠标移动、拖动 MouseMotionListener MouseMotionAdapter mouseDragged mouseMoved  
TextEvent 文本或或文本区值改变 TextListener textValueChanged
WindowEvent 窗口激活、打开、关闭最小化等 WindowListener WindowAdapter windowActivated windowClosed windowClosing windowDeactivated windowDeiconified windowIconified windowOpened

事件处理机制实现的三种方式

  • 内部类

  • 推荐使用, 既使用内部类,又避免多重继承的限制。

  • 自身类

  • 匿名类

 

内部类

package src2;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.omg.CORBA.PUBLIC_MEMBER;

public class Prectice1 extends JFrame
{
JPanel jpanel;
JLabel lab1,lab2,lab3;
TextField text1,text2,text3;
public Prectice1(String prectice1)
{
        super(prectice1);
init();
}
public void init()
{
   jpanel=new JPanel();
   lab1=new JLabel("单价");
   lab2=new JLabel("数量");
   lab3=new JLabel("总额");
text1=new TextField(15);
text2=new TextField(15);
text3=new TextField(15);
jpanel.add(lab1);
jpanel.add(text1);
jpanel.add(lab2);
jpanel.add(text2);
jpanel.add(lab3);
jpanel.add(text3);
this.setContentPane(jpanel);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300,250);
this.setLocationRelativeTo(null);
this.setResizable(true);
text1.addActionListener(new action());
text2.addActionListener(new action());
text3.addActionListener(new action());


}


  public static void main(String[] args)
  {
Prectice1 prectice1=new Prectice1("GUI");
  }
 class action implements ActionListener
{


public void actionPerformed(ActionEvent e)
{
if (e.getSource()==text1)
{
 text2. requestFocus();
}
else if (e.getSource()==text2)
{
  text3.requestFocus(); //光标下移
  double cound=Double.parseDouble(text1.getText());//字符串转换为double
double cound2=Double.parseDouble(text2.getText());
// int num=Integer.parseInt(text2.getText());
double sum=cound*cound2;
text3.setText(""+sum);
}
  }

  }
}

匿名内部类

package src;

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

import javax.swing.*;

public class Area extends JFrame
{
JLabel lab1=new JLabel("输入半径:");
JLabel lab2=new JLabel("圆的面积:");
JTextField txt1=new JTextField(15);
JTextField txt2=new JTextField(15);
JPanel p=new JPanel();
JButton btn1=new JButton("计算");
JButton btn2=new JButton("退出");
public Area()
{
p.add(lab1);
p.add(txt1);
p.add(lab2);
p.add(txt2);
p.add(btn1);
p.add(btn2);
this.getContentPane().add(p);
this.setSize(300,150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
txt1.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent arg0)
{

if (arg0.getSource()==txt1)
{
  txt2.requestFocus();
}
}
});
btn1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{

double r=Double.parseDouble(txt1.getText());//获取文本框txtf1的值,转换成Double赋值给price
double s=r*r*3.14;
txt2.setText(String.valueOf(s));//将total转换为String,赋值给文本框txtf3
}
});
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}

public static void main(String[] args)
{
Area fr=new Area();
fr.setVisible(true);
}
}
 
posted @ 2022-05-10 16:42  zjw_rp  阅读(234)  评论(0编辑  收藏  举报