今天我们来聊聊GUI中常用的四种事件吧!
1、鼠标事件
常用方法:
示例代码:
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**低价事件
* 鼠标事件
* @author LinChi
*
*/
public class TestMouseListener extends JFrame{
//定义面板组件
private JPanel pan;
private JPanel player;
public TestMouseListener() {
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setLayout(null);
addComponent();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addComponent() {
pan = new JPanel();
pan.setBounds(120,120,50,50);
pan.setBorder(BorderFactory.createEtchedBorder());
pan.setBackground(Color.GRAY);
this.add(pan);
player = new JPanel();
player.setBounds(20,20,50,50);
player.setBorder(BorderFactory.createEtchedBorder());
player.setBackground(Color.BLUE);
this.add(player);
//添加事件
MouseLis ml = new MouseLis();
//添加监听鼠标的单击、按下、松开、进入、退出事件
// pan.addMouseListener(ml);
//添加监听鼠标的移动、拖拽
//player添加监听鼠标拖拽的新点,并计算差值,让player移动到新值,达到鼠标和player相对位置不变
player.addMouseMotionListener(ml);
//player添加监听鼠标按下的事件,用来记录按下的x和y坐标到pressX,pressY
player.addMouseListener(ml);
//给窗口添加鼠标滚动事件
this.addMouseMotionListener(ml);
//给窗口添加滚动事件
this.addMouseWheelListener(ml);
}
public static void main(String[] args) {
new TestMouseListener();
}
/**
* 鼠标
* 单击、按下、松开、进入、退出事件
* 移动、拖拽
* 滚轮事件
* @author LinChi
*
*/
private class MouseLis implements MouseListener,MouseMotionListener,MouseWheelListener{
//鼠标的位置
private int pressX;
private int pressY;
//记录鼠标按键标记
private int pressBtn = -1;
@Override
public void mouseClicked(MouseEvent e) {
//点击的次数
System.out.println("mouseClicked..."+e.getClickCount());
}
@Override
public void mousePressed(MouseEvent e) {
// System.out.println("mousePressed");
//记录鼠标按下的坐标
this.pressX = e.getX();
this.pressY = e.getY();
//记录鼠标按下的按钮
this.pressBtn = e.getButton();
//返回鼠标点击的值 1是左键 2是中间键 3是右键
// System.out.println(e.getButton());
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
//清空鼠标按钮的记录
this.pressBtn = -1;
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("mouseEntered");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("mouseExited");
//获取鼠标坐标
int x = e.getX();
int y = e.getY();
String direct = checkDirect(x, y);
if("RIGHT".equals(direct)) {
pan.setLocation(pan.getX() -10, pan.getY());
}else if("LFFT".equals(direct)){
pan.setLocation(pan.getX() +10, pan.getY());
}else if("TOP".equals(direct)){
//向下跑
pan.setLocation(pan.getX(), pan.getY()+10);
}else{
//向上跑
pan.setLocation(pan.getX(), pan.getY()-10);
}
}
/**
* 判断鼠标从哪个方向进入
* @param x
* @param y
* @return
*/
public String checkDirect(int x,int y) {
//从左边或者右边进入
if(y>=5 && y<=45 ) {
if(x<=5) {
//从左边进
return "LEFT";
}else {
//从右边进
return "RIGHT";
}
}else {
//从上方或者下方进入
if(y<=5) {
return "TOP";
}else {
return "BOTTOM";
}
}
}
@Override
public void mouseDragged(MouseEvent e) {
// int x = e.getX();
// int y = e.getY();
// setTitle("mouseDragged:("+x+","+y+")");
//记录移动的坐标
int moveX = e.getX() - this.pressX;
int moveY = e.getX() - this.pressX;
//判断鼠标拖拽的按键
//左键
if(this.pressBtn == MouseEvent.BUTTON1) {
player.setLocation(player.getX()+moveX, player.getY()+moveY);
}else if(this.pressBtn == MouseEvent.BUTTON3) {
//右键 获取外部类的对象
TestMouseListener.this.setLocation(player.getX()+moveX, player.getY()+moveY);
}
}
@Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
setTitle("mouseMoved:("+x+","+y+")");
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
//得到滚轮的值
//向上 正值 向下 负值
int val = e.getWheelRotation();
System.out.println("mouseWheelMoved:"+val);
pan.setLocation(pan.getX()+val*10,pan.getY()+val*10);
}
}
}
成果截图:
2、键盘事件
常用方法:
示例代码:
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**低级事件
* 鼠标事件
* @author LinChi
*
*/
public class TestKeyListener extends JFrame{
//定义面板组件
private JPanel pan;
private JPanel player;
public TestKeyListener() {
super("推箱子");
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setLayout(null);
addComponent();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addComponent() {
pan = new JPanel();
pan.setBounds(120,120,50,50);
pan.setBorder(BorderFactory.createEtchedBorder());
pan.setBackground(Color.GRAY);
this.add(pan);
player = new JPanel();
player.setBounds(20,20,50,50);
player.setBackground(Color.BLUE);
this.add(player);
//添加事件
KeyLis kl = new KeyLis();
this.addKeyListener(kl);
}
public static void main(String[] args) {
new TestKeyListener();
}
private class KeyLis implements KeyListener{
@Override
public void keyTyped(KeyEvent e) {
System.out.println("keyTyped"+e.getKeyCode()+"|"+e.getKeyChar());
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed"+e.getKeyCode()+"|"+e.getKeyChar());
//向左
if(e.getKeyCode() == 37) {
//判断是否靠近箱子右边
if(isCloseBox() == 2) {
pan.setLocation(pan.getX()-50, pan.getY());
}
player.setLocation(player.getX()-50, player.getY());
}else if(e.getKeyCode() == 38){
//向上
//判断是否靠近箱子下边
if(isCloseBox() == 3) {
pan.setLocation(pan.getX(), pan.getY()-50);
}
player.setLocation(player.getX(), player.getY()-50);
}else if(e.getKeyCode() == 39){
//向右
//判断是否靠近箱子左边
if(isCloseBox() == 0) {
pan.setLocation(pan.getX()+50, pan.getY());
}
player.setLocation(player.getX()+50, player.getY());
}else if(e.getKeyCode() == 40){
//向下
//判断是否靠近箱子上边
if(isCloseBox() == 1) {
pan.setLocation(pan.getX(), pan.getY()+50);
}
player.setLocation(player.getX(), player.getY()+50);
}
}
/**
* 靠近箱子方法
* 从左边靠近 0
* 从上方靠近 1
* 从右方靠近 2
* 从下方靠近 3
* @return
*/
public int isCloseBox() {
if(player.getY()==pan.getY()) {
//从右边靠近
if(player.getX()-pan.getX() == 50) {
return 2;
}else if(pan.getX()-player.getX() ==50){
//从左方靠近
return 0;
}
}else if(player.getX() == pan.getX()){
//从下方靠近
if(player.getY() - pan.getY() == 50) {
return 3;
}else if(pan.getY() - player.getY() == 50){
//从上方靠近
return 1;
}
}
return -1;
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased"+e.getKeyCode()+"|"+e.getKeyChar());
}
}
}
成果截图:
3、焦点事件
常用方法:
示例代码:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class TestFocusListeren extends JFrame{
//定义面板组件
private JTextField userNameJtf;
private JComboBox<Double> userMoneyBox;
private JPasswordField userPwd;
private JPasswordField reUserPwd;
private JButton confirmBtn;
private JButton cancleBtn;
public TestFocusListeren() {
super("TestFocusListeren");
this.setSize(400,300);
this.setLocationRelativeTo(null);
// this.setLayout(null);
addComponent();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//添加焦点事件
Fls fis = new Fls();
this.userNameJtf.addFocusListener(fis);
}
class Fls implements FocusListener{
@Override
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
//判断是否用户姓名组件失去焦点
if(e.getSource() == userNameJtf) {
if(userNameJtf.getText().trim().length()<=1) {
JOptionPane.showMessageDialog(TestFocusListeren.this, "用户名不能为空,且字符数大于两个字符!","错误",JOptionPane.ERROR_MESSAGE);
//清空输入
userNameJtf.setText("");
//主动获取焦点
userNameJtf.requestFocus();
}
}
}
}
private void addComponent() {
//添加信息
JPanel pan = new JPanel(new GridLayout(4,2,5,15));
//定义边框
pan.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"注册信息"));
//添加组件到面板
pan.add(new JLabel("用户姓名:"));
userNameJtf = new JTextField();
pan.add(userNameJtf);
//开户金额
Double[] moneys = {1000.0,2000.0,3000.0,5000.0,10000.0};
userMoneyBox = new JComboBox<Double>(moneys);
pan.add(new JLabel("开户金额:"));
pan.add(userMoneyBox);
//用户密码
pan.add(new Label("用户密码:"));
userPwd = new JPasswordField();
pan.add(userPwd);
//确认密码
pan.add(new Label("确认密码:"));
reUserPwd = new JPasswordField();
pan.add(reUserPwd);
//添加注册信息组件到中部
this.add(pan);
//添加按钮
JPanel btnPan = new JPanel(new FlowLayout(FlowLayout.CENTER,50,5));
cancleBtn = new JButton("取消");
btnPan.add(cancleBtn);
confirmBtn = new JButton("注册");
btnPan.add(confirmBtn);
//添加按钮面板到南部
this.add(btnPan,BorderLayout.SOUTH);
}
public static void main(String[] args) {
new TestFocusListeren();
}
}
成果截图:
4、窗口事件
常见方法:
示例代码:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.yueqian.myswing2.TestFocusListeren.Fls;
/**
* 窗口事件
* @author LinChi
*
*/
public class TestWindowListener extends JFrame{
public TestWindowListener() {
super("TestWindowListener");
this.setSize(400,300);
this.setLocationRelativeTo(null);
// this.setLayout(null);
// addComponent();
//添加按钮
JButton jb = new JButton("关闭");
//使用布局管理器添加到南部
this.add(jb,BorderLayout.SOUTH);
//添加事件
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//关闭窗口
TestWindowListener.this.dispose();
}
});
//添加窗口事件
WinLis sl = new WinLis();
this.addWindowListener(sl);
this.addWindowStateListener(sl);
this.addWindowFocusListener(sl);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class WinLis implements WindowListener,WindowStateListener,WindowFocusListener{
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened...");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing...");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed...");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("windowIconified...");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("windowDeiconified...");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated...");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("windowDeactivated...");
}
@Override
public void windowStateChanged(WindowEvent e) {
System.out.println("windowStateChanged---");
}
@Override
public void windowGainedFocus(WindowEvent e) {
//窗口得到焦点
System.out.println("windowGainedFocus...");
}
@Override
public void windowLostFocus(WindowEvent e) {
//窗口得到焦点
System.out.println("windowLostFocus...");
}
}
public static void main(String[] args) {
new TestWindowListener();
}
}
成果截图:
5、事件适配器
示例代码:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
* 鼠标事件适配器
* @author LinChi
*
*/
public class MouseLis extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
super.mousePressed(e);
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseDragged(e);
}
}
今天学到的就分享完了,期待明天的精彩内容吧!