当前位置:网站首页>Simple listener
Simple listener
2022-07-16 07:02:00 【Lemon Ning】
First, we need to know how to construct an event monitor
1. Create a BtnAction class , Declare after class implements ActionListener
public class BtnAction implements ActionListener{}2. Override methods in the interface ( Add the code you want in the method )
public void actionPerformed(ActionEvent e) {
JFrame jF = new JFrame();
count++;
jF.setTitle("" + count);
jF.setSize(300, 100);
jF.setVisible(true);
}
3. Use the button object to call addActionListener Method
ButtonListener btnaction = new ButtonListener();
O1.addActionListener(btnaction);However, the parameter of the method is the object of the class we created ourselves
Mouse monitor
The steps of mouse listener are similar , First, create a class declaration method MouseListener, And then rewrite it MouseListener The method in the method , Load the method you want . In addition, you need to create a listener in the interface
Assign the obtained object to the attribute in the listener .
The following is a drawing image code using a mouse listener
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class DrawListener implements MouseListener {
int x1,x2,y1,y2;
Graphics g;
Graphics p;
public void mouseClicked(MouseEvent e){
System.out.println(" Click on ");
}
public void mousePressed(MouseEvent e){
x1 = e.getX();
y1 = e.getY();
System.out.println(" Press down ");
}
public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
System.out.println(" Release ");
g.drawLine(x1,y1,x2,y2);
p.drawRect(x1,y1,x2,y2);
//g.fillOval (x1,y1,30,30);
}
public void mouseEntered(MouseEvent e){
System.out.println(" Get into ");
}
public void mouseExited(MouseEvent e){
System.out.println(" Leave ");
}
}
The following is the window interface settings
package com.yn.draw0311;
import java.awt.*;
import javax.swing.*;
public class DrawUI extends JFrame {
public void initUI() {
// Because it inherits the class JFrame, So there is no need to create objects
this.setTitle(" Graph drawing ");
this.setSize(500, 800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// Create a listener Add to form object
DrawListener dl = new DrawListener();
// Non button
addMouseListener(dl);
// obtain Graphics object Assign a value to Properties in the listener g
Graphics g = getGraphics();
dl.g = g;
dl.p=g;
}
public static void main(String[] args) {
DrawUI drawUI = new DrawUI();
drawUI.initUI();
}
}边栏推荐
- 利用指针编写程序实现在一个字符串的隨意位置上插入一个字符(要求插入字符的位置由用户从键盘输入)。
- IIC communication
- 桶排序+抽屉原理
- 支付宝电脑网站支付
- Comparison and application of DHT11 and dht22 (am2302)
- Chapter 4 stm32+ld3320+syn6288+dht11 realize voice acquisition of temperature and humidity values (Part 2)
- 黑马数据库笔记DQL
- Experience of using voice chip jq8400
- (一)OSI模型
- LeetCode 刷题 第十二天 + DL第一天
猜你喜欢

Redis is the fastest to get started in history (attach redis on ECs)

Go秒杀系统3--Work模式,Publish模式

Chapter VI use of OLED module +stm32
![[introduction to go language] 12 go language structure (struct) detailed explanation](/img/44/ca65446c5b75bb0860b62906973f85.png)
[introduction to go language] 12 go language structure (struct) detailed explanation
![[introduction to go language] 03 go language data types, variables, constants](/img/36/c1b03400525b15c67b8c0518cfbdf2.png)
[introduction to go language] 03 go language data types, variables, constants
![[go language introduction] 13 go language interface details](/img/38/7576257ecc706251e23b8a5ec2d98e.png)
[go language introduction] 13 go language interface details

Go秒杀系统3--项目结构搭建,商品模型开发。
![[go language introduction] 06 go language circular statement](/img/c1/097bbd37199d2755caccf64c4ae162.png)
[go language introduction] 06 go language circular statement

四舍五入,向下取整,向上取整的使用

Vscode automatically adds comments
随机推荐
Experience of using voice chip jq8400
LeetCode 刷题 第十天
AMD RDNA 3 Navi 31旗舰GPU据称采用3D V-Cache封装以及最高384MB缓存
RT_ Use of thread mutexes
How to classify the consumption ability of members?
Download and burn raspberry pie system image
002 pointers and functions
[introduction to go language] 09 detailed explanation of go language slice
关于继承
基于数组和节点的动态变化(增删改查)
What can I do after learning C language? Let's make a push box first ~ (there are pictures)
分布式理论
Introduction to vscode plug-in installation
How to set the Internet function of raspberry pie
数据库黑马笔记DDL
动态规划+组合数学
2021-07-31
Knowledge of dark horse database
简单的监听器
LeetCode精讲——735. 行星碰撞(难度:中等)